From 095e613bcce9ec08ab951f8df31e43626023ea06 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Fri, 5 May 2023 15:17:06 +0200 Subject: [PATCH] dynamite: add builder factories for custom combinations Signed-off-by: Nikolas Rimikis --- .../dynamite/dynamite/lib/src/openapi_builder.dart | 9 ++++++++- .../dynamite/dynamite/lib/src/type_result/base.dart | 3 +++ .../dynamite/dynamite/lib/src/type_result/enum.dart | 3 +++ .../dynamite/dynamite/lib/src/type_result/list.dart | 3 +++ .../dynamite/dynamite/lib/src/type_result/map.dart | 3 +++ .../dynamite/dynamite/lib/src/type_result/object.dart | 3 +++ .../dynamite/lib/src/type_result/type_result.dart | 11 +++++++++++ 7 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index 9369d5c3..0240fa04 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -727,6 +727,8 @@ class OpenAPIBuilder implements Builder { parameter.schema!, ); + state.resolvedTypeCombinations.add(result); + if (result.name == 'String') { if (parameter.schema?.pattern != null) { code.write(''' @@ -1016,7 +1018,11 @@ class OpenAPIBuilder implements Builder { '$name,', ], '])', - r'final Serializers serializers = _$serializers;', + r'final Serializers serializers = (_$serializers.toBuilder()', + for (final type in state.resolvedTypeCombinations) ...[ + ...type.builderFactories, + ], + ').build();', '', 'final Serializers jsonSerializers = (serializers.toBuilder()..addPlugin(StandardJsonPlugin())..addPlugin(const ContentStringPlugin())).build();', '', @@ -1228,6 +1234,7 @@ class State { final resolvedTypes = []; final registeredJsonObjects = []; final output = []; + final resolvedTypeCombinations = []; } TypeResult resolveObject( diff --git a/packages/dynamite/dynamite/lib/src/type_result/base.dart b/packages/dynamite/dynamite/lib/src/type_result/base.dart index 5ccb68f0..941659ef 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/base.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/base.dart @@ -3,6 +3,9 @@ part of '../../dynamite.dart'; class TypeResultBase extends TypeResult { TypeResultBase(super.name); + @override + String? get _builderFactory => null; + @override String serialize(final String object) => object; diff --git a/packages/dynamite/dynamite/lib/src/type_result/enum.dart b/packages/dynamite/dynamite/lib/src/type_result/enum.dart index f14e2db4..735f8feb 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/enum.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/enum.dart @@ -8,6 +8,9 @@ class TypeResultEnum extends TypeResult { final TypeResult subType; + @override + String? get _builderFactory => null; + @override String serialize(final String object) => '$object.name'; diff --git a/packages/dynamite/dynamite/lib/src/type_result/list.dart b/packages/dynamite/dynamite/lib/src/type_result/list.dart index a674f32d..6e4d24cd 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/list.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/list.dart @@ -8,6 +8,9 @@ class TypeResultList extends TypeResult { TypeResult get subType => generics.first; + @override + String? get _builderFactory => '..addBuilderFactory($fullType, ListBuilder<${subType.className}>.new)'; + @override String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')})'; diff --git a/packages/dynamite/dynamite/lib/src/type_result/map.dart b/packages/dynamite/dynamite/lib/src/type_result/map.dart index 802d00d1..c12ea28f 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/map.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/map.dart @@ -8,6 +8,9 @@ class TypeResultMap extends TypeResult { TypeResult get subType => generics[1]; + @override + String? get _builderFactory => '..addBuilderFactory($fullType, ListBuilder.new)'; + @override String serialize(final String object) => object; diff --git a/packages/dynamite/dynamite/lib/src/type_result/object.dart b/packages/dynamite/dynamite/lib/src/type_result/object.dart index 9a6ff342..460da700 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/object.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/object.dart @@ -11,6 +11,9 @@ class TypeResultObject extends TypeResult { 'Use TypeResultBase instead', ); + @override + String? get _builderFactory => '..addBuilderFactory($fullType, $name.new)'; + @override String serialize(final String object) { if (className == _contentString) { diff --git a/packages/dynamite/dynamite/lib/src/type_result/type_result.dart b/packages/dynamite/dynamite/lib/src/type_result/type_result.dart index ea6cca16..657abba4 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/type_result.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/type_result.dart @@ -32,6 +32,17 @@ abstract class TypeResult { return 'FullType($className)'; } + List get builderFactories => [ + for (final class_ in generics) ...[ + ...class_.builderFactories, + ], + if (_builderFactory != null) ...[ + _builderFactory!, + ], + ]; + + String? get _builderFactory; + String serialize(final String object); String deserialize(final String object, {final bool toBuilder = false});