Browse Source

dynamite: add built_value serializer

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/194/head
Nikolas Rimikis 2 years ago
parent
commit
7f13c35e6e
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 23
      packages/dynamite/lib/src/openapi_builder.dart

23
packages/dynamite/lib/src/openapi_builder.dart

@ -65,6 +65,8 @@ class OpenAPIBuilder implements Builder {
'',
"import 'package:built_collection/built_collection.dart';",
"import 'package:built_value/built_value.dart';",
"import 'package:built_value/serializer.dart';",
"import 'package:built_value/standard_json_plugin.dart';",
"import 'package:cookie_jar/cookie_jar.dart';",
"import 'package:universal_io/io.dart';",
'',
@ -1031,24 +1033,17 @@ class OpenAPIBuilder implements Builder {
if (state.registeredJsonObjects.isNotEmpty) {
output.addAll([
'// coverage:ignore-start',
'final _deserializers = <Type, dynamic Function(dynamic)>{',
'@SerializersFor(const [',
for (final name in state.registeredJsonObjects) ...[
'$name: (final data) => ${TypeResultObject(name).deserialize('data')},',
'List<$name>: (final data) => ${TypeResultList('List<$name>', TypeResultObject(name)).deserialize('data')},',
'$name,',
],
'};',
'])',
r'final Serializers serializers = (_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();',
'',
'final _serializers = <Type, dynamic Function(dynamic)>{',
for (final name in state.registeredJsonObjects) ...[
'$name: (final data) => ${TypeResultObject(name).serialize('data')},',
'List<$name>: (final data) => ${TypeResultList('List<$name>', TypeResultObject(name)).serialize('data')},',
],
'};',
'',
'T deserialize$prefix<T>(final dynamic data) => _deserializers[T]!(data) as T;',
'// coverage:ignore-start',
'T deserialize$prefix<T>(final Object data) => serializers.deserialize(data, specifiedType: FullType(T))! as T;',
'',
'dynamic serialize$prefix<T>(final T data) => _serializers[T]!(data);',
'Object? serialize$prefix<T>(final T data) => serializers.serialize(data, specifiedType: FullType(T));',
'// coverage:ignore-end',
]);
}

Loading…
Cancel
Save