|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// ignore_for_file: deprecated_member_use |
| 6 | + |
| 7 | +// There is no public API exposed yet, the in progress api lives here. |
| 8 | +import 'package:_fe_analyzer_shared/src/macros/api.dart'; |
| 9 | + |
| 10 | +final dartCore = Uri.parse('dart:core'); |
| 11 | + |
| 12 | +// TODO: Support `toJson`, collections, and probably some other things :). |
| 13 | +macro class JsonSerializable |
| 14 | + implements ClassDeclarationsMacro, ClassDefinitionMacro { |
| 15 | + const JsonSerializable(); |
| 16 | + |
| 17 | + @override |
| 18 | + Future<void> buildDeclarationsForClass( |
| 19 | + ClassDeclaration clazz, ClassMemberDeclarationBuilder builder) async { |
| 20 | + var constructors = await builder.constructorsOf(clazz); |
| 21 | + if (constructors.any((c) => c.identifier.name == 'fromJson')) { |
| 22 | + throw ArgumentError('There is already a `fromJson` constructor for ' |
| 23 | + '`${clazz.identifier.name}`, so one could not be added.'); |
| 24 | + } |
| 25 | + |
| 26 | + var map = await builder.resolveIdentifier(dartCore, 'Map'); |
| 27 | + var string = NamedTypeAnnotationCode( |
| 28 | + name: await builder.resolveIdentifier(dartCore, 'String')); |
| 29 | + var dynamic = NamedTypeAnnotationCode( |
| 30 | + name: await builder.resolveIdentifier(dartCore, 'dynamic')); |
| 31 | + var mapStringDynamic = |
| 32 | + NamedTypeAnnotationCode(name: map, typeArguments: [string, dynamic]); |
| 33 | + builder.declareInClass(DeclarationCode.fromParts([ |
| 34 | + 'external ', |
| 35 | + clazz.identifier.name, |
| 36 | + '.fromJson(', |
| 37 | + mapStringDynamic, |
| 38 | + ' json);', |
| 39 | + ])); |
| 40 | + } |
| 41 | + |
| 42 | + @override |
| 43 | + Future<void> buildDefinitionForClass( |
| 44 | + ClassDeclaration clazz, ClassDefinitionBuilder builder) async { |
| 45 | + // TODO: support extending other classes. |
| 46 | + if (clazz.superclass != null) { |
| 47 | + throw UnsupportedError( |
| 48 | + 'Serialization of classes that extend other classes is not supported.'); |
| 49 | + } |
| 50 | + |
| 51 | + var constructors = await builder.constructorsOf(clazz); |
| 52 | + var fromJson = |
| 53 | + constructors.firstWhere((c) => c.identifier.name == 'fromJson'); |
| 54 | + var fromJsonBuilder = await builder.buildConstructor(fromJson.identifier); |
| 55 | + var fields = await builder.fieldsOf(clazz); |
| 56 | + var jsonParam = fromJson.positionalParameters.single.identifier; |
| 57 | + fromJsonBuilder.augment(initializers: [ |
| 58 | + for (var field in fields) |
| 59 | + Code.fromParts([ |
| 60 | + field.identifier, |
| 61 | + ' = ', |
| 62 | + await _convertField(field, jsonParam, builder), |
| 63 | + ]), |
| 64 | + ]); |
| 65 | + } |
| 66 | + |
| 67 | + // TODO: Support nested List, Map, etc conversion, including deep casting. |
| 68 | + Future<Code> _convertField(FieldDeclaration field, Identifier jsonParam, |
| 69 | + DefinitionBuilder builder) async { |
| 70 | + var fieldType = field.type; |
| 71 | + if (fieldType is! NamedTypeAnnotation) { |
| 72 | + throw ArgumentError( |
| 73 | + 'Only fields with named types are allowed on serializable classes, ' |
| 74 | + 'but `${field.identifier.name}` was not a named type.'); |
| 75 | + } |
| 76 | + var fieldTypeDecl = await builder.declarationOf(fieldType.identifier); |
| 77 | + while (fieldTypeDecl is TypeAliasDeclaration) { |
| 78 | + var aliasedType = fieldTypeDecl.aliasedType; |
| 79 | + if (aliasedType is! NamedTypeAnnotation) { |
| 80 | + throw ArgumentError( |
| 81 | + 'Only fields with named types are allowed on serializable classes, ' |
| 82 | + 'but `${field.identifier.name}` did not resolve to a named type.'); |
| 83 | + } |
| 84 | + } |
| 85 | + if (fieldTypeDecl is! ClassDeclaration) { |
| 86 | + throw ArgumentError( |
| 87 | + 'Only classes are supported in field types for serializable classes, ' |
| 88 | + 'but the field `${field.identifier.name}` does not have a class ' |
| 89 | + 'type.'); |
| 90 | + } |
| 91 | + |
| 92 | + var fieldConstructors = await builder.constructorsOf(fieldTypeDecl); |
| 93 | + var fieldTypeFromJson = fieldConstructors |
| 94 | + .firstWhereOrNull((c) => c.identifier.name == 'fromJson') |
| 95 | + ?.identifier; |
| 96 | + if (fieldTypeFromJson != null) { |
| 97 | + return Code.fromParts([ |
| 98 | + fieldTypeFromJson, |
| 99 | + '(', |
| 100 | + jsonParam, |
| 101 | + '["${field.identifier.name}"])', |
| 102 | + ]); |
| 103 | + } else { |
| 104 | + return Code.fromParts([ |
| 105 | + jsonParam, |
| 106 | + // TODO: support nested serializable types. |
| 107 | + '["${field.identifier.name}"] as ', |
| 108 | + field.type.code, |
| 109 | + ]); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +extension _FirstWhereOrNull<T> on Iterable<T> { |
| 115 | + T? firstWhereOrNull(bool Function(T) compare) { |
| 116 | + for (var item in this) { |
| 117 | + if (compare(item)) return item; |
| 118 | + } |
| 119 | + return null; |
| 120 | + } |
| 121 | +} |
0 commit comments