diff --git a/packages/serialize/angel_serialize_generator/README.md b/packages/serialize/angel_serialize_generator/README.md index 1b53201e..42964df9 100644 --- a/packages/serialize/angel_serialize_generator/README.md +++ b/packages/serialize/angel_serialize_generator/README.md @@ -1,4 +1,11 @@ -# angel_serialize_generator +# angel3_serialize_generator +[![version](https://img.shields.io/badge/pub-v4.0.0-brightgreen)](https://pub.dartlang.org/packages/angel3_serialize_generator) +[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety) +[![Gitter](https://img.shields.io/gitter/room/angel_dart/discussion)](https://gitter.im/angel_dart/discussion) + +[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator/LICENSE) + + The builder for Angel's model serialization. Find documentation in the main project repo: diff --git a/packages/serialize/angel_serialize_generator/example/main.dart b/packages/serialize/angel_serialize_generator/example/main.dart index ad8e3ce0..a649a351 100644 --- a/packages/serialize/angel_serialize_generator/example/main.dart +++ b/packages/serialize/angel_serialize_generator/example/main.dart @@ -1,5 +1,5 @@ // ignore_for_file: unused_element -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; part 'main.g.dart'; @serializable diff --git a/packages/serialize/angel_serialize_generator/example/main.g.dart b/packages/serialize/angel_serialize_generator/example/main.g.dart index 69d0af4e..e5acf8ad 100644 --- a/packages/serialize/angel_serialize_generator/example/main.g.dart +++ b/packages/serialize/angel_serialize_generator/example/main.g.dart @@ -21,6 +21,7 @@ class Todo extends _Todo { text: text ?? this.text, completed: completed ?? this.completed); } + @override bool operator ==(other) { return other is _Todo && other.text == text && other.completed == completed; } @@ -32,7 +33,7 @@ class Todo extends _Todo { @override String toString() { - return "Todo(text=$text, completed=$completed)"; + return 'Todo(text=$text, completed=$completed)'; } Map toJson() { @@ -64,9 +65,11 @@ class TodoSerializer extends Codec { const TodoSerializer(); @override - get encoder => const TodoEncoder(); + TodoEncoder get encoder => const TodoEncoder(); + @override - get decoder => const TodoDecoder(); + TodoDecoder get decoder => const TodoDecoder(); + static Todo fromMap(Map map) { return Todo( text: map['text'] as String?, completed: map['completed'] as bool?); diff --git a/packages/serialize/angel_serialize_generator/lib/angel_serialize_generator.dart b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart similarity index 96% rename from packages/serialize/angel_serialize_generator/lib/angel_serialize_generator.dart rename to packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart index 1cf029f6..65b1547a 100644 --- a/packages/serialize/angel_serialize_generator/lib/angel_serialize_generator.dart +++ b/packages/serialize/angel_serialize_generator/lib/angel3_serialize_generator.dart @@ -1,4 +1,4 @@ -library angel_serialize_generator; +library angel3_serialize_generator; import 'dart:async'; import 'dart:mirrors'; @@ -6,10 +6,10 @@ import 'dart:typed_data'; import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; -import 'package:angel_model/angel_model.dart'; -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_model/angel3_model.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:build/build.dart'; -import 'package:code_buffer/code_buffer.dart'; +import 'package:angel3_code_buffer/angel3_code_buffer.dart'; import 'package:code_builder/code_builder.dart'; import 'package:path/path.dart' as p; import 'package:recase/recase.dart'; diff --git a/packages/serialize/angel_serialize_generator/lib/build_context.dart b/packages/serialize/angel_serialize_generator/lib/build_context.dart index 2e8561cd..289397e2 100644 --- a/packages/serialize/angel_serialize_generator/lib/build_context.dart +++ b/packages/serialize/angel_serialize_generator/lib/build_context.dart @@ -3,7 +3,7 @@ import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/src/dart/element/element.dart'; -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:build/build.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; diff --git a/packages/serialize/angel_serialize_generator/lib/context.dart b/packages/serialize/angel_serialize_generator/lib/context.dart index 66cef80c..0e25a564 100644 --- a/packages/serialize/angel_serialize_generator/lib/context.dart +++ b/packages/serialize/angel_serialize_generator/lib/context.dart @@ -1,7 +1,7 @@ import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:code_builder/code_builder.dart'; import 'package:recase/recase.dart'; import 'package:source_gen/source_gen.dart'; diff --git a/packages/serialize/angel_serialize_generator/lib/model.dart b/packages/serialize/angel_serialize_generator/lib/model.dart index 90a0be03..d2218bc6 100644 --- a/packages/serialize/angel_serialize_generator/lib/model.dart +++ b/packages/serialize/angel_serialize_generator/lib/model.dart @@ -1,4 +1,4 @@ -part of angel_serialize_generator; +part of angel3_serialize_generator; class JsonModelGenerator extends GeneratorForAnnotation { const JsonModelGenerator(); diff --git a/packages/serialize/angel_serialize_generator/lib/serialize.dart b/packages/serialize/angel_serialize_generator/lib/serialize.dart index 759acde6..35d78690 100644 --- a/packages/serialize/angel_serialize_generator/lib/serialize.dart +++ b/packages/serialize/angel_serialize_generator/lib/serialize.dart @@ -1,4 +1,4 @@ -part of angel_serialize_generator; +part of angel3_serialize_generator; class SerializerGenerator extends GeneratorForAnnotation { final bool autoSnakeCaseNames; @@ -13,7 +13,7 @@ class SerializerGenerator extends GeneratorForAnnotation { } var ctx = await buildContext(element as ClassElement, annotation, buildStep, - await buildStep.resolver, autoSnakeCaseNames != false); + buildStep.resolver, autoSnakeCaseNames != false); var serializers = annotation.peek('serializers')?.listValue ?? []; @@ -44,29 +44,28 @@ class SerializerGenerator extends GeneratorForAnnotation { file.body.add(Code(''' const ${pascal}Serializer ${camel}Serializer = ${pascal}Serializer(); -class ${pascal}Encoder extends Converter<${pascal}, Map> { +class ${pascal}Encoder extends Converter<$pascal, Map> { const ${pascal}Encoder(); @override - Map convert(${pascal} model) => ${pascal}Serializer.toMap(model); + Map convert($pascal model) => ${pascal}Serializer.toMap(model); } -class ${pascal}Decoder extends Converter { +class ${pascal}Decoder extends Converter { const ${pascal}Decoder(); @override - ${pascal} convert(Map map) => ${pascal}Serializer.fromMap(map); + $pascal convert(Map map) => ${pascal}Serializer.fromMap(map); } ''')); } file.body.add(Class((clazz) { - clazz..name = '${pascal}Serializer'; + clazz.name = '${pascal}Serializer'; if (ctx.constructorParameters.isEmpty) { - clazz - ..extend = TypeReference((b) => b - ..symbol = 'Codec' - ..types.addAll([ctx.modelClassType, refer('Map')])); + clazz.extend = TypeReference((b) => b + ..symbol = 'Codec' + ..types.addAll([ctx.modelClassType, refer('Map')])); // Add constructor, Codec impl, etc. clazz.constructors.add(Constructor((b) => b..constant = true)); @@ -120,7 +119,7 @@ class ${pascal}Decoder extends Converter { }); buf.writeln('return {'); - int i = 0; + var i = 0; // Add named parameters for (var field in ctx.fields) { @@ -133,7 +132,7 @@ class ${pascal}Decoder extends Converter { if (i++ > 0) buf.write(', '); - String serializedRepresentation = 'model.${field.name}'; + var serializedRepresentation = 'model.${field.name}'; String serializerToMap(ReCase rc, String value) { // if (rc.pascalCase == ctx.modelClassName) { @@ -143,7 +142,8 @@ class ${pascal}Decoder extends Converter { } if (ctx.fieldInfo[field.name]?.serializer != null) { - var name = MirrorSystem.getName(ctx.fieldInfo[field.name]!.serializer!); + var name = + MirrorSystem.getName(ctx.fieldInfo[field.name]!.serializer!); serializedRepresentation = '$name(model.${field.name})'; } @@ -236,7 +236,7 @@ class ${pascal}Decoder extends Converter { }); buf.writeln('return ${ctx.modelClassName}('); - int i = 0; + var i = 0; for (var param in ctx.constructorParameters) { if (i++ > 0) buf.write(', '); @@ -252,7 +252,7 @@ class ${pascal}Decoder extends Converter { if (i++ > 0) buf.write(', '); - String deserializedRepresentation = + var deserializedRepresentation = "map['$alias'] as ${typeToString(type)}"; String? defaultValue = 'null'; @@ -271,7 +271,7 @@ class ${pascal}Decoder extends Converter { } else if (dateTimeTypeChecker.isAssignableFromType(type)) { deserializedRepresentation = "map['$alias'] != null ? " "(map['$alias'] is DateTime ? (map['$alias'] as DateTime) : DateTime.parse(map['$alias'].toString()))" - " : $defaultValue"; + ' : $defaultValue'; } // Serialize model classes via `XSerializer.toMap` @@ -279,15 +279,15 @@ class ${pascal}Decoder extends Converter { var rc = ReCase(type.name!); deserializedRepresentation = "map['$alias'] != null" " ? ${rc.pascalCase}Serializer.fromMap(map['$alias'] as Map)" - " : $defaultValue"; + ' : $defaultValue'; } else if (type is InterfaceType) { if (isListOfModelType(type)) { var rc = ReCase(type.typeArguments[0].name!); deserializedRepresentation = "map['$alias'] is Iterable" " ? List.unmodifiable(((map['$alias'] as Iterable)" - ".whereType())" - ".map(${rc.pascalCase}Serializer.fromMap))" - " : $defaultValue"; + '.whereType())' + '.map(${rc.pascalCase}Serializer.fromMap))' + ' : $defaultValue'; } else if (isMapToModelType(type)) { var rc = ReCase(type.typeArguments[1].name!); deserializedRepresentation = ''' diff --git a/packages/serialize/angel_serialize_generator/lib/typescript.dart b/packages/serialize/angel_serialize_generator/lib/typescript.dart index 3ac0a4d0..bbc75501 100644 --- a/packages/serialize/angel_serialize_generator/lib/typescript.dart +++ b/packages/serialize/angel_serialize_generator/lib/typescript.dart @@ -1,4 +1,4 @@ -part of angel_serialize_generator; +part of angel3_serialize_generator; class TypeScriptDefinitionBuilder implements Builder { final bool autoSnakeCaseNames; diff --git a/packages/serialize/angel_serialize_generator/pubspec.yaml b/packages/serialize/angel_serialize_generator/pubspec.yaml index 44858581..0eb43e23 100644 --- a/packages/serialize/angel_serialize_generator/pubspec.yaml +++ b/packages/serialize/angel_serialize_generator/pubspec.yaml @@ -1,27 +1,14 @@ -name: angel_serialize_generator +name: angel3_serialize_generator version: 4.0.0 description: Model serialization generators, designed for use with Angel. Combine with angel_serialize for flexible modeling. -homepage: https://github.com/dukefirehawk/angel -publish_to: none +homepage: https://github.com/dukefirehawk/angel/tree/angel3/packages/serialize/angel_serialize_generator environment: sdk: '>=2.12.0 <3.0.0' dependencies: analyzer: ^1.5.0 - angel_model: - git: - url: https://github.com/dukefirehawk/angel.git - ref: sdk-2.12.x_nnbd - path: packages/model - angel_serialize: - git: - url: https://github.com/dukefirehawk/angel.git - ref: sdk-2.12.x_nnbd - path: packages/serialize/angel_serialize - code_buffer: - git: - url: https://github.com/dukefirehawk/angel.git - ref: sdk-2.12.x_nnbd - path: packages/code_buffer + angel3_model: ^3.0.0 + angel3_serialize: ^4.0.0 + angel3_code_buffer: ^2.0.0 build: ^2.0.1 build_config: ^1.0.0 code_builder: ^4.0.0 @@ -34,7 +21,4 @@ dev_dependencies: build_runner: ^2.0.1 collection: ^1.15.0 pedantic: ^1.11.0 - test: ^1.17.3 -# dependency_overrides: -# angel_serialize: -# path: ../angel_serialize \ No newline at end of file + test: ^1.17.4 diff --git a/packages/serialize/angel_serialize_generator/test/book_test.dart b/packages/serialize/angel_serialize_generator/test/book_test.dart index a02605a8..1c12b6df 100644 --- a/packages/serialize/angel_serialize_generator/test/book_test.dart +++ b/packages/serialize/angel_serialize_generator/test/book_test.dart @@ -3,7 +3,7 @@ import 'models/book.dart'; const String deathlyHallowsIsbn = '0-545-01022-5'; -main() { +void main() { var deathlyHallows = Book( id: '0', author: 'J.K. Rowling', @@ -21,8 +21,8 @@ main() { age: 51, books: [deathlyHallows], newestBook: deathlyHallows); - Map serializedJkRowling = authorSerializer.encode(jkRowling); - Map? deathlyHallowsMap = bookSerializer.encode(deathlyHallows); + var serializedJkRowling = authorSerializer.encode(jkRowling); + var deathlyHallowsMap = bookSerializer.encode(deathlyHallows); print('J.K. Rowling: $jkRowling'); var library = Library(collection: {deathlyHallowsIsbn: deathlyHallows}); @@ -63,7 +63,7 @@ main() { test('heeds canDeserialize', () { var map = Map.from(serializedJkRowling)..['obscured'] = 'foo'; - Author author = authorSerializer.decode(map); + var author = authorSerializer.decode(map); expect(author.obscured, 'foo'); }); diff --git a/packages/serialize/angel_serialize_generator/test/models/book.dart b/packages/serialize/angel_serialize_generator/test/models/book.dart index f19f9598..271eb9f1 100644 --- a/packages/serialize/angel_serialize_generator/test/models/book.dart +++ b/packages/serialize/angel_serialize_generator/test/models/book.dart @@ -1,7 +1,7 @@ library angel_serialize.test.models.book; -import 'package:angel_model/angel_model.dart'; -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_model/angel3_model.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:collection/collection.dart'; import 'package:meta/meta.dart'; part 'book.g.dart'; diff --git a/packages/serialize/angel_serialize_generator/test/models/game_pad_button.dart b/packages/serialize/angel_serialize_generator/test/models/game_pad_button.dart index 4aff81a2..c8f010d0 100644 --- a/packages/serialize/angel_serialize_generator/test/models/game_pad_button.dart +++ b/packages/serialize/angel_serialize_generator/test/models/game_pad_button.dart @@ -1,4 +1,4 @@ -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; part 'game_pad_button.g.dart'; @serializable diff --git a/packages/serialize/angel_serialize_generator/test/models/goat.dart b/packages/serialize/angel_serialize_generator/test/models/goat.dart index e1bb8bd7..b35ed160 100644 --- a/packages/serialize/angel_serialize_generator/test/models/goat.dart +++ b/packages/serialize/angel_serialize_generator/test/models/goat.dart @@ -1,4 +1,4 @@ -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:collection/collection.dart'; part 'goat.g.dart'; 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 520ecc83..beef7c61 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,7 @@ import 'dart:convert'; -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:collection/collection.dart'; -import 'package:meta/meta.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/subclass.dart b/packages/serialize/angel_serialize_generator/test/models/subclass.dart index 62b59b00..b1d4e8e0 100644 --- a/packages/serialize/angel_serialize_generator/test/models/subclass.dart +++ b/packages/serialize/angel_serialize_generator/test/models/subclass.dart @@ -1,4 +1,4 @@ -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; part 'subclass.g.dart'; @serializable 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 182e33b5..0ebd8f7c 100644 --- a/packages/serialize/angel_serialize_generator/test/models/with_enum.dart +++ b/packages/serialize/angel_serialize_generator/test/models/with_enum.dart @@ -1,6 +1,6 @@ import 'dart:convert'; import 'dart:typed_data'; -import 'package:angel_serialize/angel_serialize.dart'; +import 'package:angel3_serialize/angel3_serialize.dart'; import 'package:collection/collection.dart'; part 'with_enum.g.dart';