forked from nextcloud/neon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dynamite): generate custom serializers for every built class
Signed-off-by: Nikolas Rimikis <[email protected]>
- Loading branch information
Showing
3 changed files
with
242 additions
and
4 deletions.
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
packages/dynamite/dynamite/lib/src/builder/built_object_serializer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
import 'package:code_builder/code_builder.dart'; | ||
import 'package:dynamite/src/builder/resolve_type.dart'; | ||
import 'package:dynamite/src/builder/state.dart'; | ||
import 'package:dynamite/src/helpers/dart_helpers.dart'; | ||
import 'package:dynamite/src/helpers/dynamite.dart'; | ||
import 'package:dynamite/src/models/openapi.dart' as openapi; | ||
import 'package:dynamite/src/models/type_result.dart'; | ||
|
||
Spec buildBuiltClassSerializer( | ||
final State state, | ||
final String identifier, | ||
final openapi.OpenAPI spec, | ||
final openapi.Schema schema, | ||
) => | ||
Class( | ||
(final b) => b | ||
..name = '_\$${identifier}Serializer' | ||
..implements.add(refer('StructuredSerializer<$identifier>')) | ||
..constructors.add( | ||
Constructor( | ||
(final b) => b..constant = true, | ||
), | ||
) | ||
..methods.addAll([ | ||
Method( | ||
(final b) => b | ||
..name = 'types' | ||
..type = MethodType.getter | ||
..lambda = true | ||
..returns = refer('Iterable<Type>') | ||
..annotations.add(refer('override')) | ||
..body = Code('const [$identifier, _\$$identifier]'), | ||
), | ||
Method( | ||
(final b) => b | ||
..name = 'wireName' | ||
..type = MethodType.getter | ||
..lambda = true | ||
..returns = refer('String') | ||
..annotations.add(refer('override')) | ||
..body = Code("r'$identifier'"), | ||
), | ||
Method((final b) { | ||
b | ||
..name = 'serialize' | ||
..returns = refer('Iterable<Object?>') | ||
..annotations.add(refer('override')) | ||
..requiredParameters.addAll([ | ||
Parameter( | ||
(final b) => b | ||
..name = 'serializers' | ||
..type = refer('Serializers'), | ||
), | ||
Parameter( | ||
(final b) => b | ||
..name = 'object' | ||
..type = refer(identifier), | ||
), | ||
]) | ||
..optionalParameters.add( | ||
Parameter( | ||
(final b) => b | ||
..name = 'specifiedType' | ||
..type = refer('FullType') | ||
..named = true | ||
..defaultTo = const Code('FullType.unspecified'), | ||
), | ||
); | ||
|
||
final properties = serializeProperty(state, identifier, spec, schema); | ||
final nullableProperties = serializePropertyNullable(state, identifier, spec, schema); | ||
final buffer = StringBuffer() | ||
..write('final result = <Object?>[') | ||
..writeAll(properties, '\n') | ||
..write('];') | ||
..writeln(); | ||
|
||
if (nullableProperties.isNotEmpty) { | ||
buffer | ||
..write('Object? value;') | ||
..writeAll(nullableProperties, '\n') | ||
..writeln(); | ||
} | ||
|
||
buffer.write('return result;'); | ||
|
||
b.body = Code(buffer.toString()); | ||
}), | ||
Method((final b) { | ||
b | ||
..name = 'deserialize' | ||
..returns = refer(identifier) | ||
..annotations.add(refer('override')) | ||
..requiredParameters.addAll([ | ||
Parameter( | ||
(final b) => b | ||
..name = 'serializers' | ||
..type = refer('Serializers'), | ||
), | ||
Parameter( | ||
(final b) => b | ||
..name = 'serialized' | ||
..type = refer('Iterable<Object?>'), | ||
), | ||
]) | ||
..optionalParameters.add( | ||
Parameter( | ||
(final b) => b | ||
..name = 'specifiedType' | ||
..type = refer('FullType') | ||
..named = true | ||
..defaultTo = const Code('FullType.unspecified'), | ||
), | ||
) | ||
..body = Code(''' | ||
final result = new ${identifier}Builder(); | ||
final iterator = serialized.iterator; | ||
while (iterator.moveNext()) { | ||
final key = iterator.current! as String; | ||
iterator.moveNext(); | ||
final value = iterator.current; | ||
switch (key) { | ||
${deserializeProperty(state, identifier, spec, schema).join('\n')} | ||
} | ||
} | ||
return result.build(); | ||
'''); | ||
}), | ||
]), | ||
); | ||
|
||
Iterable<String> deserializeProperty( | ||
final State state, | ||
final String identifier, | ||
final openapi.OpenAPI spec, | ||
final openapi.Schema schema, | ||
) sync* { | ||
for (final property in schema.properties!.entries) { | ||
final propertyName = property.key; | ||
final propertySchema = property.value; | ||
final dartName = toDartName(propertyName); | ||
final result = resolveType( | ||
spec, | ||
state, | ||
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}', | ||
propertySchema, | ||
nullable: isDartParameterNullable(schema.required.contains(propertyName), propertySchema), | ||
); | ||
|
||
yield "case '$propertyName':"; | ||
final deserialize = result.deserialize('value', 'serializers'); | ||
|
||
if (result is TypeResultBase || result is TypeResultEnum) { | ||
yield 'result.$dartName = $deserialize;'; | ||
} else { | ||
yield 'result.$dartName.replace($deserialize,);'; | ||
} | ||
} | ||
} | ||
|
||
Iterable<String> serializePropertyNullable( | ||
final State state, | ||
final String identifier, | ||
final openapi.OpenAPI spec, | ||
final openapi.Schema schema, | ||
) sync* { | ||
for (final property in schema.properties!.entries) { | ||
final propertyName = property.key; | ||
final propertySchema = property.value; | ||
final dartName = toDartName(propertyName); | ||
final result = resolveType( | ||
spec, | ||
state, | ||
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}', | ||
propertySchema, | ||
nullable: isDartParameterNullable(schema.required.contains(propertyName), propertySchema), | ||
); | ||
if (!result.nullable) { | ||
continue; | ||
} | ||
|
||
yield ''' | ||
value = object.$dartName; | ||
if (value != null) { | ||
result | ||
..add('$propertyName') | ||
..add(${result.serialize('value', 'serializers')},); | ||
} | ||
'''; | ||
} | ||
} | ||
|
||
Iterable<String> serializeProperty( | ||
final State state, | ||
final String identifier, | ||
final openapi.OpenAPI spec, | ||
final openapi.Schema schema, | ||
) sync* { | ||
for (final property in schema.properties!.entries) { | ||
final propertyName = property.key; | ||
final propertySchema = property.value; | ||
final dartName = toDartName(propertyName); | ||
final result = resolveType( | ||
spec, | ||
state, | ||
'${identifier}_${toDartName(propertyName, uppercaseFirstCharacter: true)}', | ||
propertySchema, | ||
nullable: isDartParameterNullable(schema.required.contains(propertyName), propertySchema), | ||
); | ||
if (result.nullable) { | ||
continue; | ||
} | ||
|
||
yield ''' | ||
'$propertyName', | ||
${result.serialize('object.$dartName', 'serializers')}, | ||
'''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters