Browse Source

dynamite: migrate types to built_value

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

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

@ -65,6 +65,7 @@ class OpenAPIBuilder implements Builder {
'',
"import 'package:built_collection/built_collection.dart';",
"import 'package:built_value/built_value.dart';",
"import 'package:built_value/json_object.dart';",
"import 'package:built_value/serializer.dart';",
"import 'package:built_value/standard_json_plugin.dart';",
"import 'package:cookie_jar/cookie_jar.dart';",
@ -1520,7 +1521,7 @@ TypeResult resolveType(
}) {
TypeResult? result;
if (schema.ref == null && schema.ofs == null && schema.type == null) {
return TypeResultBase('dynamic');
return TypeResultBase('JsonObject');
}
if (schema.ref != null) {
final name = schema.ref!.split('/').last;
@ -1759,14 +1760,14 @@ TypeResult resolveType(
extraJsonSerializableValues: extraJsonSerializableValues,
);
result = TypeResultList(
'List<${subResult.name}>',
'BuiltList<${subResult.name}>',
subResult,
fromContentString: fromContentString,
);
} else {
result = TypeResultList(
'List',
TypeResultBase('dynamic'),
'BuiltList<JsonObject>',
TypeResultBase('JsonObject'),
);
}
break;
@ -1775,8 +1776,8 @@ TypeResult resolveType(
if (schema.additionalProperties != null) {
if (schema.additionalProperties is EmptySchema) {
result = TypeResultMap(
'Map<String, dynamic>',
TypeResultBase('dynamic'),
'BuiltMap<String, Object?>',
TypeResultBase('JsonObject'),
);
} else {
final subResult = resolveType(
@ -1787,19 +1788,19 @@ TypeResult resolveType(
extraJsonSerializableValues: extraJsonSerializableValues,
);
result = TypeResultMap(
'Map<String, ${subResult.name}>',
TypeResultBase('dynamic'),
'BuiltMap<String, ${subResult.name}>',
TypeResultBase('JsonObject'),
);
}
break;
}
result = TypeResultBase('dynamic');
result = TypeResultBase('JsonObject');
break;
}
if (schema.properties!.isEmpty) {
result = TypeResultMap(
'Map<String, dynamic>',
TypeResultBase('dynamic'),
'BuiltMap<String, Object?>',
TypeResultBase('JsonObject'),
);
break;
}

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

@ -15,7 +15,12 @@ class TypeResultBase extends TypeResult {
name == 'String' ? object : '$object.toString()';
@override
String deserialize(final String object) => '($object as $name)';
String deserialize(final String object, {final bool toBuilder = false}) {
if (name == 'JsonObject') {
return 'JsonObject($object)';
}
return '($object as $name)';
}
@override
String decode(final String object) {
@ -24,6 +29,8 @@ class TypeResultBase extends TypeResult {
return '($object as String)';
case 'int':
return 'int.parse($object as String)';
case 'JsonObject':
return 'JsonObject($object)';
default:
throw Exception('Can not decode "$name" from String');
}

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

@ -20,7 +20,8 @@ class TypeResultEnum extends TypeResult {
subType.encode(object);
@override
String deserialize(final String object) => '$name.valueOf($object as ${subType.name})';
String deserialize(final String object, {final bool toBuilder = false}) =>
'$name.valueOf($object as ${subType.name})';
@override
String decode(final String object) => subType.decode(object);

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

@ -11,7 +11,7 @@ class TypeResultList extends TypeResult {
final bool fromContentString;
@override
String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')}).toList()';
String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')})';
@override
String encode(
@ -20,7 +20,7 @@ class TypeResultList extends TypeResult {
final String? mimeType,
}) {
if (onlyChildren) {
return '$object.map((final e) => ${subType.encode('e', mimeType: mimeType)}).toList()';
return '$object.map((final e) => ${subType.encode('e', mimeType: mimeType)})';
}
switch (mimeType) {
@ -34,7 +34,8 @@ class TypeResultList extends TypeResult {
}
@override
String deserialize(final String object) => '($object as List).map((final e) => ${subType.deserialize('e')}).toList()';
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)';

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

@ -28,7 +28,8 @@ class TypeResultMap extends TypeResult {
}
@override
String deserialize(final String object) => '($object as Map<String, ${subType.name}>)';
String deserialize(final String object, {final bool toBuilder = false}) =>
'($object as BuiltMap<String, ${subType.name}>)${toBuilder ? '.toBuilder()' : ''}';
@override
String decode(final String object) => 'json.decode($object as String)';

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

@ -4,7 +4,7 @@ class TypeResultObject extends TypeResult {
TypeResultObject(
super.name, {
this.fromContentString = false,
});
}) : assert(name != 'JsonObject' && name != 'Object' && name != 'dynamic', 'Use TypeResultBase instead');
final bool fromContentString;
@ -33,11 +33,11 @@ class TypeResultObject extends TypeResult {
}
@override
String deserialize(final String object) {
String deserialize(final String object, {final bool toBuilder = false}) {
if (fromContentString) {
return '$name.fromJsonString($object as String)';
return '$name.fromJsonString($object as String)${toBuilder ? '.toBuilder()' : ''}';
}
return '$name.fromJson($object as Map<String, dynamic>)';
return '$name.fromJson($object as Map<String, dynamic>)${toBuilder ? '.toBuilder()' : ''}';
}
@override

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

@ -7,7 +7,7 @@ abstract class TypeResult {
String serialize(final String object);
String deserialize(final String object);
String deserialize(final String object, {final bool toBuilder = false});
String decode(final String object);

Loading…
Cancel
Save