From e6f82341d10d7cf372e390da7720d52253f84828 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Wed, 11 Jul 2018 11:45:45 -0400 Subject: [PATCH] Patches for private fields, isModelClass, etc.; bump to 2.0.10 --- angel_serialize/.packages | 2 +- angel_serialize/CHANGELOG.md | 3 + angel_serialize/analysis_options.yaml | 3 +- angel_serialize/lib/angel_serialize.dart | 7 ++ angel_serialize/pubspec.yaml | 2 +- angel_serialize_generator/CHANGELOG.md | 8 ++ .../analysis_options.yaml | 3 +- angel_serialize_generator/build.yaml | 5 +- .../lib/angel_serialize_generator.dart | 17 ++- .../lib/build_context.dart | 11 +- angel_serialize_generator/lib/model.dart | 51 ++++---- angel_serialize_generator/lib/serialize.dart | 26 +++- angel_serialize_generator/lib/typescript.dart | 116 +++++++++--------- angel_serialize_generator/pubspec.yaml | 2 +- .../test/models/author.g.dart | 3 + .../test/models/author.serializer.g.dart | 12 +- .../test/models/book.g.dart | 1 + .../test/models/book.serializer.g.dart | 4 +- .../test/models/game_pad.dart | 12 ++ .../test/models/game_pad.g.dart | 41 +++++++ .../test/models/game_pad.serializer.g.dart | 35 ++++++ .../test/models/game_pad_button.dart | 9 ++ .../test/models/game_pad_button.g.dart | 33 +++++ .../models/game_pad_button.serializer.g.dart | 27 ++++ .../test/models/with_enum.g.dart | 3 +- 25 files changed, 329 insertions(+), 107 deletions(-) create mode 100644 angel_serialize_generator/test/models/game_pad.dart create mode 100644 angel_serialize_generator/test/models/game_pad.g.dart create mode 100644 angel_serialize_generator/test/models/game_pad.serializer.g.dart create mode 100644 angel_serialize_generator/test/models/game_pad_button.dart create mode 100644 angel_serialize_generator/test/models/game_pad_button.g.dart create mode 100644 angel_serialize_generator/test/models/game_pad_button.serializer.g.dart diff --git a/angel_serialize/.packages b/angel_serialize/.packages index 133d63ce..3c13d586 100644 --- a/angel_serialize/.packages +++ b/angel_serialize/.packages @@ -1,2 +1,2 @@ -# Generated by pub on 2018-06-29 00:08:13.330472. +# Generated by pub on 2018-07-11 11:34:17.168572. angel_serialize:lib/ diff --git a/angel_serialize/CHANGELOG.md b/angel_serialize/CHANGELOG.md index 172a30c0..ca801123 100644 --- a/angel_serialize/CHANGELOG.md +++ b/angel_serialize/CHANGELOG.md @@ -1,3 +1,6 @@ +# 2.0.4 +* Added `generatedSerializable`. + # 2.0.3 * Increased the upper SDK boundary. diff --git a/angel_serialize/analysis_options.yaml b/angel_serialize/analysis_options.yaml index 518eb901..eae1e42a 100644 --- a/angel_serialize/analysis_options.yaml +++ b/angel_serialize/analysis_options.yaml @@ -1,2 +1,3 @@ analyzer: - strong-mode: true \ No newline at end of file + strong-mode: + implicit-casts: false \ No newline at end of file diff --git a/angel_serialize/lib/angel_serialize.dart b/angel_serialize/lib/angel_serialize.dart index 12e5c0f6..51c462e6 100644 --- a/angel_serialize/lib/angel_serialize.dart +++ b/angel_serialize/lib/angel_serialize.dart @@ -37,6 +37,13 @@ class Serializable { const Serializable serializable = const Serializable(); +/// Used by `package:angel_serialize_generator` to reliably identify generated models. +class GeneratedSerializable { + const GeneratedSerializable(); +} + +const GeneratedSerializable generatedSerializable = const GeneratedSerializable(); + /// The supported serialization types. abstract class Serializers { /// All supported serialization types. diff --git a/angel_serialize/pubspec.yaml b/angel_serialize/pubspec.yaml index 1b9ba4a7..6ebf4043 100644 --- a/angel_serialize/pubspec.yaml +++ b/angel_serialize/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_serialize -version: 2.0.3 +version: 2.0.4 description: Static annotations powering Angel model serialization. Combine with angel_serialize_generator for flexible modeling. author: Tobe O homepage: https://github.com/angel-dart/serialize diff --git a/angel_serialize_generator/CHANGELOG.md b/angel_serialize_generator/CHANGELOG.md index cc6af2a9..df5f50c2 100644 --- a/angel_serialize_generator/CHANGELOG.md +++ b/angel_serialize_generator/CHANGELOG.md @@ -1,3 +1,11 @@ +# 2.0.10 +* Generate `XFields.allFields` constant. +* No longer breaks in cases where `dynamic` is present. +* Call `toJson` in `toMap` on nested models. +* Never generate named parameters from private fields. +* Use the new `@generatedSerializable` to *always* find generated +models. + # 2.0.9+4 * Remove `defaults` in `build.yaml`. diff --git a/angel_serialize_generator/analysis_options.yaml b/angel_serialize_generator/analysis_options.yaml index 518eb901..eae1e42a 100644 --- a/angel_serialize_generator/analysis_options.yaml +++ b/angel_serialize_generator/analysis_options.yaml @@ -1,2 +1,3 @@ analyzer: - strong-mode: true \ No newline at end of file + strong-mode: + implicit-casts: false \ No newline at end of file diff --git a/angel_serialize_generator/build.yaml b/angel_serialize_generator/build.yaml index 7980a8b9..6d8b75f4 100644 --- a/angel_serialize_generator/build.yaml +++ b/angel_serialize_generator/build.yaml @@ -19,13 +19,14 @@ targets: _book: sources: - "test/models/book.dart" + - "test/models/game_pad_button.dart" - "test/models/with_enum.dart" _typescript_definition: sources: - - "lib/*.dart" + - "test/*.dart" $default: dependencies: - ":_book" sources: - - "lib/*.dart" - "test/models/author.dart" + - "test/models/game_pad.dart" diff --git a/angel_serialize_generator/lib/angel_serialize_generator.dart b/angel_serialize_generator/lib/angel_serialize_generator.dart index c361f677..dff6d31b 100644 --- a/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/angel_serialize_generator/lib/angel_serialize_generator.dart @@ -53,7 +53,17 @@ TypeReference convertTypeReference(DartType t) { bool isModelClass(DartType t) { if (t == null) return false; - if (serializableTypeChecker.hasAnnotationOf(t.element)) return true; + if (serializableTypeChecker.hasAnnotationOf(t.element)) { + return true; + } + + if (generatedSerializableTypeChecker.hasAnnotationOf(t.element)) { + return true; + } + + if (const TypeChecker.fromRuntime(Model).isAssignableFromType(t)) { + return true; + } if (t is InterfaceType) { return isModelClass(t.superclass); @@ -76,7 +86,7 @@ bool isEnumType(DartType t) { } /// Determines if a [DartType] is a `List` with the first type argument being a `Model`. -bool isListModelType(InterfaceType t) { +bool isListOfModelType(InterfaceType t) { return const TypeChecker.fromRuntime(List).isAssignableFromType(t) && t.typeArguments.length == 1 && isModelClass(t.typeArguments[0]); @@ -89,7 +99,8 @@ bool isMapToModelType(InterfaceType t) { isModelClass(t.typeArguments[1]); } -bool isAssignableToModel(DartType type) => const TypeChecker.fromRuntime(Model).isAssignableFromType(type); +bool isAssignableToModel(DartType type) => + const TypeChecker.fromRuntime(Model).isAssignableFromType(type); /// Compute a [String] representation of a [type]. String typeToString(DartType type) { diff --git a/angel_serialize_generator/lib/build_context.dart b/angel_serialize_generator/lib/build_context.dart index 54d1d5b9..38e0869a 100644 --- a/angel_serialize_generator/lib/build_context.dart +++ b/angel_serialize_generator/lib/build_context.dart @@ -19,6 +19,9 @@ const TypeChecker excludeTypeChecker = const TypeChecker.fromRuntime(Exclude); const TypeChecker serializableTypeChecker = const TypeChecker.fromRuntime(Serializable); +const TypeChecker generatedSerializableTypeChecker = + const TypeChecker.fromRuntime(GeneratedSerializable); + /// Create a [BuildContext]. Future buildContext( ClassElement clazz, @@ -43,6 +46,11 @@ Future buildContext( List fieldNames = []; for (var field in clazz.fields) { + // Skip private fields + if (field.name.startsWith('_')) { + continue; + } + if (field.getter != null && (field.setter != null || field.getter.isAbstract)) { var el = field.setter == null ? field.getter : field; @@ -80,8 +88,7 @@ Future buildContext( if (required != null) { var cr = new ConstantReader(required); var reason = cr.peek('reason')?.stringValue ?? - "Missing required field '${ctx.resolveFieldName(field.name)}' on ${ctx - .modelClassName}."; + "Missing required field '${ctx.resolveFieldName(field.name)}' on ${ctx.modelClassName}."; ctx.requiredFields[field.name] = reason; } diff --git a/angel_serialize_generator/lib/model.dart b/angel_serialize_generator/lib/model.dart index fbc008e9..7ec76a53 100644 --- a/angel_serialize_generator/lib/model.dart +++ b/angel_serialize_generator/lib/model.dart @@ -11,7 +11,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { if (element.kind != ElementKind.CLASS) throw 'Only classes can be annotated with a @Serializable() annotation.'; - var ctx = await buildContext(element, annotation, buildStep, + var ctx = await buildContext(element as ClassElement, annotation, buildStep, await buildStep.resolver, true, autoIdAndDateFields != false); var lib = new Library((b) { @@ -26,7 +26,9 @@ class JsonModelGenerator extends GeneratorForAnnotation { void generateClass( BuildContext ctx, LibraryBuilder file, ConstantReader annotation) { file.body.add(new Class((clazz) { - clazz..name = ctx.modelClassNameRecase.pascalCase; + clazz + ..name = ctx.modelClassNameRecase.pascalCase + ..annotations.add(refer('generatedSerializable')); if (shouldBeConstant(ctx)) { clazz.implements.add(new Reference(ctx.originalClassName)); @@ -122,8 +124,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { if (!shouldBeConstant(ctx) || ctx.clazz.unnamedConstructor?.isConst == true) constructor.initializers.add(new Code( - 'super(${ctx.constructorParameters.map((p) => p.name).join( - ',')})')); + 'super(${ctx.constructorParameters.map((p) => p.name).join(',')})')); } })); } @@ -172,30 +173,32 @@ class JsonModelGenerator extends GeneratorForAnnotation { } static String generateEquality(DartType type, [bool nullable = false]) { -//if (type is! InterfaceType) return 'const DefaultEquality()'; - var it = type as InterfaceType; - if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) { - if (it.typeParameters.length == 1) { - var eq = generateEquality(it.typeArguments[0]); - return 'const ListEquality<${it.typeArguments[0].name}>($eq)'; - } else - return 'const ListEquality<${it.typeArguments[0].name}>()'; - } else if (const TypeChecker.fromRuntime(Map).isAssignableFromType(type)) { - if (it.typeParameters.length == 2) { - var keq = generateEquality(it.typeArguments[0]), - veq = generateEquality(it.typeArguments[1]); - return 'const MapEquality<${it.typeArguments[0].name}, ${it - .typeArguments[1].name}>(keys: $keq, values: $veq)'; - } else - return 'const MapEquality()<${it.typeArguments[0].name}, ${it - .typeArguments[1].name}>'; - } + if (type is InterfaceType) { + if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) { + if (type.typeParameters.length == 1) { + var eq = generateEquality(type.typeArguments[0]); + return 'const ListEquality<${type.typeArguments[0].name}>($eq)'; + } else + return 'const ListEquality<${type.typeArguments[0].name}>()'; + } else if (const TypeChecker.fromRuntime(Map) + .isAssignableFromType(type)) { + if (type.typeParameters.length == 2) { + var keq = generateEquality(type.typeArguments[0]), + veq = generateEquality(type.typeArguments[1]); + return 'const MapEquality<${type.typeArguments[0].name}, ${type.typeArguments[1].name}>(keys: $keq, values: $veq)'; + } else + return 'const MapEquality()<${type.typeArguments[0].name}, ${type.typeArguments[1].name}>'; + } - return nullable ? null : 'const DefaultEquality<${type.name}>()'; + return nullable ? null : 'const DefaultEquality<${type.name}>()'; + } else { + return 'const DefaultEquality()'; + } } static String Function(String, String) generateComparator(DartType type) { - if (type is! InterfaceType) return (a, b) => '$a == $b'; + if (type is! InterfaceType || type.name == 'dynamic') + return (a, b) => '$a == $b'; var eq = generateEquality(type, true); if (eq == null) return (a, b) => '$a == $b'; return (a, b) => '$eq.equals($a, $b)'; diff --git a/angel_serialize_generator/lib/serialize.dart b/angel_serialize_generator/lib/serialize.dart index de5ca713..70300795 100644 --- a/angel_serialize_generator/lib/serialize.dart +++ b/angel_serialize_generator/lib/serialize.dart @@ -11,7 +11,7 @@ class SerializerGenerator extends GeneratorForAnnotation { if (element.kind != ElementKind.CLASS) throw 'Only classes can be annotated with a @Serializable() annotation.'; - var ctx = await buildContext(element, annotation, buildStep, + var ctx = await buildContext(element as ClassElement, annotation, buildStep, await buildStep.resolver, true, autoSnakeCaseNames != false); var serializers = annotation.peek('serializers')?.listValue ?? []; @@ -109,7 +109,7 @@ class SerializerGenerator extends GeneratorForAnnotation { } else if (field.type is InterfaceType) { var t = field.type as InterfaceType; - if (isListModelType(t)) { + if (isListOfModelType(t)) { //var rc = new ReCase(t.typeArguments[0].name); serializedRepresentation = ''' model.${field.name} @@ -191,13 +191,13 @@ class SerializerGenerator extends GeneratorForAnnotation { if (i++ > 0) buf.write(', '); - String deserializedRepresentation = "map['$alias'] as ${typeToString( - field.type)}"; + String deserializedRepresentation = + "map['$alias'] as ${typeToString(field.type)}"; // Deserialize dates if (dateTimeTypeChecker.isAssignableFromType(field.type)) deserializedRepresentation = "map['$alias'] != null ? " - "(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias']))" + "(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias'].toString()))" " : null"; // Serialize model classes via `XSerializer.toMap` @@ -209,7 +209,7 @@ class SerializerGenerator extends GeneratorForAnnotation { } else if (field.type is InterfaceType) { var t = field.type as InterfaceType; - if (isListModelType(t)) { + if (isListOfModelType(t)) { var rc = new ReCase(t.typeArguments[0].name); deserializedRepresentation = "map['$alias'] is Iterable" " ? new List.unmodifiable(((map['$alias'] as Iterable)" @@ -254,6 +254,20 @@ class SerializerGenerator extends GeneratorForAnnotation { ..abstract = true ..name = '${ctx.modelClassNameRecase.pascalCase}Fields'; + clazz.fields.add(new Field((b) { + b + ..static = true + ..modifier = FieldModifier.constant + ..type = new TypeReference((b) => b + ..symbol = 'List' + ..types.add(refer('String'))) + ..name = 'allFields' + ..assignment = literalConstList( + ctx.fields.map((f) => refer(f.name)).toList(), + refer('String')) + .code; + })); + for (var field in ctx.fields) { clazz.fields.add(new Field((b) { b diff --git a/angel_serialize_generator/lib/typescript.dart b/angel_serialize_generator/lib/typescript.dart index bf8bb592..a90bd213 100644 --- a/angel_serialize_generator/lib/typescript.dart +++ b/angel_serialize_generator/lib/typescript.dart @@ -15,7 +15,7 @@ class TypeScriptDefinitionBuilder implements Builder { Future compileToTypeScriptType( BuildContext ctx, String fieldName, - InterfaceType type, + DartType type, List refs, List ext, BuildStep buildStep) async { @@ -33,18 +33,20 @@ class TypeScriptDefinitionBuilder implements Builder { typeScriptType = tsType; }); - if (isListModelType(type)) { - var arg = await compileToTypeScriptType( - ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); - typeScriptType = '$arg[]'; - } else if (const TypeChecker.fromRuntime(Map).isAssignableFromType(type) && - type.typeArguments.length == 2) { - var key = await compileToTypeScriptType( - ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); - var value = await compileToTypeScriptType( - ctx, fieldName, type.typeArguments[1], refs, ext, buildStep); - //var modelType = type.typeArguments[1]; - /*var innerCtx = await buildContext( + if (type is InterfaceType) { + if (isListOfModelType(type)) { + var arg = await compileToTypeScriptType( + ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); + typeScriptType = '$arg[]'; + } else if (const TypeChecker.fromRuntime(Map) + .isAssignableFromType(type) && + type.typeArguments.length == 2) { + var key = await compileToTypeScriptType( + ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); + var value = await compileToTypeScriptType( + ctx, fieldName, type.typeArguments[1], refs, ext, buildStep); + //var modelType = type.typeArguments[1]; + /*var innerCtx = await buildContext( modelType.element, new ConstantReader( serializableTypeChecker.firstAnnotationOf(modelType.element)), @@ -54,50 +56,52 @@ class TypeScriptDefinitionBuilder implements Builder { true, );*/ - typeScriptType = ctx.modelClassNameRecase.pascalCase + - new ReCase(fieldName).pascalCase; + typeScriptType = ctx.modelClassNameRecase.pascalCase + + new ReCase(fieldName).pascalCase; - ext.add(new CodeBuffer() - ..writeln('interface $typeScriptType {') - ..indent() - ..writeln('[key: $key]: $value;') - ..outdent() - ..writeln('}')); - } else if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) { - if (type.typeArguments.length == 0) - typeScriptType = 'any[]'; - else { - var arg = await compileToTypeScriptType( - ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); - typeScriptType = '$arg[]'; + ext.add(new CodeBuffer() + ..writeln('interface $typeScriptType {') + ..indent() + ..writeln('[key: $key]: $value;') + ..outdent() + ..writeln('}')); + } else if (const TypeChecker.fromRuntime(List) + .isAssignableFromType(type)) { + if (type.typeArguments.length == 0) + typeScriptType = 'any[]'; + else { + var arg = await compileToTypeScriptType( + ctx, fieldName, type.typeArguments[0], refs, ext, buildStep); + typeScriptType = '$arg[]'; + } + } else if (isModelClass(type)) { + var sourcePath = buildStep.inputId.uri.toString(); + var targetPath = type.element.source.uri.toString(); + + if (!p.equals(sourcePath, targetPath)) { + //var relative = p.relative(targetPath, from: sourcePath); + var relative = (p.dirname(targetPath) == p.dirname(sourcePath)) + ? p.basename(targetPath) + : p.relative(targetPath, from: sourcePath); + var parent = p.dirname(relative); + var filename = + p.setExtension(p.basenameWithoutExtension(relative), '.d.ts'); + relative = p.joinAll(p.split(parent).toList()..add(filename)); + var ref = '/// '; + if (!refs.contains(ref)) refs.add(ref); + } + + var ctx = await buildContext( + type.element, + new ConstantReader( + serializableTypeChecker.firstAnnotationOf(type.element)), + buildStep, + buildStep.resolver, + autoSnakeCaseNames, + true, + ); + typeScriptType = ctx.modelClassNameRecase.pascalCase; } - } else if (isModelClass(type)) { - var sourcePath = buildStep.inputId.uri.toString(); - var targetPath = type.element.source.uri.toString(); - - if (!p.equals(sourcePath, targetPath)) { - //var relative = p.relative(targetPath, from: sourcePath); - var relative = (p.dirname(targetPath) == p.dirname(sourcePath)) - ? p.basename(targetPath) - : p.relative(targetPath, from: sourcePath); - var parent = p.dirname(relative); - var filename = - p.setExtension(p.basenameWithoutExtension(relative), '.d.ts'); - relative = p.joinAll(p.split(parent).toList()..add(filename)); - var ref = '/// '; - if (!refs.contains(ref)) refs.add(ref); - } - - var ctx = await buildContext( - type.element, - new ConstantReader( - serializableTypeChecker.firstAnnotationOf(type.element)), - buildStep, - buildStep.resolver, - autoSnakeCaseNames, - true, - ); - typeScriptType = ctx.modelClassNameRecase.pascalCase; } return typeScriptType; @@ -133,7 +137,7 @@ class TypeScriptDefinitionBuilder implements Builder { } contexts.add(await buildContext( - element.element, + element.element as ClassElement, element.annotation, buildStep, await buildStep.resolver, diff --git a/angel_serialize_generator/pubspec.yaml b/angel_serialize_generator/pubspec.yaml index 85b9ad19..c39670b8 100644 --- a/angel_serialize_generator/pubspec.yaml +++ b/angel_serialize_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_serialize_generator -version: 2.0.9+4 +version: 2.0.10 description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. author: Tobe O homepage: https://github.com/angel-dart/serialize diff --git a/angel_serialize_generator/test/models/author.g.dart b/angel_serialize_generator/test/models/author.g.dart index b5b7e1df..98234677 100644 --- a/angel_serialize_generator/test/models/author.g.dart +++ b/angel_serialize_generator/test/models/author.g.dart @@ -6,6 +6,7 @@ part of angel_serialize.test.models.author; // JsonModelGenerator // ************************************************************************** +@generatedSerializable class Author extends _Author { Author( {this.id, @@ -87,6 +88,7 @@ class Author extends _Author { } } +@generatedSerializable class Library extends _Library { Library( {this.id, Map collection, this.createdAt, this.updatedAt}) @@ -132,6 +134,7 @@ class Library extends _Library { } } +@generatedSerializable class Bookmark extends _Bookmark { Bookmark(Book book, {this.id, diff --git a/angel_serialize_generator/test/models/author.serializer.g.dart b/angel_serialize_generator/test/models/author.serializer.g.dart index a39b3125..c14b32e7 100644 --- a/angel_serialize_generator/test/models/author.serializer.g.dart +++ b/angel_serialize_generator/test/models/author.serializer.g.dart @@ -32,12 +32,12 @@ abstract class AuthorSerializer { createdAt: map['created_at'] != null ? (map['created_at'] is DateTime ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'])) + : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'])) + : DateTime.parse(map['updated_at'].toString())) : null); } @@ -100,12 +100,12 @@ abstract class LibrarySerializer { createdAt: map['created_at'] != null ? (map['created_at'] is DateTime ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'])) + : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'])) + : DateTime.parse(map['updated_at'].toString())) : null); } @@ -148,12 +148,12 @@ abstract class BookmarkSerializer { createdAt: map['created_at'] != null ? (map['created_at'] is DateTime ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'])) + : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'])) + : DateTime.parse(map['updated_at'].toString())) : null); } diff --git a/angel_serialize_generator/test/models/book.g.dart b/angel_serialize_generator/test/models/book.g.dart index 4f2382dc..3f3be261 100644 --- a/angel_serialize_generator/test/models/book.g.dart +++ b/angel_serialize_generator/test/models/book.g.dart @@ -6,6 +6,7 @@ part of angel_serialize.test.models.book; // JsonModelGenerator // ************************************************************************** +@generatedSerializable class Book extends _Book { Book( {this.id, diff --git a/angel_serialize_generator/test/models/book.serializer.g.dart b/angel_serialize_generator/test/models/book.serializer.g.dart index b5c8eb1a..4eb44cce 100644 --- a/angel_serialize_generator/test/models/book.serializer.g.dart +++ b/angel_serialize_generator/test/models/book.serializer.g.dart @@ -19,12 +19,12 @@ abstract class BookSerializer { createdAt: map['created_at'] != null ? (map['created_at'] is DateTime ? (map['created_at'] as DateTime) - : DateTime.parse(map['created_at'])) + : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime ? (map['updated_at'] as DateTime) - : DateTime.parse(map['updated_at'])) + : DateTime.parse(map['updated_at'].toString())) : null); } diff --git a/angel_serialize_generator/test/models/game_pad.dart b/angel_serialize_generator/test/models/game_pad.dart new file mode 100644 index 00000000..9d158051 --- /dev/null +++ b/angel_serialize_generator/test/models/game_pad.dart @@ -0,0 +1,12 @@ +import 'package:angel_serialize/angel_serialize.dart'; +import 'package:collection/collection.dart'; +import 'game_pad_button.dart'; +part 'game_pad.g.dart'; +part 'game_pad.serializer.g.dart'; + +@Serializable(autoIdAndDateFields: false) +abstract class _Gamepad { + List get buttons; + + Map get dynamicMap; +} \ No newline at end of file diff --git a/angel_serialize_generator/test/models/game_pad.g.dart b/angel_serialize_generator/test/models/game_pad.g.dart new file mode 100644 index 00000000..fedaf393 --- /dev/null +++ b/angel_serialize_generator/test/models/game_pad.g.dart @@ -0,0 +1,41 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'game_pad.dart'; + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class Gamepad implements _Gamepad { + const Gamepad( + {List this.buttons, Map this.dynamicMap}); + + @override + final List buttons; + + @override + final Map dynamicMap; + + Gamepad copyWith( + {List buttons, Map dynamicMap}) { + return new Gamepad( + buttons: buttons ?? this.buttons, + dynamicMap: dynamicMap ?? this.dynamicMap); + } + + bool operator ==(other) { + return other is _Gamepad && + const ListEquality( + const DefaultEquality()) + .equals(other.buttons, buttons) && + const MapEquality( + keys: const DefaultEquality(), + values: const DefaultEquality()) + .equals(other.dynamicMap, dynamicMap); + } + + Map toJson() { + return GamepadSerializer.toMap(this); + } +} diff --git a/angel_serialize_generator/test/models/game_pad.serializer.g.dart b/angel_serialize_generator/test/models/game_pad.serializer.g.dart new file mode 100644 index 00000000..7f60eb7e --- /dev/null +++ b/angel_serialize_generator/test/models/game_pad.serializer.g.dart @@ -0,0 +1,35 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'game_pad.dart'; + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +abstract class GamepadSerializer { + static Gamepad fromMap(Map map) { + return new Gamepad( + buttons: map['buttons'] is Iterable + ? new List.unmodifiable(((map['buttons'] as Iterable) + .where((x) => x is Map) as Iterable) + .map(GamepadButtonSerializer.fromMap)) + : null, + dynamicMap: map['dynamic_map'] as Map); + } + + static Map toMap(Gamepad model) { + if (model == null) { + return null; + } + return { + 'buttons': model.buttons?.map((m) => m.toJson())?.toList(), + 'dynamic_map': model.dynamicMap + }; + } +} + +abstract class GamepadFields { + static const String buttons = 'buttons'; + + static const String dynamicMap = 'dynamic_map'; +} diff --git a/angel_serialize_generator/test/models/game_pad_button.dart b/angel_serialize_generator/test/models/game_pad_button.dart new file mode 100644 index 00000000..fe61c5aa --- /dev/null +++ b/angel_serialize_generator/test/models/game_pad_button.dart @@ -0,0 +1,9 @@ +import 'package:angel_serialize/angel_serialize.dart'; +part 'game_pad_button.g.dart'; +part 'game_pad_button.serializer.g.dart'; + +@Serializable(autoIdAndDateFields: false) +abstract class _GamepadButton { + String get name; + int get radius; +} \ No newline at end of file diff --git a/angel_serialize_generator/test/models/game_pad_button.g.dart b/angel_serialize_generator/test/models/game_pad_button.g.dart new file mode 100644 index 00000000..1c1ce0d1 --- /dev/null +++ b/angel_serialize_generator/test/models/game_pad_button.g.dart @@ -0,0 +1,33 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'game_pad_button.dart'; + +// ************************************************************************** +// JsonModelGenerator +// ************************************************************************** + +@generatedSerializable +class GamepadButton implements _GamepadButton { + const GamepadButton({this.name, this.radius}); + + @override + final String name; + + @override + final int radius; + + GamepadButton copyWith({String name, int radius}) { + return new GamepadButton( + name: name ?? this.name, radius: radius ?? this.radius); + } + + bool operator ==(other) { + return other is _GamepadButton && + other.name == name && + other.radius == radius; + } + + Map toJson() { + return GamepadButtonSerializer.toMap(this); + } +} diff --git a/angel_serialize_generator/test/models/game_pad_button.serializer.g.dart b/angel_serialize_generator/test/models/game_pad_button.serializer.g.dart new file mode 100644 index 00000000..06d3ad1a --- /dev/null +++ b/angel_serialize_generator/test/models/game_pad_button.serializer.g.dart @@ -0,0 +1,27 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'game_pad_button.dart'; + +// ************************************************************************** +// SerializerGenerator +// ************************************************************************** + +abstract class GamepadButtonSerializer { + static GamepadButton fromMap(Map map) { + return new GamepadButton( + name: map['name'] as String, radius: map['radius'] as int); + } + + static Map toMap(GamepadButton model) { + if (model == null) { + return null; + } + return {'name': model.name, 'radius': model.radius}; + } +} + +abstract class GamepadButtonFields { + static const String name = 'name'; + + static const String radius = 'radius'; +} diff --git a/angel_serialize_generator/test/models/with_enum.g.dart b/angel_serialize_generator/test/models/with_enum.g.dart index aeef25e6..632f919d 100644 --- a/angel_serialize_generator/test/models/with_enum.g.dart +++ b/angel_serialize_generator/test/models/with_enum.g.dart @@ -3,9 +3,10 @@ part of 'with_enum.dart'; // ************************************************************************** -// Generator: JsonModelGenerator +// JsonModelGenerator // ************************************************************************** +@generatedSerializable class WithEnum implements _WithEnum { const WithEnum({this.type, List this.finalList});