diff --git a/packages/serialize/angel_serialize_generator/CHANGELOG.md b/packages/serialize/angel_serialize_generator/CHANGELOG.md index fb4515c4..023606f5 100644 --- a/packages/serialize/angel_serialize_generator/CHANGELOG.md +++ b/packages/serialize/angel_serialize_generator/CHANGELOG.md @@ -4,11 +4,12 @@ * Fixed `toMap` method generation * Fixed `fromMape` method generation +* Fixed `TypescriptBuilder` * Updated generator to add `const []` to `List` type args in the contructor * Updated generator to product non nullable aware code -* Removed redudant null checking in the generated code * Refactored away nullable code * Added logging to facilitate tracing the code generation via `-verbose` flag +* Removed redudant null checking in the generated code ## 4.1.1 diff --git a/packages/serialize/angel_serialize_generator/lib/build_context.dart b/packages/serialize/angel_serialize_generator/lib/build_context.dart index abb9ee56..2dc8e268 100644 --- a/packages/serialize/angel_serialize_generator/lib/build_context.dart +++ b/packages/serialize/angel_serialize_generator/lib/build_context.dart @@ -38,7 +38,7 @@ Future buildContext( Resolver resolver, bool autoSnakeCaseNames, {bool heedExclude = true}) async { - var id = clazz.location!.components.join('-'); + var id = clazz.location?.components.join('-'); if (_cache.containsKey(id)) { return _cache[id]; } diff --git a/packages/serialize/angel_serialize_generator/lib/serialize.dart b/packages/serialize/angel_serialize_generator/lib/serialize.dart index 17ca6b26..60e89182 100644 --- a/packages/serialize/angel_serialize_generator/lib/serialize.dart +++ b/packages/serialize/angel_serialize_generator/lib/serialize.dart @@ -218,10 +218,15 @@ class ${pascal}Decoder extends Converter { '''; } else if (const TypeChecker.fromRuntime(Uint8List) .isAssignableFromType(type)) { + var convert = + (field.type.nullabilitySuffix == NullabilitySuffix.question) + ? '!' + : ''; + serializedRepresentation = ''' - model.${field.name} == null ? - null - : base64.encode(model.${field.name}) + model.${field.name} != null ? + base64.encode(model.${field.name}$convert) + : null '''; } } @@ -261,6 +266,7 @@ class ${pascal}Decoder extends Converter { var buf = StringBuffer(); + // Required Fields ctx.requiredFields.forEach((key, msg) { if (ctx.excluded[key]?.canDeserialize == false) return; var name = ctx.resolveFieldName(key); @@ -274,11 +280,13 @@ class ${pascal}Decoder extends Converter { buf.writeln('return ${ctx.modelClassName}('); var i = 0; + // Parameters in the constructor for (var param in ctx.constructorParameters) { if (i++ > 0) buf.write(', '); buf.write(param.name); } + // Fields for (var field in ctx.fields) { var type = ctx.resolveSerializedFieldType(field.name); @@ -332,19 +340,16 @@ class ${pascal}Decoder extends Converter { } var rc = ReCase( type.typeArguments[0].getDisplayString(withNullability: true)); + deserializedRepresentation = "map['$alias'] is Iterable" " ? List.unmodifiable(((map['$alias'] as Iterable)" '.whereType())' '.map(${rc.pascalCase.replaceAll('?', '')}Serializer.fromMap))' ' : $defaultValue'; } else if (isMapToModelType(type)) { - if (defaultValue == 'null') { - defaultValue = '{}'; - } - + // TODO: This requires refractoring var rc = ReCase( type.typeArguments[1].getDisplayString(withNullability: true)); - deserializedRepresentation = ''' map['$alias'] is Map ? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) { @@ -364,9 +369,14 @@ class ${pascal}Decoder extends Converter { : $defaultValue ) '''; + + //log.warning('Code => $deserializedRepresentation'); } else if (const TypeChecker.fromRuntime(List) .isAssignableFromType(type) && type.typeArguments.length == 1) { + if (defaultValue == 'null') { + defaultValue = '[]'; + } var arg = convertTypeReference(type.typeArguments[0]) .accept(DartEmitter(useNullSafetySyntax: true)); deserializedRepresentation = ''' @@ -381,6 +391,10 @@ class ${pascal}Decoder extends Converter { .accept(DartEmitter(useNullSafetySyntax: true)); var value = convertTypeReference(type.typeArguments[1]) .accept(DartEmitter(useNullSafetySyntax: true)); + + if (defaultValue == 'null') { + defaultValue = '{}'; + } deserializedRepresentation = ''' map['$alias'] is Map ? (map['$alias'] as Map).cast<$key, $value>() diff --git a/packages/serialize/angel_serialize_generator/lib/typescript.dart b/packages/serialize/angel_serialize_generator/lib/typescript.dart index bbc75501..45bbf304 100644 --- a/packages/serialize/angel_serialize_generator/lib/typescript.dart +++ b/packages/serialize/angel_serialize_generator/lib/typescript.dart @@ -104,15 +104,16 @@ class TypeScriptDefinitionBuilder implements Builder { if (!refs.contains(ref)) refs.add(ref); } - var ctx = await (buildContext( + var ctx = await buildContext( type.element, ConstantReader( serializableTypeChecker.firstAnnotationOf(type.element)), buildStep, buildStep.resolver, autoSnakeCaseNames, - ) as FutureOr); - typeScriptType = ctx.modelClassNameRecase.pascalCase; + ); + + typeScriptType = ctx?.modelClassNameRecase.pascalCase ?? 'any'; } }