Browse Source

dynamite: unify serialization and encoding

pull/324/head
Nikolas Rimikis 2 years ago
parent
commit
8d1f4143bd
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 24
      packages/dynamite/dynamite/lib/src/openapi_builder.dart
  2. 2
      packages/dynamite/dynamite/lib/src/type_result/base.dart
  3. 14
      packages/dynamite/dynamite/lib/src/type_result/enum.dart
  4. 19
      packages/dynamite/dynamite/lib/src/type_result/list.dart
  5. 28
      packages/dynamite/dynamite/lib/src/type_result/map.dart
  6. 32
      packages/dynamite/dynamite/lib/src/type_result/object.dart
  7. 29
      packages/dynamite/dynamite/lib/src/type_result/type_result.dart

24
packages/dynamite/dynamite/lib/src/openapi_builder.dart

@ -779,7 +779,7 @@ class OpenAPIBuilder implements Builder {
code.write('if (${_toDartName(parameter.name)} != null) {');
}
final value = result.encode(
result.serialize(_toDartName(parameter.name)),
_toDartName(parameter.name),
onlyChildren: result is TypeResultList && parameter.in_ == 'query',
// Objects inside the query always have to be interpreted in some way
mimeType: 'application/json',
@ -858,7 +858,7 @@ class OpenAPIBuilder implements Builder {
code.write('if ($parameterName != null) {');
}
code.write(
'body = Uint8List.fromList(utf8.encode(${result.encode(result.serialize(parameterName), mimeType: mimeType)}));',
'body = Uint8List.fromList(utf8.encode(${result.encode(parameterName, mimeType: mimeType)}));',
);
if (dartParameterNullable) {
code.write('}');
@ -1493,9 +1493,13 @@ TypeResult resolveObject(
return [
Code("case '$propertyName':"),
if (result.className != 'String') ...[
if (result is TypeResultBase || result is TypeResultEnum) ...[
Code('result.${_toDartName(propertyName)} = ${result.deserialize(result.decode('value!'))};'),
] else ...[
Code(
'result.${_toDartName(propertyName)} = ${result.deserialize('jsonDecode(value!)', toBuilder: true)};',
'result.${_toDartName(propertyName)}.replace(${result.deserialize(result.decode('value!'))});',
),
],
] else ...[
Code(
'result.${_toDartName(propertyName)} = value!;',
@ -1512,7 +1516,7 @@ TypeResult resolveObject(
const Code('while (iterator.moveNext()) {'),
const Code('final key = iterator.current! as String;'),
const Code('iterator.moveNext();'),
const Code('final value = iterator.current as String?;'),
const Code('final value = iterator.current! as String;'),
const Code('switch (key) {'),
for (final propertyName in schema.properties!.keys) ...[
...deserializeProperty(propertyName),
@ -1749,7 +1753,11 @@ TypeResult resolveType(
'..data = JsonObject(data);',
if (schema.allOf != null) ...[
for (final result in results) ...[
'result.${fields[result.name]!} = ${result.deserialize('data', toBuilder: true)};',
if (result is TypeResultBase || result is TypeResultEnum) ...[
'result.${fields[result.name]!} = ${result.deserialize('data')};',
] else ...[
'result.${fields[result.name]!}.replace(${result.deserialize('data')});',
],
],
] else ...[
if (schema.discriminator != null) ...[
@ -1786,7 +1794,11 @@ TypeResult resolveType(
],
],
'try {',
'result.${fields[result.name]!} = ${result.deserialize('data', toBuilder: true)};',
if (result is TypeResultBase || result is TypeResultEnum) ...[
'result._${fields[result.name]!} = ${result.deserialize('data')};',
] else ...[
'result._${fields[result.name]!} = ${result.deserialize('data')}.toBuilder();',
],
'} catch (_) {',
if (schema.discriminator != null) ...[
'rethrow;',

2
packages/dynamite/dynamite/lib/src/type_result/base.dart

@ -26,7 +26,7 @@ class TypeResultBase extends TypeResult {
return 'JsonObject($object)${nullable ? '?' : ''}';
}
return '($object as $name ${nullable ? '?' : ''})';
return '($object as $nullableName)';
}
@override

14
packages/dynamite/dynamite/lib/src/type_result/enum.dart

@ -12,20 +12,18 @@ class TypeResultEnum extends TypeResult {
@override
String? get _builderFactory => null;
@override
String serialize(final String object) => '$object.name';
@override
String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) =>
subType.encode(object);
}) {
if (subType.name == 'String') {
return '$object.name';
}
@override
String deserialize(final String object, {final bool toBuilder = false}) =>
'$name.valueOf($object as ${subType.name})';
return super.encode(object, mimeType: mimeType);
}
@override
String decode(final String object) => subType.decode(object);

19
packages/dynamite/dynamite/lib/src/type_result/list.dart

@ -12,9 +12,6 @@ class TypeResultList extends TypeResult {
@override
String? get _builderFactory => '..addBuilderFactory($fullType, ListBuilder<${subType.className}>.new)';
@override
String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')})';
@override
String encode(
final String object, {
@ -25,22 +22,8 @@ class TypeResultList extends TypeResult {
return '$object.map((final e) => ${subType.encode('e', mimeType: mimeType)})';
}
switch (mimeType) {
case 'application/json':
return 'json.encode($object)';
case 'application/x-www-form-urlencoded':
return 'Uri(queryParameters: $object).query';
default:
throw Exception('Can not encode mime type "$mimeType"');
return super.encode(object, mimeType: mimeType);
}
}
@override
String deserialize(final String object, {final bool toBuilder = false}) =>
'$name(($object as List).map((final e) => ${subType.deserialize('e')}))${toBuilder ? '.toBuilder()' : ''}';
@override
String decode(final String object) => 'json.decode($object as String)';
@override
TypeResultList get dartType => TypeResultList('List', subType, nullable: nullable);

28
packages/dynamite/dynamite/lib/src/type_result/map.dart

@ -10,33 +10,7 @@ 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;
@override
String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
}) {
switch (mimeType) {
case 'application/json':
return 'json.encode($object)';
case 'application/x-www-form-urlencoded':
return 'Uri(queryParameters: $object).query';
default:
throw Exception('Can not encode mime type "$mimeType"');
}
}
@override
String deserialize(final String object, {final bool toBuilder = false}) =>
'($object as $className<String, ${subType.name}>)${toBuilder ? '.toBuilder()' : ''}';
@override
String decode(final String object) => 'json.decode($object as String)';
String? get _builderFactory => '..addBuilderFactory($fullType, MapBuilder<String, ${subType.className}>.new)';
@override
TypeResultMap get dartType => TypeResultMap('Map', subType, nullable: nullable);

32
packages/dynamite/dynamite/lib/src/type_result/object.dart

@ -15,15 +15,6 @@ class TypeResultObject extends TypeResult {
@override
String? get _builderFactory => '..addBuilderFactory($fullType, $name.new)';
@override
String serialize(final String object) {
if (className == _contentString) {
return 'jsonSerializers.serialize(messages, specifiedType: const $fullType)';
}
return '$object.toJson()';
}
@override
String encode(
final String object, {
@ -32,28 +23,9 @@ class TypeResultObject extends TypeResult {
}) {
if (className == _contentString) {
assert(mimeType == 'application/json', '$_contentString should have a mimeType of application/json');
return object;
}
switch (mimeType) {
case 'application/json':
return 'json.encode($object)';
case 'application/x-www-form-urlencoded':
return 'Uri(queryParameters: $object).query';
default:
throw Exception('Can not encode mime type "$mimeType"');
}
return serialize(object);
}
@override
String deserialize(final String object, {final bool toBuilder = false}) {
if (className == 'ContentString') {
return 'jsonSerializers.deserialize(messages, specifiedType: const $fullType)! as $name${nullable ? '?' : ''}';
return super.encode(object, onlyChildren: onlyChildren, mimeType: mimeType);
}
return '$name.fromJson($object as Object${nullable ? '?' : ''})${nullable && toBuilder ? '?' : ''}${toBuilder ? '.toBuilder()' : ''}';
}
@override
String decode(final String object) => 'json.decode($object as String)';
}

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

@ -46,18 +46,39 @@ abstract class TypeResult {
String? get _builderFactory;
String serialize(final String object);
/// Serializes the variable named [object].
///
/// The serialized result is an [Object]?
String serialize(final String object) => 'jsonSerializers.serialize($object, specifiedType: const $fullType)';
String deserialize(final String object, {final bool toBuilder = false});
/// Deserializes the variable named [object].
///
/// The serialized result will be of [name].
String deserialize(final String object) =>
'(jsonSerializers.deserialize($object, specifiedType: const $fullType)! as $name)';
String decode(final String object);
/// Decodes the variable named [object].
String decode(final String object) => 'json.decode($object as String)';
/// Encodes the variable named [object].
String encode(
final String object, {
final bool onlyChildren = false,
final String? mimeType,
});
}) {
final serialized = serialize(object);
switch (mimeType) {
case 'application/json':
return 'json.encode($serialized)';
case 'application/x-www-form-urlencoded':
return 'Uri(queryParameters: $serialized! as Map<String, dynamic>).query';
default:
throw Exception('Can not encode mime type "$mimeType"');
}
}
/// Nullable TypeName
String get nullableName => nullable ? '$name?' : name;
/// Native dart type equivalent

Loading…
Cancel
Save