Skip to content

Commit

Permalink
Merge pull request #1400 from nextcloud/feat/dynamite/reduce-name-col…
Browse files Browse the repository at this point in the history
…lisions

Feat/dynamite/reduce-name-collisions
  • Loading branch information
Leptopoda authored Dec 31, 2023
2 parents 40af938 + da7a551 commit a4d3a49
Show file tree
Hide file tree
Showing 51 changed files with 3,951 additions and 3,645 deletions.
52 changes: 21 additions & 31 deletions packages/dynamite/dynamite/example/lib/petstore.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ class $Client extends DynamiteClient {
'Accept': 'application/json',
};

final $tags = jsonSerializers.serialize(tags, specifiedType: const FullType(BuiltList, [FullType(String)]));
final $tags = _$jsonSerializers.serialize(tags, specifiedType: const FullType(BuiltList, [FullType(String)]));
_parameters['tags'] = $tags;

final $limit = jsonSerializers.serialize(limit, specifiedType: const FullType(int));
final $limit = _$jsonSerializers.serialize(limit, specifiedType: const FullType(int));
_parameters['limit'] = $limit;

final _path = UriTemplate('/pets{?tags*,limit*}').expand(_parameters);
Expand All @@ -118,7 +118,7 @@ class $Client extends DynamiteClient {
),
bodyType: const FullType(BuiltList, [FullType(Pet)]),
headersType: null,
serializers: jsonSerializers,
serializers: _$jsonSerializers,
);
}

Expand Down Expand Up @@ -162,7 +162,7 @@ class $Client extends DynamiteClient {
Uint8List? _body;

_headers['Content-Type'] = 'application/json';
_body = utf8.encode(json.encode(jsonSerializers.serialize(newPet, specifiedType: const FullType(NewPet))));
_body = utf8.encode(json.encode(_$jsonSerializers.serialize(newPet, specifiedType: const FullType(NewPet))));
const _path = '/pets';
return DynamiteRawResponse<Pet, void>(
response: executeRequest(
Expand All @@ -174,7 +174,7 @@ class $Client extends DynamiteClient {
),
bodyType: const FullType(Pet),
headersType: null,
serializers: jsonSerializers,
serializers: _$jsonSerializers,
);
}

Expand Down Expand Up @@ -223,7 +223,7 @@ class $Client extends DynamiteClient {
'Accept': 'application/json',
};

final $id = jsonSerializers.serialize(id, specifiedType: const FullType(int));
final $id = _$jsonSerializers.serialize(id, specifiedType: const FullType(int));
_parameters['id'] = $id;

final _path = UriTemplate('/pets/{id}').expand(_parameters);
Expand All @@ -237,7 +237,7 @@ class $Client extends DynamiteClient {
),
bodyType: const FullType(Pet),
headersType: null,
serializers: jsonSerializers,
serializers: _$jsonSerializers,
);
}

Expand Down Expand Up @@ -284,7 +284,7 @@ class $Client extends DynamiteClient {
final _parameters = <String, dynamic>{};
final _headers = <String, String>{};

final $id = jsonSerializers.serialize(id, specifiedType: const FullType(int));
final $id = _$jsonSerializers.serialize(id, specifiedType: const FullType(int));
_parameters['id'] = $id;

final _path = UriTemplate('/pets/{id}').expand(_parameters);
Expand All @@ -298,7 +298,7 @@ class $Client extends DynamiteClient {
),
bodyType: null,
headersType: null,
serializers: jsonSerializers,
serializers: _$jsonSerializers,
);
}
}
Expand All @@ -320,16 +320,12 @@ abstract class NewPet implements $NewPetInterface, Built<NewPet, NewPetBuilder>
/// Creates a new object from the given [json] data.
///
/// Use [toJson] to serialize it back into json.
// coverage:ignore-start
factory NewPet.fromJson(Map<String, dynamic> json) => jsonSerializers.deserializeWith(serializer, json)!;
// coverage:ignore-end
factory NewPet.fromJson(Map<String, dynamic> json) => _$jsonSerializers.deserializeWith(serializer, json)!;

/// Parses this object into a json like map.
///
/// Use the fromJson factory to revive it again.
// coverage:ignore-start
Map<String, dynamic> toJson() => jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>;
// coverage:ignore-end
Map<String, dynamic> toJson() => _$jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>;

/// Serializer for NewPet.
static Serializer<NewPet> get serializer => _$newPetSerializer;
Expand All @@ -354,16 +350,12 @@ abstract class Pet implements $PetInterface, Built<Pet, PetBuilder> {
/// Creates a new object from the given [json] data.
///
/// Use [toJson] to serialize it back into json.
// coverage:ignore-start
factory Pet.fromJson(Map<String, dynamic> json) => jsonSerializers.deserializeWith(serializer, json)!;
// coverage:ignore-end
factory Pet.fromJson(Map<String, dynamic> json) => _$jsonSerializers.deserializeWith(serializer, json)!;

/// Parses this object into a json like map.
///
/// Use the fromJson factory to revive it again.
// coverage:ignore-start
Map<String, dynamic> toJson() => jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>;
// coverage:ignore-end
Map<String, dynamic> toJson() => _$jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>;

/// Serializer for Pet.
static Serializer<Pet> get serializer => _$petSerializer;
Expand All @@ -386,16 +378,12 @@ abstract class Error implements $ErrorInterface, Built<Error, ErrorBuilder> {
/// Creates a new object from the given [json] data.
///
/// Use [toJson] to serialize it back into json.
// coverage:ignore-start
factory Error.fromJson(Map<String, dynamic> json) => jsonSerializers.deserializeWith(serializer, json)!;
// coverage:ignore-end
factory Error.fromJson(Map<String, dynamic> json) => _$jsonSerializers.deserializeWith(serializer, json)!;

/// Parses this object into a json like map.
///
/// Use the fromJson factory to revive it again.
// coverage:ignore-start
Map<String, dynamic> toJson() => jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>;
// coverage:ignore-end
Map<String, dynamic> toJson() => _$jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>;

/// Serializer for Error.
static Serializer<Error> get serializer => _$errorSerializer;
Expand All @@ -405,9 +393,10 @@ abstract class Error implements $ErrorInterface, Built<Error, ErrorBuilder> {
/// Serializer for all values in this library.
///
/// Serializes values into the `built_value` wire format.
/// See: [jsonSerializers] for serializing into json.
/// See: [$jsonSerializers] for serializing into json.
@visibleForTesting
final Serializers serializers = (Serializers().toBuilder()
final Serializers $serializers = _$serializers;
final Serializers _$serializers = (Serializers().toBuilder()
..addBuilderFactory(const FullType(BuiltList, [FullType(String)]), ListBuilder<String>.new)
..addBuilderFactory(const FullType(Pet), PetBuilder.new)
..add(Pet.serializer)
Expand All @@ -421,9 +410,10 @@ final Serializers serializers = (Serializers().toBuilder()
/// Serializer for all values in this library.
///
/// Serializes values into the json. Json serialization is more expensive than the built_value wire format.
/// See: [serializers] for serializing into the `built_value` wire format.
/// See: [$serializers] for serializing into the `built_value` wire format.
@visibleForTesting
final Serializers jsonSerializers = (serializers.toBuilder()
final Serializers $jsonSerializers = _$jsonSerializers;
final Serializers _$jsonSerializers = (_$serializers.toBuilder()
..add(DynamiteDoubleSerializer())
..addPlugin(StandardJsonPlugin())
..addPlugin(const HeaderPlugin())
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamite/dynamite/lib/src/builder/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ Iterable<Method> buildTags(
),
bodyType: ${dataType?.fullType},
headersType: ${headersType?.fullType},
serializers: jsonSerializers,
serializers: _\$jsonSerializers,
);
''');
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dynamite/dynamite/lib/src/builder/generate_ofs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Iterable<Spec> generateSomeOf(
),
)
..lambda = true
..body = const Code('jsonSerializers.deserializeWith(_serializer, json)!'),
..body = const Code(r'_$jsonSerializers.deserializeWith(_serializer, json)!'),
);

final toJson = Method(
Expand All @@ -144,7 +144,7 @@ Iterable<Spec> generateSomeOf(
..name = 'toJson'
..returns = refer('Object?')
..lambda = true
..body = const Code('jsonSerializers.serializeWith(_serializer, this)'),
..body = const Code(r'_$jsonSerializers.serializeWith(_serializer, this)'),
);

yield Extension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ TypeResult resolveEnum(
..name = 'value'
..type = MethodType.getter
..lambda = true
..body = Code('jsonSerializers.serializeWith(serializer, this)! as ${subResult.dartType.className}'),
..body = Code('_\$jsonSerializers.serializeWith(serializer, this)! as ${subResult.dartType.className}'),
),
buildSerializer(identifier, 'const _\$${identifier}Serializer()'),
]),
Expand Down
28 changes: 21 additions & 7 deletions packages/dynamite/dynamite/lib/src/builder/serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ List<Spec> buildSerializer(final State state) => [
const Code('// coverage:ignore-start\n'),
Field((final b) {
b
..docs.add('''
..docs.add(r'''
/// Serializer for all values in this library.
///
/// Serializes values into the `built_value` wire format.
/// See: [jsonSerializers] for serializing into json.''')
/// See: [$jsonSerializers] for serializing into json.''')
..annotations.add(refer('visibleForTesting'))
..modifier = FieldModifier.final$
..type = refer('Serializers')
..name = 'serializers';
..name = r'$serializers'
..assignment = const Code(r'_$serializers');
}),
Field((final b) {
b
..modifier = FieldModifier.final$
..type = refer('Serializers')
..name = r'_$serializers';

final serializers =
state.resolvedTypes.map((final type) => type.serializers).expand((final element) => element).toSet();
Expand All @@ -31,15 +38,22 @@ List<Spec> buildSerializer(final State state) => [
}),
Field((final b) {
b
..docs.add('''
..docs.add(r'''
/// Serializer for all values in this library.
///
/// Serializes values into the json. Json serialization is more expensive than the built_value wire format.
/// See: [serializers] for serializing into the `built_value` wire format.''')
/// See: [$serializers] for serializing into the `built_value` wire format.''')
..annotations.add(refer('visibleForTesting'))
..modifier = FieldModifier.final$
..type = refer('Serializers')
..name = 'jsonSerializers';
..name = r'$jsonSerializers'
..assignment = const Code(r'_$jsonSerializers');
}),
Field((final b) {
b
..modifier = FieldModifier.final$
..type = refer('Serializers')
..name = r'_$jsonSerializers';

const serializers = [
'..add(DynamiteDoubleSerializer())',
Expand All @@ -49,7 +63,7 @@ List<Spec> buildSerializer(final State state) => [
];

final bodyBuilder = StringBuffer()
..writeln('(serializers.toBuilder()')
..writeln(r'(_$serializers.toBuilder()')
..writeAll(serializers, '\n')
..writeln(').build()');

Expand Down
4 changes: 2 additions & 2 deletions packages/dynamite/dynamite/lib/src/helpers/built_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Method get toJsonMethod => Method(
..name = 'toJson'
..returns = refer('Map<String, dynamic>')
..lambda = true
..body = const Code('jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>'),
..body = const Code(r'_$jsonSerializers.serializeWith(serializer, this)! as Map<String, dynamic>'),
);

/// Builds the serializer getter for a built class.
Expand Down Expand Up @@ -164,5 +164,5 @@ Constructor get fromJsonConstructor => Constructor(
..type = refer('Map<String, dynamic>'),
),
)
..body = const Code('jsonSerializers.deserializeWith(serializer, json)!'),
..body = const Code(r'_$jsonSerializers.deserializeWith(serializer, json)!'),
);
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ const _reservedNames = [
'on',
'operator',
'part',
'RegExp',
'rethrow',
'return',
'set',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ sealed class TypeResult {
/// The serialized result is an [Object]?
@mustCallSuper
String serialize(final String object, [final String? serializerName]) =>
'${serializerName ?? 'jsonSerializers'}.serialize($object, specifiedType: $fullType)';
'${serializerName ?? r'_$jsonSerializers'}.serialize($object, specifiedType: $fullType)';

/// Deserializes the variable named [object].
///
/// The serialized result will be of [name].
@mustCallSuper
String deserialize(final String object, [final String? serializerName]) {
final buffer = StringBuffer()
..write(serializerName ?? 'jsonSerializers')
..write(serializerName ?? r'_$jsonSerializers')
..write('.deserialize(')
..write(object)
..write(', specifiedType: $fullType)');
Expand Down
28 changes: 14 additions & 14 deletions packages/dynamite/dynamite/test/type_result_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ void main() {
);
expect(
type.serialize('value'),
'jsonSerializers.serialize(value, specifiedType: const FullType(BuiltList, [FullType(String)]))',
r'_$jsonSerializers.serialize(value, specifiedType: const FullType(BuiltList, [FullType(String)]))',
);
expect(
type.deserialize('value'),
'jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltList, [FullType(String)]))! as BuiltList<String>',
r'_$jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltList, [FullType(String)]))! as BuiltList<String>',
);
});

Expand All @@ -40,11 +40,11 @@ void main() {
);
expect(
type.serialize('value'),
'jsonSerializers.serialize(value, specifiedType: const FullType(BuiltList, [FullType(BuiltList, [FullType(String)])]))',
r'_$jsonSerializers.serialize(value, specifiedType: const FullType(BuiltList, [FullType(BuiltList, [FullType(String)])]))',
);
expect(
type.deserialize('value'),
'jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltList, [FullType(BuiltList, [FullType(String)])]))! as BuiltList<BuiltList<String>>',
r'_$jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltList, [FullType(BuiltList, [FullType(String)])]))! as BuiltList<BuiltList<String>>',
);
});

Expand Down Expand Up @@ -75,11 +75,11 @@ void main() {
);
expect(
type.serialize('value'),
'jsonSerializers.serialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(int)]))',
r'_$jsonSerializers.serialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(int)]))',
);
expect(
type.deserialize('value'),
'jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(int)]))! as BuiltMap<String, int>',
r'_$jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(int)]))! as BuiltMap<String, int>',
);
});

Expand All @@ -102,11 +102,11 @@ void main() {
);
expect(
type.serialize('value'),
'jsonSerializers.serialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(BuiltMap, [FullType(String), FullType(int)])]))',
r'_$jsonSerializers.serialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(BuiltMap, [FullType(String), FullType(int)])]))',
);
expect(
type.deserialize('value'),
'jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(BuiltMap, [FullType(String), FullType(int)])]))! as BuiltMap<String, BuiltMap<String, int>>',
r'_$jsonSerializers.deserialize(value, specifiedType: const FullType(BuiltMap, [FullType(String), FullType(BuiltMap, [FullType(String), FullType(int)])]))! as BuiltMap<String, BuiltMap<String, int>>',
);
});

Expand Down Expand Up @@ -138,11 +138,11 @@ void main() {
);
expect(
type.serialize('value'),
'jsonSerializers.serialize(value, specifiedType: const FullType(CustomType, [FullType(String)]))',
r'_$jsonSerializers.serialize(value, specifiedType: const FullType(CustomType, [FullType(String)]))',
);
expect(
type.deserialize('value'),
'jsonSerializers.deserialize(value, specifiedType: const FullType(CustomType, [FullType(String)]))! as CustomType<String>',
r'_$jsonSerializers.deserialize(value, specifiedType: const FullType(CustomType, [FullType(String)]))! as CustomType<String>',
);
});

Expand All @@ -158,11 +158,11 @@ void main() {
]);
expect(
type.serialize('value'),
'jsonSerializers.serialize(value, specifiedType: const FullType(ContentString, [FullType(int)]))',
r'_$jsonSerializers.serialize(value, specifiedType: const FullType(ContentString, [FullType(int)]))',
);
expect(
type.deserialize('value'),
'jsonSerializers.deserialize(value, specifiedType: const FullType(ContentString, [FullType(int)]))! as ContentString<int>',
r'_$jsonSerializers.deserialize(value, specifiedType: const FullType(ContentString, [FullType(int)]))! as ContentString<int>',
);
});

Expand All @@ -187,11 +187,11 @@ void main() {
expect(type.serializers.toList(), const <String>[]);
expect(
type.serialize('value'),
'jsonSerializers.serialize(value, specifiedType: const FullType(String))',
r'_$jsonSerializers.serialize(value, specifiedType: const FullType(String))',
);
expect(
type.deserialize('value'),
'jsonSerializers.deserialize(value, specifiedType: const FullType(String))! as String',
r'_$jsonSerializers.deserialize(value, specifiedType: const FullType(String))! as String',
);
});

Expand Down
Loading

0 comments on commit a4d3a49

Please sign in to comment.