Browse Source

dynamite: add builder factories for custom combinations

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/194/head
Nikolas Rimikis 2 years ago
parent
commit
095e613bcc
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 9
      packages/dynamite/dynamite/lib/src/openapi_builder.dart
  2. 3
      packages/dynamite/dynamite/lib/src/type_result/base.dart
  3. 3
      packages/dynamite/dynamite/lib/src/type_result/enum.dart
  4. 3
      packages/dynamite/dynamite/lib/src/type_result/list.dart
  5. 3
      packages/dynamite/dynamite/lib/src/type_result/map.dart
  6. 3
      packages/dynamite/dynamite/lib/src/type_result/object.dart
  7. 11
      packages/dynamite/dynamite/lib/src/type_result/type_result.dart

9
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 = <String>[];
final registeredJsonObjects = <String>[];
final output = <Spec>[];
final resolvedTypeCombinations = <TypeResult>[];
}
TypeResult resolveObject(

3
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;

3
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';

3
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')})';

3
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<String, ${subType.className}>.new)';
@override
String serialize(final String object) => object;

3
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) {

11
packages/dynamite/dynamite/lib/src/type_result/type_result.dart

@ -32,6 +32,17 @@ abstract class TypeResult {
return 'FullType($className)';
}
List<String> 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});

Loading…
Cancel
Save