From 6db0cb3179332b661227b57260f223424c169cf1 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Mon, 17 Jul 2023 21:47:14 +0200 Subject: [PATCH] fix(dynamite): add serializers of parameter types --- .../dynamite/lib/src/openapi_builder.dart | 7 +++--- .../dynamite/lib/src/type_result/base.dart | 3 +++ .../dynamite/lib/src/type_result/enum.dart | 3 +++ .../dynamite/lib/src/type_result/list.dart | 3 +++ .../dynamite/lib/src/type_result/map.dart | 3 +++ .../dynamite/lib/src/type_result/object.dart | 3 +++ .../lib/src/type_result/type_result.dart | 22 ++++++++++++------- 7 files changed, 33 insertions(+), 11 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index 9f74a174..8ab1dd40 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -721,9 +721,10 @@ class OpenAPIBuilder implements Builder { ], '])', r'final Serializers _serializers = (_$_serializers.toBuilder()', - for (final type in state.resolvedTypeCombinations) ...[ - ...type.builderFactories, - ], + ...state.resolvedTypeCombinations + .map((final type) => type.serializers) + .expand((final element) => element) + .toSet(), ').build();', '', 'Serializers get ${variablePrefix}Serializers => _serializers;', diff --git a/packages/dynamite/dynamite/lib/src/type_result/base.dart b/packages/dynamite/dynamite/lib/src/type_result/base.dart index 08a006ba..13c2218e 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/base.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/base.dart @@ -10,6 +10,9 @@ class TypeResultBase extends TypeResult { @override String? get _builderFactory => null; + @override + String? get _serializer => 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 7ef627a0..8f2cc279 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/enum.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/enum.dart @@ -13,6 +13,9 @@ class TypeResultEnum extends TypeResult { @override String? get _builderFactory => null; + @override + String? get _serializer => '..add($className.serializer)'; + @override String encode( final String object, { diff --git a/packages/dynamite/dynamite/lib/src/type_result/list.dart b/packages/dynamite/dynamite/lib/src/type_result/list.dart index 2034436e..a3ce03ce 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/list.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/list.dart @@ -13,6 +13,9 @@ class TypeResultList extends TypeResult { @override String? get _builderFactory => '..addBuilderFactory($fullType, ListBuilder<${subType.className}>.new)'; + @override + String? get _serializer => null; + @override String encode( final String object, { diff --git a/packages/dynamite/dynamite/lib/src/type_result/map.dart b/packages/dynamite/dynamite/lib/src/type_result/map.dart index 0050354d..7ae50130 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/map.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/map.dart @@ -13,6 +13,9 @@ class TypeResultMap extends TypeResult { @override String? get _builderFactory => '..addBuilderFactory($fullType, MapBuilder.new)'; + @override + String? get _serializer => null; + @override TypeResultMap get dartType => TypeResultMap('Map', subType, nullable: nullable); diff --git a/packages/dynamite/dynamite/lib/src/type_result/object.dart b/packages/dynamite/dynamite/lib/src/type_result/object.dart index b6f94bc3..830f91a7 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/object.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/object.dart @@ -16,6 +16,9 @@ class TypeResultObject extends TypeResult { @override String? get _builderFactory => '..addBuilderFactory($fullType, $name.new)'; + @override + String? get _serializer => '..add($className.serializer)'; + @override String encode( final String object, { 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 7dbf1cd8..021b0d93 100644 --- a/packages/dynamite/dynamite/lib/src/type_result/type_result.dart +++ b/packages/dynamite/dynamite/lib/src/type_result/type_result.dart @@ -43,14 +43,20 @@ abstract class TypeResult { return 'FullType($className)'; } - List get builderFactories => [ - for (final class_ in generics) ...[ - ...class_.builderFactories, - ], - if (_builderFactory != null) ...[ - _builderFactory!, - ], - ]; + Iterable get serializers sync* { + for (final class_ in generics) { + yield* class_.serializers; + } + + if (_builderFactory != null) { + yield _builderFactory!; + } + if (_serializer != null) { + yield _serializer!; + } + } + + String? get _serializer; String? get _builderFactory;