diff --git a/.gitignore b/.gitignore index 07086897..009a14b1 100644 --- a/.gitignore +++ b/.gitignore @@ -74,4 +74,5 @@ logs/ *.pem .DS_Store server_log.txt -ormapp/ \ No newline at end of file +ormapp/ +backup/ \ No newline at end of file diff --git a/packages/serialize/.idea/modules.xml b/packages/serialize/.idea/modules.xml deleted file mode 100644 index 08fe6948..00000000 --- a/packages/serialize/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/packages/serialize/.idea/runConfigurations/tests_in_angel_serialize_generator.xml b/packages/serialize/.idea/runConfigurations/tests_in_angel_serialize_generator.xml deleted file mode 100644 index e61b9017..00000000 --- a/packages/serialize/.idea/runConfigurations/tests_in_angel_serialize_generator.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - \ No newline at end of file diff --git a/packages/serialize/.idea/runConfigurations/tests_in_enum_test_dart.xml b/packages/serialize/.idea/runConfigurations/tests_in_enum_test_dart.xml deleted file mode 100644 index 9a4a3357..00000000 --- a/packages/serialize/.idea/runConfigurations/tests_in_enum_test_dart.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/packages/serialize/.idea/serialize.iml b/packages/serialize/.idea/serialize.iml deleted file mode 100644 index 67079d9b..00000000 --- a/packages/serialize/.idea/serialize.iml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/packages/serialize/.idea/vcs.xml b/packages/serialize/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/packages/serialize/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/packages/serialize/angel_serialize_generator/CHANGELOG.md b/packages/serialize/angel_serialize_generator/CHANGELOG.md index 73dfdd77..a13163e2 100644 --- a/packages/serialize/angel_serialize_generator/CHANGELOG.md +++ b/packages/serialize/angel_serialize_generator/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 4.1.2 + +* Fixed `toMap` method generator + ## 4.1.1 * Fixed `SerializerGenerator` to recognize nullable class diff --git a/packages/serialize/angel_serialize_generator/README.md b/packages/serialize/angel_serialize_generator/README.md index 18ad154e..d3987cc4 100644 --- a/packages/serialize/angel_serialize_generator/README.md +++ b/packages/serialize/angel_serialize_generator/README.md @@ -1,6 +1,6 @@ # Angel3 Serialize Generator -[![version](https://img.shields.io/badge/pub-v4.1.1-brightgreen)](https://pub.dartlang.org/packages/angel3_serialize_generator) +[![version](https://img.shields.io/badge/pub-v4.1.2-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) diff --git a/packages/serialize/angel_serialize_generator/lib/model.dart b/packages/serialize/angel_serialize_generator/lib/model.dart index a9995edf..22c245ee 100644 --- a/packages/serialize/angel_serialize_generator/lib/model.dart +++ b/packages/serialize/angel_serialize_generator/lib/model.dart @@ -30,7 +30,7 @@ class JsonModelGenerator extends GeneratorForAnnotation { void generateClass( BuildContext ctx, LibraryBuilder file, ConstantReader annotation) { file.body.add(Class((clazz) { - log.fine('Generate Class: ${ctx.modelClassNameRecase.pascalCase}'); + //log.fine('Generate Class: ${ctx.modelClassNameRecase.pascalCase}'); clazz ..name = ctx.modelClassNameRecase.pascalCase ..annotations.add(refer('generatedSerializable')); @@ -50,7 +50,8 @@ class JsonModelGenerator extends GeneratorForAnnotation { // Generate the fields for the class for (var field in ctx.fields) { - log.fine('Generate Field: ${field.name}'); + //log.fine('Generate Field: ${field.name}'); + clazz.fields.add(Field((b) { b ..name = field.name @@ -106,11 +107,11 @@ class JsonModelGenerator extends GeneratorForAnnotation { BuildContext ctx, ClassBuilder clazz, LibraryBuilder file) { clazz.constructors.add(Constructor((constructor) { // Add all `super` params - constructor.constant = (ctx.clazz.unnamedConstructor?.isConst == true || - shouldBeConstant(ctx)) && - ctx.fields.every((f) { - return f.setter == null && f is! ShimFieldImpl; - }); + //constructor.constant = (ctx.clazz.unnamedConstructor?.isConst == true || + // shouldBeConstant(ctx)) && + // ctx.fields.every((f) { + // return f.setter == null && f is! ShimFieldImpl; + // }); for (var param in ctx.constructorParameters) { //log.fine('Contructor Parameter: ${param.name}'); diff --git a/packages/serialize/angel_serialize_generator/lib/serialize.dart b/packages/serialize/angel_serialize_generator/lib/serialize.dart index f92762b6..ed610efa 100644 --- a/packages/serialize/angel_serialize_generator/lib/serialize.dart +++ b/packages/serialize/angel_serialize_generator/lib/serialize.dart @@ -8,7 +8,7 @@ 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.'; @@ -39,20 +39,20 @@ class SerializerGenerator extends GeneratorForAnnotation { generateFieldsClass(ctx, b); }); - var buf = lib.accept(DartEmitter()); + var buf = lib.accept(DartEmitter(useNullSafetySyntax: true)); return buf.toString(); } /// Generate a serializer class. void generateClass( List serializers, BuildContext ctx, LibraryBuilder file) { - log.fine('Generate serailizer class'); + //log.fine('Generate serializer class'); // Generate canonical codecs, etc. var pascal = ctx.modelClassNameRecase.pascalCase.replaceAll('?', ''); var camel = ctx.modelClassNameRecase.camelCase.replaceAll('?', ''); - log.fine('Pascal = $pascal, camel = $camel'); + //log.fine('Pascal = $pascal, camel = $camel'); if (ctx.constructorParameters.isEmpty) { file.body.add(Code(''' @@ -112,6 +112,11 @@ class ${pascal}Decoder extends Converter { void generateToMapMethod( ClassBuilder clazz, BuildContext ctx, LibraryBuilder file) { + var originalClassName = ctx.originalClassName; + if (originalClassName == null) { + log.warning('Unable to generate toMap(), classname is null'); + return; + } clazz.methods.add(Method((method) { method ..static = true @@ -120,7 +125,9 @@ class ${pascal}Decoder extends Converter { ..requiredParameters.add(Parameter((b) { b ..name = 'model' - ..type = refer(ctx.originalClassName!); + ..type = TypeReference((b) => b + ..symbol = '$originalClassName' + ..isNullable = true); })); var buf = StringBuffer(); @@ -159,9 +166,9 @@ class ${pascal}Decoder extends Converter { return '${rc.pascalCase.replaceAll('?', '')}Serializer.toMap($value)'; } - if (ctx.fieldInfo[field.name]?.serializer != null) { - var name = - MirrorSystem.getName(ctx.fieldInfo[field.name]!.serializer!); + var fieldNameSerializer = ctx.fieldInfo[field.name]?.serializer; + if (fieldNameSerializer != null) { + var name = MirrorSystem.getName(fieldNameSerializer); serializedRepresentation = '$name(model.${field.name})'; } @@ -184,8 +191,7 @@ class ${pascal}Decoder extends Converter { var m = serializerToMap(rc, 'm'); serializedRepresentation = ''' model.${field.name} - ?.map((m) => $m) - ?.toList()'''; + ?.map((m) => $m).toList()'''; } else if (isMapToModelType(type)) { var rc = ReCase( type.typeArguments[1].getDisplayString(withNullability: true)); @@ -215,7 +221,7 @@ class ${pascal}Decoder extends Converter { buf.write('};'); method.body = Block.of([ -// Code('if (model == null) { return null; }'), + Code('if (model == null) { return {}; }'), Code(buf.toString()), ]); })); @@ -275,18 +281,21 @@ class ${pascal}Decoder extends Converter { var deserializedRepresentation = "map['$alias'] as ${typeToString(type)}"; - String? defaultValue = 'null'; + var defaultValue = 'null'; var existingDefault = ctx.defaults[field.name]; if (existingDefault != null) { - defaultValue = dartObjectToString(existingDefault); + var d = dartObjectToString(existingDefault); + if (d != null) { + defaultValue = d; + } deserializedRepresentation = '$deserializedRepresentation ?? $defaultValue'; } - if (ctx.fieldInfo[field.name]?.deserializer != null) { - var name = - MirrorSystem.getName(ctx.fieldInfo[field.name]!.deserializer!); + var fieldNameDeserializer = ctx.fieldInfo[field.name]?.deserializer; + if (fieldNameDeserializer != null) { + var name = MirrorSystem.getName(fieldNameDeserializer); deserializedRepresentation = "$name(map['$alias'])"; } else if (dateTimeTypeChecker.isAssignableFromType(type)) { deserializedRepresentation = "map['$alias'] != null ? " @@ -302,6 +311,9 @@ class ${pascal}Decoder extends Converter { ' : $defaultValue'; } else if (type is InterfaceType) { if (isListOfModelType(type)) { + if (defaultValue == 'null') { + defaultValue = '[]'; + } var rc = ReCase( type.typeArguments[0].getDisplayString(withNullability: true)); deserializedRepresentation = "map['$alias'] is Iterable" @@ -310,8 +322,13 @@ class ${pascal}Decoder extends Converter { '.map(${rc.pascalCase.replaceAll('?', '')}Serializer.fromMap))' ' : $defaultValue'; } else if (isMapToModelType(type)) { + if (defaultValue == 'null') { + defaultValue = '{}'; + } + var rc = ReCase( type.typeArguments[1].getDisplayString(withNullability: true)); + deserializedRepresentation = ''' map['$alias'] is Map ? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) { @@ -335,7 +352,7 @@ class ${pascal}Decoder extends Converter { .isAssignableFromType(type) && type.typeArguments.length == 1) { var arg = convertTypeReference(type.typeArguments[0]) - .accept(DartEmitter()); + .accept(DartEmitter(useNullSafetySyntax: true)); deserializedRepresentation = ''' map['$alias'] is Iterable ? (map['$alias'] as Iterable).cast<$arg>().toList() @@ -345,9 +362,9 @@ class ${pascal}Decoder extends Converter { .isAssignableFromType(type) && type.typeArguments.length == 2) { var key = convertTypeReference(type.typeArguments[0]) - .accept(DartEmitter()); + .accept(DartEmitter(useNullSafetySyntax: true)); var value = convertTypeReference(type.typeArguments[1]) - .accept(DartEmitter()); + .accept(DartEmitter(useNullSafetySyntax: true)); deserializedRepresentation = ''' map['$alias'] is Map ? (map['$alias'] as Map).cast<$key, $value>() @@ -382,7 +399,7 @@ class ${pascal}Decoder extends Converter { } void generateFieldsClass(BuildContext ctx, LibraryBuilder file) { - log.fine('Generate serializer fields'); + //log.fine('Generate serializer fields'); file.body.add(Class((clazz) { clazz diff --git a/packages/serialize/angel_serialize_generator/pubspec.yaml b/packages/serialize/angel_serialize_generator/pubspec.yaml index e0148946..53322ff7 100644 --- a/packages/serialize/angel_serialize_generator/pubspec.yaml +++ b/packages/serialize/angel_serialize_generator/pubspec.yaml @@ -1,5 +1,5 @@ name: angel3_serialize_generator -version: 4.1.1 +version: 4.1.2 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/angel3/packages/serialize/angel_serialize_generator