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

Loading…
Cancel
Save