Browse Source

dynamite: support generics for TypeResult

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

2
packages/dynamite/dynamite/.gitignore vendored

@ -8,3 +8,5 @@ build/
# Omit committing pubspec.lock for library packages; see # Omit committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock. # https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock pubspec.lock
coverage/

1
packages/dynamite/dynamite/lib/dynamite.dart

@ -9,6 +9,7 @@ import 'package:dynamite/src/models/open_api.dart';
import 'package:dynamite/src/models/parameter.dart' as spec_parameter; import 'package:dynamite/src/models/parameter.dart' as spec_parameter;
import 'package:dynamite/src/models/path_item.dart'; import 'package:dynamite/src/models/path_item.dart';
import 'package:dynamite/src/models/schema.dart'; import 'package:dynamite/src/models/schema.dart';
import 'package:intersperse/intersperse.dart';
import 'package:path/path.dart' as p; import 'package:path/path.dart' as p;
part 'src/openapi_builder.dart'; part 'src/openapi_builder.dart';

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

@ -1671,7 +1671,7 @@ TypeResult resolveType(
result = TypeResultObject('${state.prefix}$identifier'); result = TypeResultObject('${state.prefix}$identifier');
} else if (schema.isContentString) { } else if (schema.isContentString) {
result = resolveType( final subResult = resolveType(
spec, spec,
state, state,
identifier, identifier,
@ -1679,7 +1679,7 @@ TypeResult resolveType(
); );
state.hasContentString = true; state.hasContentString = true;
result = TypeResultObject('ContentString<${result.name}>'); result = TypeResultObject('ContentString', generics: [subResult]);
} else { } else {
switch (schema.type) { switch (schema.type) {
case 'boolean': case 'boolean':
@ -1709,12 +1709,12 @@ TypeResult resolveType(
schema.items!, schema.items!,
); );
result = TypeResultList( result = TypeResultList(
'BuiltList<${subResult.name}>', 'BuiltList',
subResult, subResult,
); );
} else { } else {
result = TypeResultList( result = TypeResultList(
'BuiltList<JsonObject>', 'BuiltList',
TypeResultBase('JsonObject'), TypeResultBase('JsonObject'),
); );
} }
@ -1724,7 +1724,7 @@ TypeResult resolveType(
if (schema.additionalProperties != null) { if (schema.additionalProperties != null) {
if (schema.additionalProperties is EmptySchema) { if (schema.additionalProperties is EmptySchema) {
result = TypeResultMap( result = TypeResultMap(
'BuiltMap<String, Object?>', 'BuiltMap',
TypeResultBase('JsonObject'), TypeResultBase('JsonObject'),
); );
} else { } else {
@ -1735,8 +1735,8 @@ TypeResult resolveType(
schema.additionalProperties!, schema.additionalProperties!,
); );
result = TypeResultMap( result = TypeResultMap(
'BuiltMap<String, ${subResult.name}>', 'BuiltMap',
TypeResultBase('JsonObject'), subResult,
); );
} }
break; break;
@ -1746,7 +1746,7 @@ TypeResult resolveType(
} }
if (schema.properties!.isEmpty) { if (schema.properties!.isEmpty) {
result = TypeResultMap( result = TypeResultMap(
'BuiltMap<String, Object?>', 'BuiltMap',
TypeResultBase('JsonObject'), TypeResultBase('JsonObject'),
); );
break; break;

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

@ -3,10 +3,10 @@ part of '../../dynamite.dart';
class TypeResultList extends TypeResult { class TypeResultList extends TypeResult {
TypeResultList( TypeResultList(
super.name, super.name,
this.subType, final TypeResult subType,
); ) : super(generics: [subType]);
final TypeResult subType; TypeResult get subType => generics.first;
@override @override
String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')})'; String serialize(final String object) => '$object.map((final e) => ${subType.serialize('e')})';

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

@ -3,10 +3,10 @@ part of '../../dynamite.dart';
class TypeResultMap extends TypeResult { class TypeResultMap extends TypeResult {
TypeResultMap( TypeResultMap(
super.name, super.name,
this.subType, final TypeResult subType,
); ) : super(generics: [TypeResultBase('String'), subType]);
final TypeResult subType; TypeResult get subType => generics[1];
@override @override
String serialize(final String object) => object; String serialize(final String object) => object;
@ -29,7 +29,7 @@ class TypeResultMap extends TypeResult {
@override @override
String deserialize(final String object, {final bool toBuilder = false}) => String deserialize(final String object, {final bool toBuilder = false}) =>
'($object as BuiltMap<String, ${subType.name}>)${toBuilder ? '.toBuilder()' : ''}'; '($object as $className<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)';

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

@ -1,8 +1,13 @@
part of '../../dynamite.dart'; part of '../../dynamite.dart';
class TypeResultObject extends TypeResult { class TypeResultObject extends TypeResult {
TypeResultObject(super.name) TypeResultObject(
: assert(name != 'JsonObject' && name != 'Object' && name != 'dynamic', 'Use TypeResultBase instead'); super.className, {
super.generics,
}) : assert(
className != 'JsonObject' && className != 'Object' && className != 'dynamic',
'Use TypeResultBase instead',
);
@override @override
String serialize(final String object) => '$object.toJson()'; String serialize(final String object) => '$object.toJson()';

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

@ -1,9 +1,24 @@
part of '../../dynamite.dart'; part of '../../dynamite.dart';
abstract class TypeResult { abstract class TypeResult {
TypeResult(this.name); TypeResult(
this.className, {
this.generics = const [],
}) : assert(!className.contains('<'), 'Specifiy generics in the generics parameter.'),
assert(!className.contains('?'), 'Nullability should not be specified in the type.');
final String name; final String className;
final List<TypeResult> generics;
String get name {
if (generics.isNotEmpty) {
final buffer = StringBuffer('$className<')
..writeAll(generics.map((final c) => c.name).intersperse(', '))
..write('>');
return buffer.toString();
}
return className;
}
String serialize(final String object); String serialize(final String object);

1
packages/dynamite/dynamite/pubspec.yaml

@ -9,6 +9,7 @@ dependencies:
built_value: ^8.5.0 built_value: ^8.5.0
code_builder: ^4.4.0 code_builder: ^4.4.0
dart_style: ^2.3.1 dart_style: ^2.3.1
intersperse: ^2.0.0
json_annotation: ^4.8.1 json_annotation: ^4.8.1
path: ^1.8.3 path: ^1.8.3

39
packages/dynamite/dynamite/test/type_result_test.dart

@ -0,0 +1,39 @@
import 'package:dynamite/dynamite.dart';
import 'package:test/test.dart';
void main() {
group(TypeResultList, () {
test('name', () {
final subType = TypeResultBase('String');
final listType = TypeResultList('BuiltList', subType);
expect(listType.name, 'BuiltList<String>');
});
});
group(TypeResultMap, () {
test('name', () {
final subType = TypeResultBase('int');
final listType = TypeResultMap('BuiltMap', subType);
expect(listType.name, 'BuiltMap<String, int>');
});
});
group(TypeResultObject, () {
test('name', () {
final subType = TypeResultBase('String');
final listType = TypeResultObject('CustomType', generics: [subType]);
expect(listType.name, 'CustomType<String>');
});
});
group(TypeResultBase, () {
test('name', () {
final type = TypeResultBase('String');
expect(type.name, 'String');
});
});
}
Loading…
Cancel
Save