diff --git a/packages/serialize/angel_serialize/CHANGELOG.md b/packages/serialize/angel_serialize/CHANGELOG.md index d59da367..ec5800d3 100644 --- a/packages/serialize/angel_serialize/CHANGELOG.md +++ b/packages/serialize/angel_serialize/CHANGELOG.md @@ -1,9 +1,12 @@ # Change Log +## 6.0.0 + +* Updated to min SDK 2.15.x + ## 5.0.0 -* Updated `analyzer` to version 3.x -* Updated min SDK to 2.14.x +* Skipped release ## 4.1.1 diff --git a/packages/serialize/angel_serialize/lib/angel3_serialize.dart b/packages/serialize/angel_serialize/lib/angel3_serialize.dart index cf0fa14a..577219e7 100644 --- a/packages/serialize/angel_serialize/lib/angel3_serialize.dart +++ b/packages/serialize/angel_serialize/lib/angel3_serialize.dart @@ -38,7 +38,7 @@ class SerializableField { final String? alias; /// A default for this field. - final defaultValue; + final dynamic defaultValue; /// A custom serializer for this field. final Symbol? serializer; diff --git a/packages/serialize/angel_serialize/pubspec.yaml b/packages/serialize/angel_serialize/pubspec.yaml index 9a853785..dec7bda1 100644 --- a/packages/serialize/angel_serialize/pubspec.yaml +++ b/packages/serialize/angel_serialize/pubspec.yaml @@ -1,15 +1,18 @@ name: angel3_serialize -version: 4.1.1 +version: 6.0.0 description: Static annotations powering Angel3 model serialization. Combine with angel3_serialize_generator for flexible modeling. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/master/packages/serialize/angel_serialize environment: - sdk: '>=2.12.0 <3.0.0' + sdk: '>=2.15.0 <3.0.0' dependencies: - angel3_model: ^3.0.0 + angel3_model: ^6.0.0 collection: ^1.15.0 meta: ^1.3.0 quiver: ^3.0.1 dev_dependencies: lints: ^1.0.0 +dependency_overrides: + angel3_model: + path: ../../model diff --git a/packages/serialize/angel_serialize_generator/AUTHORS.md b/packages/serialize/angel_serialize_generator/AUTHORS.md index ac95ab58..6ae218e2 100644 --- a/packages/serialize/angel_serialize_generator/AUTHORS.md +++ b/packages/serialize/angel_serialize_generator/AUTHORS.md @@ -3,7 +3,7 @@ Primary Authors * __[Thomas Hii](dukefirehawk.apps@gmail.com)__ - Thomas is the current maintainer of the code base. He has refactored and migrated the + Thomas is the current maintainer of the code base. He has refactored and migrated the code base to support NNBD. * __[Tobe O](thosakwe@gmail.com)__ diff --git a/packages/serialize/angel_serialize_generator/CHANGELOG.md b/packages/serialize/angel_serialize_generator/CHANGELOG.md index a4991c69..8f8292ad 100644 --- a/packages/serialize/angel_serialize_generator/CHANGELOG.md +++ b/packages/serialize/angel_serialize_generator/CHANGELOG.md @@ -1,9 +1,14 @@ # Change Log +## 6.0.0 + +* Updated to min SDK 2.15.x +* Updated to `analyzer` 3.x.x +* Fixed default value for `List` and `Enum` + ## 5.0.0 -* Updated `analyzer` to version 3.x -* Updated min SDK to 2.14.x +* Skipped release ## 4.3.0 diff --git a/packages/serialize/angel_serialize_generator/example/main.g.dart b/packages/serialize/angel_serialize_generator/example/main.g.dart index e5acf8ad..4a8905ed 100644 --- a/packages/serialize/angel_serialize_generator/example/main.g.dart +++ b/packages/serialize/angel_serialize_generator/example/main.g.dart @@ -66,16 +66,17 @@ class TodoSerializer extends Codec { @override TodoEncoder get encoder => const TodoEncoder(); - @override TodoDecoder get decoder => const TodoDecoder(); - static Todo fromMap(Map map) { return Todo( text: map['text'] as String?, completed: map['completed'] as bool?); } - static Map toMap(_Todo model) { + static Map toMap(_Todo? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); + } return {'text': model.text, 'completed': model.completed}; } } diff --git a/packages/serialize/angel_serialize_generator/lib/build_context.dart b/packages/serialize/angel_serialize_generator/lib/build_context.dart index c7dbfb8d..f8a5b242 100644 --- a/packages/serialize/angel_serialize_generator/lib/build_context.dart +++ b/packages/serialize/angel_serialize_generator/lib/build_context.dart @@ -215,32 +215,6 @@ Future buildContext( } } - // ShimFields are no longer used. - // if (const TypeChecker.fromRuntime(Model).isAssignableFromType(clazz.type)) { - // if (!fieldNames.contains('id')) { - // var idField = ShimFieldImpl('id', lib.context.typeProvider.stringType); - // ctx.fields.insert(0, idField); - // ctx.shimmed['id'] = true; - // } - - // DartType dateTime; - // for (var key in ['createdAt', 'updatedAt']) { - // if (!fieldNames.contains(key)) { - // if (dateTime == null) { - // var coreLib = - // await resolver.libraries.singleWhere((lib) => lib.isDartCore); - // var dt = coreLib.getType('DateTime'); - // dateTime = dt.type; - // } - - // var field = ShimFieldImpl(key, dateTime); - // ctx.aliases[key] = ReCase(key).snakeCase; - // ctx.fields.add(field); - // ctx.shimmed[key] = true; - // } - // } - // } - // Get constructor params, if any ctx.constructorParameters.addAll(clazz.unnamedConstructor!.parameters); diff --git a/packages/serialize/angel_serialize_generator/lib/model.dart b/packages/serialize/angel_serialize_generator/lib/model.dart index e74051ac..3285cb5e 100644 --- a/packages/serialize/angel_serialize_generator/lib/model.dart +++ b/packages/serialize/angel_serialize_generator/lib/model.dart @@ -14,7 +14,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { buildStep.resolver, true); if (ctx == null) { - log.severe('Invalid builder context'); + log.fine('Invalid builder context'); throw 'Invalid builder context'; } @@ -128,8 +128,8 @@ class JsonModelGenerator extends GeneratorForAnnotation { ? 'List' : 'Map'; String? defaultValue = typeName == 'List' ? '[]' : '{}'; - var existingDefault = ctx.defaults[field.name]; + var existingDefault = ctx.defaults[field.name]; if (existingDefault != null) { defaultValue = dartObjectToString(existingDefault); } @@ -173,7 +173,15 @@ class JsonModelGenerator extends GeneratorForAnnotation { if (!b.toThis) { b.type = convertTypeReference(field.type); } - b.defaultTo = Code('const []'); + + // Get the default if presence + var existingDefault = ctx.defaults[field.name]; + if (existingDefault != null) { + var defaultValue = dartObjectToString(existingDefault); + b.defaultTo = Code('$defaultValue'); + } else { + b.defaultTo = Code('const []'); + } } else if (!b.toThis) { b.type = convertTypeReference(field.type); } else { diff --git a/packages/serialize/angel_serialize_generator/lib/serialize.dart b/packages/serialize/angel_serialize_generator/lib/serialize.dart index 390bbbd7..3c91f21e 100644 --- a/packages/serialize/angel_serialize_generator/lib/serialize.dart +++ b/packages/serialize/angel_serialize_generator/lib/serialize.dart @@ -8,14 +8,14 @@ class SerializerGenerator extends GeneratorForAnnotation { @override Future generateForAnnotatedElement( Element element, ConstantReader annotation, BuildStep buildStep) async { - //log.fine('Running SerializerGenerator'); + log.fine('Running SerializerGenerator'); if (element.kind != ElementKind.CLASS) { throw 'Only classes can be annotated with a @Serializable() annotation.'; } var ctx = await buildContext(element as ClassElement, annotation, buildStep, - buildStep.resolver, autoSnakeCaseNames != false); + buildStep.resolver, !autoSnakeCaseNames); if (ctx == null) { log.severe('Invalid builder context'); @@ -25,11 +25,13 @@ class SerializerGenerator extends GeneratorForAnnotation { var serializers = annotation.peek('serializers')?.listValue ?? []; if (serializers.isEmpty) { + log.severe("No Serializers"); return null; } // Check if any serializer is recognized if (!serializers.any((s) => Serializers.all.contains(s.toIntValue()))) { + log.severe("No recognizable Serializers"); return null; } @@ -46,15 +48,17 @@ class SerializerGenerator extends GeneratorForAnnotation { /// Generate a serializer class. void generateClass( List serializers, BuildContext ctx, LibraryBuilder file) { - //log.fine('Generate serializer class'); + log.fine('Generate serializer class'); // Generate canonical codecs, etc. var pascal = ctx.modelClassNameRecase.pascalCase.replaceAll('?', ''); var camel = ctx.modelClassNameRecase.camelCase.replaceAll('?', ''); - log.info('Generating ${pascal}Serializer'); + log.fine('Generating ${pascal}Serializer'); if (ctx.constructorParameters.isEmpty) { + log.fine("Constructor is empty"); + file.body.add(Code(''' const ${pascal}Serializer ${camel}Serializer = ${pascal}Serializer(); @@ -201,7 +205,7 @@ class ${pascal}Decoder extends Converter { type.typeArguments[1].getDisplayString(withNullability: true)); serializedRepresentation = '''model.${field.name}.keys.fold({}, (map, key) { - return (map as Map?)?..[key] = + return (map as Map?)?..[key] = ${serializerToMap(rc, 'model.${field.name}[key]')}; })'''; } else if (type.element.isEnum) { @@ -235,7 +239,8 @@ class ${pascal}Decoder extends Converter { buf.write('};'); method.body = Block.of([ - Code('if (model == null) { return {}; }'), + Code( + 'if (model == null) { throw FormatException("Required field [model] cannot be null"); }'), Code(buf.toString()), ]); })); @@ -305,13 +310,11 @@ class ${pascal}Decoder extends Converter { var deserializedRepresentation = "map['$alias'] as ${typeToString(type)}"; + if (type.nullabilitySuffix == NullabilitySuffix.question) { deserializedRepresentation += '?'; } - //log.fine( - // 'deserializedRepresentation => $deserializedRepresentation, type => $type, nullcheck => ${type.nullabilitySuffix}'); - var defaultValue = 'null'; var existingDefault = ctx.defaults[field.name]; @@ -319,6 +322,10 @@ class ${pascal}Decoder extends Converter { var d = dartObjectToString(existingDefault); if (d != null) { defaultValue = d; + + if (!deserializedRepresentation.endsWith("?")) { + deserializedRepresentation += "?"; + } } deserializedRepresentation = '$deserializedRepresentation ?? $defaultValue'; @@ -372,7 +379,7 @@ class ${pascal}Decoder extends Converter { } else if (type.element.isEnum) { deserializedRepresentation = ''' map['$alias'] is ${type.getDisplayString(withNullability: true)} - ? (map['$alias'] as ${type.getDisplayString(withNullability: true)}) + ? (map['$alias'] as ${type.getDisplayString(withNullability: true)}) ?? $defaultValue : ( map['$alias'] is int diff --git a/packages/serialize/angel_serialize_generator/pubspec.yaml b/packages/serialize/angel_serialize_generator/pubspec.yaml index 7fa5b0a7..01b23c44 100644 --- a/packages/serialize/angel_serialize_generator/pubspec.yaml +++ b/packages/serialize/angel_serialize_generator/pubspec.yaml @@ -1,14 +1,14 @@ name: angel3_serialize_generator -version: 5.0.0 +version: 6.0.0 description: Angel3 model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. homepage: https://angel3-framework.web.app/ repository: https://github.com/dukefirehawk/angel/tree/master/packages/serialize/angel_serialize_generator environment: - sdk: '>=2.14.0 <3.0.0' + sdk: '>=2.15.0 <3.0.0' dependencies: analyzer: ^3.0.0 - angel3_model: ^3.0.0 - angel3_serialize: ^4.1.0 + angel3_model: ^6.0.0 + angel3_serialize: ^6.0.0 belatuk_code_buffer: ^3.0.0 build: ^2.0.1 build_config: ^1.0.0 @@ -24,3 +24,8 @@ dev_dependencies: collection: ^1.15.0 lints: ^1.0.0 test: ^1.17.4 +dependency_overrides: + angel3_model: + path: ../../model + angel3_serialize: + path: ../angel_serialize diff --git a/packages/serialize/angel_serialize_generator/test/book_test.dart b/packages/serialize/angel_serialize_generator/test/book_test.dart index 1c12b6df..bb6977d6 100644 --- a/packages/serialize/angel_serialize_generator/test/book_test.dart +++ b/packages/serialize/angel_serialize_generator/test/book_test.dart @@ -31,30 +31,30 @@ void main() { group('serialization', () { test('serialization sets proper fields', () { - expect(serializedDeathlyHallows!['id'], deathlyHallows.id); + expect(serializedDeathlyHallows['id'], deathlyHallows.id); expect(serializedDeathlyHallows['author'], deathlyHallows.author); expect( serializedDeathlyHallows['description'], deathlyHallows.description); expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount); expect(serializedDeathlyHallows['created_at'], isNull); expect(serializedDeathlyHallows['updated_at'], - deathlyHallows.updatedAt!.toIso8601String()); + deathlyHallows.updatedAt?.toIso8601String()); }); test('can be mutated', () { var b = deathlyHallows.copyWith(); b.author = 'Hey'; expect(b.author, 'Hey'); - expect(b.toJson()![BookFields.author], 'Hey'); + expect(b.toJson()[BookFields.author], 'Hey'); }); test('heeds @Alias', () { - expect(serializedDeathlyHallows!['page_count'], deathlyHallows.pageCount); + expect(serializedDeathlyHallows['page_count'], deathlyHallows.pageCount); expect(serializedDeathlyHallows.keys, isNot(contains('pageCount'))); }); test('standard list', () { - expect(serializedDeathlyHallows!['not_models'], deathlyHallows.notModels); + expect(serializedDeathlyHallows['not_models'], deathlyHallows.notModels); }); test('heeds @exclude', () { @@ -112,7 +112,7 @@ void main() { group('deserialization', () { test('deserialization sets proper fields', () { - var book = BookSerializer.fromMap(deathlyHallowsMap!); + var book = BookSerializer.fromMap(deathlyHallowsMap); expect(book.id, deathlyHallows.id); expect(book.author, deathlyHallows.author); expect(book.description, deathlyHallows.description); diff --git a/packages/serialize/angel_serialize_generator/test/enum_test.dart b/packages/serialize/angel_serialize_generator/test/enum_test.dart index e3b2b5c3..0be89b36 100644 --- a/packages/serialize/angel_serialize_generator/test/enum_test.dart +++ b/packages/serialize/angel_serialize_generator/test/enum_test.dart @@ -3,8 +3,8 @@ import 'dart:typed_data'; import 'package:test/test.dart'; import 'models/with_enum.dart'; -const WithEnum aWithEnum = WithEnum(type: WithEnumType.a); -const WithEnum aWithEnum2 = WithEnum(type: WithEnumType.a); +WithEnum aWithEnum = WithEnum(type: WithEnumType.a); +WithEnum aWithEnum2 = WithEnum(type: WithEnumType.a); void main() { test('enum serializes to int', () { @@ -44,7 +44,11 @@ void main() { }); test('const', () { - expect(identical(aWithEnum, aWithEnum2), true); + print('aWithEnum ${aWithEnum.hashCode}'); + print('aWithEnum2 ${aWithEnum2.hashCode}'); + + //expect(identical(aWithEnum, aWithEnum2), true); + expect(aWithEnum == aWithEnum2, true); }); test('uint8list', () { diff --git a/packages/serialize/angel_serialize_generator/test/models/book.d.ts b/packages/serialize/angel_serialize_generator/test/models/book.d.ts index c0c01be5..97f19f78 100644 --- a/packages/serialize/angel_serialize_generator/test/models/book.d.ts +++ b/packages/serialize/angel_serialize_generator/test/models/book.d.ts @@ -1,5 +1,5 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -declare module 'angel_serialize_generator' { +declare module 'angel3_serialize_generator' { interface Book { id?: string; created_at?: any; diff --git a/packages/serialize/angel_serialize_generator/test/models/book.dart b/packages/serialize/angel_serialize_generator/test/models/book.dart index 45cfc2e1..db22c5b4 100644 --- a/packages/serialize/angel_serialize_generator/test/models/book.dart +++ b/packages/serialize/angel_serialize_generator/test/models/book.dart @@ -1,8 +1,6 @@ library angel_serialize.test.models.book; -import 'package:angel3_model/angel3_model.dart'; import 'package:angel3_serialize/angel3_serialize.dart'; -import 'package:collection/collection.dart'; part 'book.g.dart'; @Serializable( diff --git a/packages/serialize/angel_serialize_generator/test/models/book.g.dart b/packages/serialize/angel_serialize_generator/test/models/book.g.dart index 05845c2d..bd2f4488 100644 --- a/packages/serialize/angel_serialize_generator/test/models/book.g.dart +++ b/packages/serialize/angel_serialize_generator/test/models/book.g.dart @@ -18,7 +18,7 @@ class Book extends _Book { this.title, this.description, this.pageCount, - List? notModels, + List? notModels = const [], this.camelCaseString}) : notModels = List.unmodifiable(notModels ?? []); @@ -110,7 +110,7 @@ class Book extends _Book { return 'Book(id=$id, createdAt=$createdAt, updatedAt=$updatedAt, author=$author, title=$title, description=$description, pageCount=$pageCount, notModels=$notModels, camelCaseString=$camelCaseString)'; } - Map? toJson() { + Map toJson() { return BookSerializer.toMap(this); } } @@ -123,11 +123,11 @@ class Author extends _Author { this.updatedAt, required this.name, required this.age, - List<_Book>? books, + List<_Book> books = const [], this.newestBook, this.secret, this.obscured}) - : books = List.unmodifiable(books ?? []); + : books = List.unmodifiable(books); /// A unique identifier corresponding to this item. @override @@ -142,23 +142,23 @@ class Author extends _Author { DateTime? updatedAt; @override - final String? name; + String? name; @override - final int? age; + int? age; @override - final List<_Book> books; + List<_Book> books; /// The newest book. @override - final _Book? newestBook; + _Book? newestBook; @override - final String? secret; + String? secret; @override - final String? obscured; + String? obscured; Author copyWith( {String? id, @@ -225,8 +225,11 @@ class Author extends _Author { @generatedSerializable class Library extends _Library { Library( - {this.id, this.createdAt, this.updatedAt, Map? collection}) - : collection = Map.unmodifiable(collection ?? {}); + {this.id, + this.createdAt, + this.updatedAt, + required Map collection}) + : collection = Map.unmodifiable(collection); /// A unique identifier corresponding to this item. @override @@ -241,7 +244,7 @@ class Library extends _Library { DateTime? updatedAt; @override - final Map collection; + Map collection; Library copyWith( {String? id, @@ -288,10 +291,10 @@ class Bookmark extends _Bookmark { {this.id, this.createdAt, this.updatedAt, - List? history, + List history = const [], required this.page, this.comment}) - : history = List.unmodifiable(history ?? []), + : history = List.unmodifiable(history), super(book); /// A unique identifier corresponding to this item. @@ -307,13 +310,13 @@ class Bookmark extends _Bookmark { DateTime? updatedAt; @override - final List history; + List history; @override - final int? page; + int? page; @override - final String? comment; + String? comment; Bookmark copyWith(_Book book, {String? id, @@ -364,11 +367,11 @@ class Bookmark extends _Bookmark { const BookSerializer bookSerializer = BookSerializer(); -class BookEncoder extends Converter { +class BookEncoder extends Converter { const BookEncoder(); @override - Map? convert(Book model) => BookSerializer.toMap(model); + Map convert(Book model) => BookSerializer.toMap(model); } class BookDecoder extends Converter { @@ -378,26 +381,24 @@ class BookDecoder extends Converter { Book convert(Map map) => BookSerializer.fromMap(map); } -class BookSerializer extends Codec { +class BookSerializer extends Codec { const BookSerializer(); @override BookEncoder get encoder => const BookEncoder(); - @override BookDecoder get decoder => const BookDecoder(); - static Book fromMap(Map map) { return Book( id: map['id'] as String?, createdAt: map['created_at'] != null ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime?) + ? (map['created_at'] as DateTime) : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime?) + ? (map['updated_at'] as DateTime) : DateTime.parse(map['updated_at'].toString())) : null, author: map['author'] as String?, @@ -406,13 +407,13 @@ class BookSerializer extends Codec { pageCount: map['page_count'] as int?, notModels: map['not_models'] is Iterable ? (map['not_models'] as Iterable).cast().toList() - : null, + : [], camelCaseString: map['camelCase'] as String?); } - static Map? toMap(_Book? model) { + static Map toMap(_Book? model) { if (model == null) { - return null; + throw FormatException("Required field [model] cannot be null"); } return { 'id': model.id, @@ -481,10 +482,8 @@ class AuthorSerializer extends Codec { @override AuthorEncoder get encoder => const AuthorEncoder(); - @override AuthorDecoder get decoder => const AuthorDecoder(); - static Author fromMap(Map map) { if (map['name'] == null) { throw FormatException("Missing required field 'name' on Author."); @@ -498,12 +497,12 @@ class AuthorSerializer extends Codec { id: map['id'] as String?, createdAt: map['created_at'] != null ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime?) + ? (map['created_at'] as DateTime) : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime?) + ? (map['updated_at'] as DateTime) : DateTime.parse(map['updated_at'].toString())) : null, name: map['name'] as String?, @@ -511,22 +510,17 @@ class AuthorSerializer extends Codec { books: map['books'] is Iterable ? List.unmodifiable(((map['books'] as Iterable).whereType()) .map(BookSerializer.fromMap)) - : null, + : [], newestBook: map['newest_book'] != null ? BookSerializer.fromMap(map['newest_book'] as Map) : null, obscured: map['obscured'] as String?); } - static Map toMap(_Author model) { - if (model.name == null) { - throw FormatException("Missing required field 'name' on Author."); + static Map toMap(_Author? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); } - - if (model.age == null) { - throw FormatException('Custom message for missing `age`'); - } - return { 'id': model.id, 'created_at': model.createdAt?.toIso8601String(), @@ -592,21 +586,19 @@ class LibrarySerializer extends Codec { @override LibraryEncoder get encoder => const LibraryEncoder(); - @override LibraryDecoder get decoder => const LibraryDecoder(); - static Library fromMap(Map map) { return Library( id: map['id'] as String?, createdAt: map['created_at'] != null ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime?) + ? (map['created_at'] as DateTime) : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime?) + ? (map['updated_at'] as DateTime) : DateTime.parse(map['updated_at'].toString())) : null, collection: map['collection'] is Map @@ -616,16 +608,20 @@ class LibrarySerializer extends Codec { ..[key] = BookSerializer.fromMap( ((map['collection'] as Map)[key]) as Map); })) - : null); + : {}); } - static Map toMap(_Library model) { + static Map toMap(_Library? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); + } return { 'id': model.id, 'created_at': model.createdAt?.toIso8601String(), 'updated_at': model.updatedAt?.toIso8601String(), - 'collection': model.collection.keys.fold({}, (dynamic map, key) { - return map..[key] = BookSerializer.toMap(model.collection[key]); + 'collection': model.collection.keys.fold({}, (map, key) { + return (map as Map?) + ?..[key] = BookSerializer.toMap(model.collection[key]); }) }; } @@ -658,26 +654,25 @@ abstract class BookmarkSerializer { id: map['id'] as String?, createdAt: map['created_at'] != null ? (map['created_at'] is DateTime - ? (map['created_at'] as DateTime?) + ? (map['created_at'] as DateTime) : DateTime.parse(map['created_at'].toString())) : null, updatedAt: map['updated_at'] != null ? (map['updated_at'] is DateTime - ? (map['updated_at'] as DateTime?) + ? (map['updated_at'] as DateTime) : DateTime.parse(map['updated_at'].toString())) : null, history: map['history'] is Iterable ? (map['history'] as Iterable).cast().toList() - : null, + : [], page: map['page'] as int?, comment: map['comment'] as String?); } - static Map toMap(_Bookmark model) { - if (model.page == null) { - throw FormatException("Missing required field 'page' on Bookmark."); + static Map toMap(_Bookmark? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); } - return { 'id': model.id, 'created_at': model.createdAt?.toIso8601String(), diff --git a/packages/serialize/angel_serialize_generator/test/models/game_pad_button.g.dart b/packages/serialize/angel_serialize_generator/test/models/game_pad_button.g.dart index 2559c257..c0daeb2e 100644 --- a/packages/serialize/angel_serialize_generator/test/models/game_pad_button.g.dart +++ b/packages/serialize/angel_serialize_generator/test/models/game_pad_button.g.dart @@ -8,13 +8,13 @@ part of 'game_pad_button.dart'; @generatedSerializable class GamepadButton implements _GamepadButton { - const GamepadButton({this.name, this.radius}); + GamepadButton({this.name, this.radius}); @override - final String? name; + String? name; @override - final int? radius; + int? radius; GamepadButton copyWith({String? name, int? radius}) { return GamepadButton( @@ -45,7 +45,9 @@ class GamepadButton implements _GamepadButton { @generatedSerializable class Gamepad extends _Gamepad { - Gamepad({List<_GamepadButton>? buttons, Map? dynamicMap}) + Gamepad( + {List<_GamepadButton>? buttons = const [], + Map? dynamicMap}) : buttons = List.unmodifiable(buttons ?? []), dynamicMap = Map.unmodifiable(dynamicMap ?? {}); @@ -120,7 +122,10 @@ class GamepadButtonSerializer extends Codec { name: map['name'] as String?, radius: map['radius'] as int?); } - static Map toMap(_GamepadButton model) { + static Map toMap(_GamepadButton? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); + } return {'name': model.name, 'radius': model.radius}; } } @@ -161,13 +166,16 @@ class GamepadSerializer extends Codec { buttons: map['buttons'] is Iterable ? List.unmodifiable(((map['buttons'] as Iterable).whereType()) .map(GamepadButtonSerializer.fromMap)) - : null, + : [], dynamicMap: map['dynamic_map'] is Map ? (map['dynamic_map'] as Map).cast() - : null); + : {}); } - static Map toMap(_Gamepad model) { + static Map toMap(_Gamepad? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); + } return { 'buttons': model.buttons?.map((m) => GamepadButtonSerializer.toMap(m)).toList(), diff --git a/packages/serialize/angel_serialize_generator/test/models/goat.dart b/packages/serialize/angel_serialize_generator/test/models/goat.dart index b35ed160..79152280 100644 --- a/packages/serialize/angel_serialize_generator/test/models/goat.dart +++ b/packages/serialize/angel_serialize_generator/test/models/goat.dart @@ -1,5 +1,4 @@ import 'package:angel3_serialize/angel3_serialize.dart'; -import 'package:collection/collection.dart'; part 'goat.g.dart'; @serializable diff --git a/packages/serialize/angel_serialize_generator/test/models/goat.g.dart b/packages/serialize/angel_serialize_generator/test/models/goat.g.dart index ead0a156..4c8c5fb7 100644 --- a/packages/serialize/angel_serialize_generator/test/models/goat.g.dart +++ b/packages/serialize/angel_serialize_generator/test/models/goat.g.dart @@ -8,13 +8,13 @@ part of 'goat.dart'; @generatedSerializable class Goat implements _Goat { - const Goat({this.integer = 34, this.list = const [34, 35]}); + Goat({this.integer = 34, this.list = const [34, 35]}); @override - final int integer; + int integer; @override - final List list; + List list; Goat copyWith({int? integer, List? list}) { return Goat(integer: integer ?? this.integer, list: list ?? this.list); @@ -77,7 +77,10 @@ class GoatSerializer extends Codec { : const [34, 35]); } - static Map toMap(_Goat model) { + static Map toMap(_Goat? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); + } return {'integer': model.integer, 'list': model.list}; } } diff --git a/packages/serialize/angel_serialize_generator/test/models/has_map.dart b/packages/serialize/angel_serialize_generator/test/models/has_map.dart index beef7c61..b335c187 100644 --- a/packages/serialize/angel_serialize_generator/test/models/has_map.dart +++ b/packages/serialize/angel_serialize_generator/test/models/has_map.dart @@ -1,7 +1,4 @@ -import 'dart:convert'; import 'package:angel3_serialize/angel3_serialize.dart'; -import 'package:collection/collection.dart'; -//import 'package:meta/meta.dart'; part 'has_map.g.dart'; Map? _fromString(v) => json.decode(v.toString()) as Map?; diff --git a/packages/serialize/angel_serialize_generator/test/models/has_map.g.dart b/packages/serialize/angel_serialize_generator/test/models/has_map.g.dart index 25416576..8f9482d1 100644 --- a/packages/serialize/angel_serialize_generator/test/models/has_map.g.dart +++ b/packages/serialize/angel_serialize_generator/test/models/has_map.g.dart @@ -8,10 +8,10 @@ part of 'has_map.dart'; @generatedSerializable class HasMap implements _HasMap { - const HasMap({required this.value}); + HasMap({required this.value}); @override - final Map? value; + Map? value; HasMap copyWith({Map? value}) { return HasMap(value: value ?? this.value); @@ -75,11 +75,10 @@ class HasMapSerializer extends Codec { return HasMap(value: _fromString(map['value'])); } - static Map toMap(_HasMap model) { - if (model.value == null) { - throw FormatException("Missing required field 'value' on HasMap."); + static Map toMap(_HasMap? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); } - return {'value': _toString(model.value)}; } } diff --git a/packages/serialize/angel_serialize_generator/test/models/subclass.g.dart b/packages/serialize/angel_serialize_generator/test/models/subclass.g.dart index 611401f1..d6612fb5 100644 --- a/packages/serialize/angel_serialize_generator/test/models/subclass.g.dart +++ b/packages/serialize/angel_serialize_generator/test/models/subclass.g.dart @@ -123,15 +123,10 @@ class AnimalSerializer extends Codec { genus: map['genus'] as String?, species: map['species'] as String?); } - static Map toMap(_Animal model) { - if (model.genus == null) { - throw FormatException("Missing required field 'genus' on Animal."); + static Map toMap(_Animal? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); } - - if (model.species == null) { - throw FormatException("Missing required field 'species' on Animal."); - } - return {'genus': model.genus, 'species': model.species}; } } @@ -182,15 +177,10 @@ class BirdSerializer extends Codec { isSparrow: map['is_sparrow'] as bool? ?? false); } - static Map toMap(_Bird model) { - if (model.genus == null) { - throw FormatException("Missing required field 'genus' on Bird."); + static Map toMap(_Bird? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); } - - if (model.species == null) { - throw FormatException("Missing required field 'species' on Bird."); - } - return { 'genus': model.genus, 'species': model.species, diff --git a/packages/serialize/angel_serialize_generator/test/models/with_enum.dart b/packages/serialize/angel_serialize_generator/test/models/with_enum.dart index 0ebd8f7c..eb92923a 100644 --- a/packages/serialize/angel_serialize_generator/test/models/with_enum.dart +++ b/packages/serialize/angel_serialize_generator/test/models/with_enum.dart @@ -1,7 +1,6 @@ import 'dart:convert'; import 'dart:typed_data'; import 'package:angel3_serialize/angel3_serialize.dart'; -import 'package:collection/collection.dart'; part 'with_enum.g.dart'; @serializable diff --git a/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart b/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart index f2446c6c..f85ff650 100644 --- a/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart +++ b/packages/serialize/angel_serialize_generator/test/models/with_enum.g.dart @@ -8,16 +8,17 @@ part of 'with_enum.dart'; @generatedSerializable class WithEnum implements _WithEnum { - const WithEnum({this.type = WithEnumType.b, this.finalList, this.imageBytes}); + WithEnum( + {this.type = WithEnumType.b, this.finalList = const [], this.imageBytes}); @override - final WithEnumType? type; + WithEnumType? type; @override - final List? finalList; + List? finalList; @override - final Uint8List? imageBytes; + Uint8List? imageBytes; WithEnum copyWith( {WithEnumType? type, List? finalList, Uint8List? imageBytes}) { @@ -80,16 +81,16 @@ class WithEnumSerializer extends Codec { WithEnumDecoder get decoder => const WithEnumDecoder(); static WithEnum fromMap(Map map) { return WithEnum( - type: map['type'] is WithEnumType - ? (map['type'] as WithEnumType?) + type: map['type'] is WithEnumType? + ? (map['type'] as WithEnumType?) ?? WithEnumType.b : (map['type'] is int - ? WithEnumType.values[map['type'] as int] + ? WithEnumType?.values[map['type'] as int] : WithEnumType.b), finalList: map['final_list'] is Iterable ? (map['final_list'] as Iterable).cast().toList() - : null, + : [], imageBytes: map['image_bytes'] is Uint8List - ? (map['image_bytes'] as Uint8List?) + ? (map['image_bytes'] as Uint8List) : (map['image_bytes'] is Iterable ? Uint8List.fromList( (map['image_bytes'] as Iterable).toList()) @@ -99,13 +100,16 @@ class WithEnumSerializer extends Codec { : null))); } - static Map toMap(_WithEnum model) { + static Map toMap(_WithEnum? model) { + if (model == null) { + throw FormatException("Required field [model] cannot be null"); + } return { 'type': - model.type == null ? null : WithEnumType.values.indexOf(model.type!), + model.type != null ? WithEnumType.values.indexOf(model.type!) : null, 'final_list': model.finalList, 'image_bytes': - model.imageBytes == null ? null : base64.encode(model.imageBytes!) + model.imageBytes != null ? base64.encode(model.imageBytes!) : null }; } }