Updated serialize generator

This commit is contained in:
thomashii 2021-08-17 15:29:10 +08:00
parent b8024e8120
commit d2c6750225
4 changed files with 29 additions and 13 deletions

View file

@ -4,11 +4,12 @@
* Fixed `toMap` method generation * Fixed `toMap` method generation
* Fixed `fromMape` method generation * Fixed `fromMape` method generation
* Fixed `TypescriptBuilder`
* Updated generator to add `const []` to `List` type args in the contructor * Updated generator to add `const []` to `List` type args in the contructor
* Updated generator to product non nullable aware code * Updated generator to product non nullable aware code
* Removed redudant null checking in the generated code
* Refactored away nullable code * Refactored away nullable code
* Added logging to facilitate tracing the code generation via `-verbose` flag * Added logging to facilitate tracing the code generation via `-verbose` flag
* Removed redudant null checking in the generated code
## 4.1.1 ## 4.1.1

View file

@ -38,7 +38,7 @@ Future<BuildContext?> buildContext(
Resolver resolver, Resolver resolver,
bool autoSnakeCaseNames, bool autoSnakeCaseNames,
{bool heedExclude = true}) async { {bool heedExclude = true}) async {
var id = clazz.location!.components.join('-'); var id = clazz.location?.components.join('-');
if (_cache.containsKey(id)) { if (_cache.containsKey(id)) {
return _cache[id]; return _cache[id];
} }

View file

@ -218,10 +218,15 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
'''; ''';
} else if (const TypeChecker.fromRuntime(Uint8List) } else if (const TypeChecker.fromRuntime(Uint8List)
.isAssignableFromType(type)) { .isAssignableFromType(type)) {
var convert =
(field.type.nullabilitySuffix == NullabilitySuffix.question)
? '!'
: '';
serializedRepresentation = ''' serializedRepresentation = '''
model.${field.name} == null ? model.${field.name} != null ?
null base64.encode(model.${field.name}$convert)
: base64.encode(model.${field.name}) : null
'''; ''';
} }
} }
@ -261,6 +266,7 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
var buf = StringBuffer(); var buf = StringBuffer();
// Required Fields
ctx.requiredFields.forEach((key, msg) { ctx.requiredFields.forEach((key, msg) {
if (ctx.excluded[key]?.canDeserialize == false) return; if (ctx.excluded[key]?.canDeserialize == false) return;
var name = ctx.resolveFieldName(key); var name = ctx.resolveFieldName(key);
@ -274,11 +280,13 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
buf.writeln('return ${ctx.modelClassName}('); buf.writeln('return ${ctx.modelClassName}(');
var i = 0; var i = 0;
// Parameters in the constructor
for (var param in ctx.constructorParameters) { for (var param in ctx.constructorParameters) {
if (i++ > 0) buf.write(', '); if (i++ > 0) buf.write(', ');
buf.write(param.name); buf.write(param.name);
} }
// Fields
for (var field in ctx.fields) { for (var field in ctx.fields) {
var type = ctx.resolveSerializedFieldType(field.name); var type = ctx.resolveSerializedFieldType(field.name);
@ -332,19 +340,16 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
} }
var rc = ReCase( var rc = ReCase(
type.typeArguments[0].getDisplayString(withNullability: true)); type.typeArguments[0].getDisplayString(withNullability: true));
deserializedRepresentation = "map['$alias'] is Iterable" deserializedRepresentation = "map['$alias'] is Iterable"
" ? List.unmodifiable(((map['$alias'] as Iterable)" " ? List.unmodifiable(((map['$alias'] as Iterable)"
'.whereType<Map>())' '.whereType<Map>())'
'.map(${rc.pascalCase.replaceAll('?', '')}Serializer.fromMap))' '.map(${rc.pascalCase.replaceAll('?', '')}Serializer.fromMap))'
' : $defaultValue'; ' : $defaultValue';
} else if (isMapToModelType(type)) { } else if (isMapToModelType(type)) {
if (defaultValue == 'null') { // TODO: This requires refractoring
defaultValue = '{}';
}
var rc = ReCase( var rc = ReCase(
type.typeArguments[1].getDisplayString(withNullability: true)); type.typeArguments[1].getDisplayString(withNullability: true));
deserializedRepresentation = ''' deserializedRepresentation = '''
map['$alias'] is Map map['$alias'] is Map
? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) { ? Map.unmodifiable((map['$alias'] as Map).keys.fold({}, (out, key) {
@ -364,9 +369,14 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
: $defaultValue : $defaultValue
) )
'''; ''';
//log.warning('Code => $deserializedRepresentation');
} else if (const TypeChecker.fromRuntime(List) } else if (const TypeChecker.fromRuntime(List)
.isAssignableFromType(type) && .isAssignableFromType(type) &&
type.typeArguments.length == 1) { type.typeArguments.length == 1) {
if (defaultValue == 'null') {
defaultValue = '[]';
}
var arg = convertTypeReference(type.typeArguments[0]) var arg = convertTypeReference(type.typeArguments[0])
.accept(DartEmitter(useNullSafetySyntax: true)); .accept(DartEmitter(useNullSafetySyntax: true));
deserializedRepresentation = ''' deserializedRepresentation = '''
@ -381,6 +391,10 @@ class ${pascal}Decoder extends Converter<Map, $pascal> {
.accept(DartEmitter(useNullSafetySyntax: true)); .accept(DartEmitter(useNullSafetySyntax: true));
var value = convertTypeReference(type.typeArguments[1]) var value = convertTypeReference(type.typeArguments[1])
.accept(DartEmitter(useNullSafetySyntax: true)); .accept(DartEmitter(useNullSafetySyntax: true));
if (defaultValue == 'null') {
defaultValue = '{}';
}
deserializedRepresentation = ''' deserializedRepresentation = '''
map['$alias'] is Map map['$alias'] is Map
? (map['$alias'] as Map).cast<$key, $value>() ? (map['$alias'] as Map).cast<$key, $value>()

View file

@ -104,15 +104,16 @@ class TypeScriptDefinitionBuilder implements Builder {
if (!refs.contains(ref)) refs.add(ref); if (!refs.contains(ref)) refs.add(ref);
} }
var ctx = await (buildContext( var ctx = await buildContext(
type.element, type.element,
ConstantReader( ConstantReader(
serializableTypeChecker.firstAnnotationOf(type.element)), serializableTypeChecker.firstAnnotationOf(type.element)),
buildStep, buildStep,
buildStep.resolver, buildStep.resolver,
autoSnakeCaseNames, autoSnakeCaseNames,
) as FutureOr<BuildContext>); );
typeScriptType = ctx.modelClassNameRecase.pascalCase;
typeScriptType = ctx?.modelClassNameRecase.pascalCase ?? 'any';
} }
} }