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

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

@ -15,7 +15,12 @@ class TypeResultBase extends TypeResult {
name == 'String' ? object : '$object.toString()'; name == 'String' ? object : '$object.toString()';
@override @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 @override
String decode(final String object) { String decode(final String object) {
@ -24,6 +29,8 @@ class TypeResultBase extends TypeResult {
return '($object as String)'; return '($object as String)';
case 'int': case 'int':
return 'int.parse($object as String)'; return 'int.parse($object as String)';
case 'JsonObject':
return 'JsonObject($object)';
default: default:
throw Exception('Can not decode "$name" from String'); 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); subType.encode(object);
@override @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 @override
String decode(final String object) => subType.decode(object); 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; final bool fromContentString;
@override @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 @override
String encode( String encode(
@ -20,7 +20,7 @@ class TypeResultList extends TypeResult {
final String? mimeType, final String? mimeType,
}) { }) {
if (onlyChildren) { 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) { switch (mimeType) {
@ -34,7 +34,8 @@ class TypeResultList extends TypeResult {
} }
@override @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 @override
String decode(final String object) => 'json.decode($object as String)'; 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 @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 @override
String decode(final String object) => 'json.decode($object as String)'; 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( TypeResultObject(
super.name, { super.name, {
this.fromContentString = false, this.fromContentString = false,
}); }) : assert(name != 'JsonObject' && name != 'Object' && name != 'dynamic', 'Use TypeResultBase instead');
final bool fromContentString; final bool fromContentString;
@ -33,11 +33,11 @@ class TypeResultObject extends TypeResult {
} }
@override @override
String deserialize(final String object) { String deserialize(final String object, {final bool toBuilder = false}) {
if (fromContentString) { 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 @override

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

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

Loading…
Cancel
Save