Browse Source

Merge pull request #1129 from nextcloud/fix/dynamite/nested_built_collection_builder

pull/1136/head
Nikolas Rimikis 1 year ago committed by GitHub
parent
commit
b76745d9ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      packages/dynamite/dynamite/lib/src/models/type_result/base.dart
  2. 3
      packages/dynamite/dynamite/lib/src/models/type_result/enum.dart
  3. 4
      packages/dynamite/dynamite/lib/src/models/type_result/list.dart
  4. 4
      packages/dynamite/dynamite/lib/src/models/type_result/map.dart
  5. 6
      packages/dynamite/dynamite/lib/src/models/type_result/object.dart
  6. 28
      packages/dynamite/dynamite/lib/src/models/type_result/type_result.dart
  7. 101
      packages/dynamite/dynamite/test/type_result_test.dart
  8. 2
      packages/nextcloud/lib/src/api/dashboard.openapi.dart
  9. 2
      packages/nextcloud/lib/src/api/files_sharing.openapi.dart
  10. 2
      packages/nextcloud/lib/src/api/provisioning_api.openapi.dart
  11. 8
      packages/nextcloud/lib/src/api/spreed.openapi.dart

3
packages/dynamite/dynamite/lib/src/models/type_result/base.dart

@ -14,9 +14,6 @@ class TypeResultBase extends TypeResult {
@override @override
String? get _serializer => null; String? get _serializer => null;
@override
String serialize(final String object) => object;
@override @override
String encode( String encode(
final String object, { final String object, {

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

@ -14,9 +14,6 @@ class TypeResultEnum extends TypeResult {
@override @override
String? get _builderFactory => null; String? get _builderFactory => null;
@override
String? get _serializer => '..add($className.serializer)';
@override @override
String encode( String encode(
final String object, { final String object, {

4
packages/dynamite/dynamite/lib/src/models/type_result/list.dart

@ -7,13 +7,11 @@ class TypeResultList extends TypeResult {
final TypeResult subType, { final TypeResult subType, {
super.nullable, super.nullable,
super.isTypeDef, super.isTypeDef,
super.builderName = 'ListBuilder',
}) : super(generics: [subType]); }) : super(generics: [subType]);
TypeResult get subType => generics.first; TypeResult get subType => generics.first;
@override
String? get _builderFactory => '..addBuilderFactory($fullType, ListBuilder<${subType.className}>.new)';
@override @override
String? get _serializer => null; String? get _serializer => null;

4
packages/dynamite/dynamite/lib/src/models/type_result/map.dart

@ -7,13 +7,11 @@ class TypeResultMap extends TypeResult {
final TypeResult subType, { final TypeResult subType, {
super.nullable, super.nullable,
super.isTypeDef, super.isTypeDef,
super.builderName = 'MapBuilder',
}) : super(generics: [TypeResultBase('String'), subType]); }) : super(generics: [TypeResultBase('String'), subType]);
TypeResult get subType => generics[1]; TypeResult get subType => generics[1];
@override
String? get _builderFactory => '..addBuilderFactory($fullType, MapBuilder<String, ${subType.className}>.new)';
@override @override
String? get _serializer => null; String? get _serializer => null;

6
packages/dynamite/dynamite/lib/src/models/type_result/object.dart

@ -14,12 +14,6 @@ class TypeResultObject extends TypeResult {
'Use TypeResultBase instead', 'Use TypeResultBase instead',
); );
@override
String? get _builderFactory => '..addBuilderFactory($fullType, $name.new)';
@override
String? get _serializer => '..add($className.serializer)';
@override @override
String encode( String encode(
final String object, { final String object, {

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

@ -1,4 +1,3 @@
import 'package:intersperse/intersperse.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
part 'base.dart'; part 'base.dart';
@ -14,20 +13,24 @@ sealed class TypeResult {
this.generics = const [], this.generics = const [],
this.nullable = false, this.nullable = false,
this.isTypeDef = false, this.isTypeDef = false,
}) : assert(!className.contains('<'), 'Specify generics in the generics parameter.'), final String? builderName,
}) : builderName = builderName ?? className,
assert(!className.contains('<'), 'Specify generics in the generics parameter.'),
assert(!className.contains('?'), 'Nullability should not be specified in the type.'); assert(!className.contains('?'), 'Nullability should not be specified in the type.');
final String className; final String className;
final String builderName;
final List<TypeResult> generics; final List<TypeResult> generics;
final bool nullable; final bool nullable;
/// Whether this type should be represented as a typedef. /// Whether this type should be represented as a typedef.
final bool isTypeDef; final bool isTypeDef;
@nonVirtual
String get name { String get name {
if (generics.isNotEmpty) { if (generics.isNotEmpty) {
final buffer = StringBuffer('$className<') final buffer = StringBuffer('$className<')
..writeAll(generics.map((final c) => c.name).intersperse(', ')) ..writeAll(generics.map((final c) => c.name), ', ')
..write('>'); ..write('>');
return buffer.toString(); return buffer.toString();
} }
@ -35,10 +38,23 @@ sealed class TypeResult {
return className; return className;
} }
@nonVirtual
String get builder {
if (generics.isNotEmpty) {
final buffer = StringBuffer('$builderName<')
..writeAll(generics.map((final c) => c.name), ', ')
..write('>');
return buffer.toString();
}
return builderName;
}
@nonVirtual
String get fullType { String get fullType {
if (generics.isNotEmpty) { if (generics.isNotEmpty) {
final buffer = StringBuffer('FullType($className, [') final buffer = StringBuffer('FullType($className, [')
..writeAll(generics.map((final c) => c.fullType).intersperse(', ')) ..writeAll(generics.map((final c) => c.fullType), ', ')
..write('])'); ..write('])');
return buffer.toString(); return buffer.toString();
@ -64,9 +80,9 @@ sealed class TypeResult {
} }
} }
String? get _serializer; String? get _serializer => '..add($className.serializer)';
String? get _builderFactory; String? get _builderFactory => '..addBuilderFactory($fullType, $builder.new)';
/// Serializes the variable named [object]. /// Serializes the variable named [object].
/// ///

101
packages/dynamite/dynamite/test/type_result_test.dart

@ -9,6 +9,42 @@ void main() {
expect(type.name, 'BuiltList<String>'); expect(type.name, 'BuiltList<String>');
expect(type.fullType, 'FullType(BuiltList, [FullType(String)])'); expect(type.fullType, 'FullType(BuiltList, [FullType(String)])');
expect(
type.serializers.toList(),
const ['..addBuilderFactory(FullType(BuiltList, [FullType(String)]), ListBuilder<String>.new)'],
);
expect(
type.serialize('value'),
'_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>)',
);
});
test('Nested list', () {
final subType = TypeResultBase('String');
var type = TypeResultList('BuiltList', subType);
type = TypeResultList('BuiltList', type);
expect(type.name, 'BuiltList<BuiltList<String>>');
expect(type.fullType, 'FullType(BuiltList, [FullType(BuiltList, [FullType(String)])])');
expect(
type.serializers.toList(),
const [
'..addBuilderFactory(FullType(BuiltList, [FullType(String)]), ListBuilder<String>.new)',
'..addBuilderFactory(FullType(BuiltList, [FullType(BuiltList, [FullType(String)])]), ListBuilder<BuiltList<String>>.new)',
],
);
expect(
type.serialize('value'),
'_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>>)',
);
}); });
}); });
@ -19,6 +55,47 @@ void main() {
expect(type.name, 'BuiltMap<String, int>'); expect(type.name, 'BuiltMap<String, int>');
expect(type.fullType, 'FullType(BuiltMap, [FullType(String), FullType(int)])'); expect(type.fullType, 'FullType(BuiltMap, [FullType(String), FullType(int)])');
expect(
type.serializers.toList(),
const [
'..addBuilderFactory(FullType(BuiltMap, [FullType(String), FullType(int)]), MapBuilder<String, int>.new)',
],
);
expect(
type.serialize('value'),
'_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>)',
);
});
test('Nested map', () {
final subType = TypeResultBase('int');
var type = TypeResultMap('BuiltMap', subType);
type = TypeResultMap('BuiltMap', type);
expect(type.name, 'BuiltMap<String, BuiltMap<String, int>>');
expect(
type.fullType,
'FullType(BuiltMap, [FullType(String), FullType(BuiltMap, [FullType(String), FullType(int)])])',
);
expect(
type.serializers.toList(),
const [
'..addBuilderFactory(FullType(BuiltMap, [FullType(String), FullType(int)]), MapBuilder<String, int>.new)',
'..addBuilderFactory(FullType(BuiltMap, [FullType(String), FullType(BuiltMap, [FullType(String), FullType(int)])]), MapBuilder<String, BuiltMap<String, int>>.new)',
],
);
expect(
type.serialize('value'),
'_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>>)',
);
}); });
}); });
@ -29,6 +106,21 @@ void main() {
expect(type.name, 'CustomType<String>'); expect(type.name, 'CustomType<String>');
expect(type.fullType, 'FullType(CustomType, [FullType(String)])'); expect(type.fullType, 'FullType(CustomType, [FullType(String)])');
expect(
type.serializers.toList(),
const [
'..addBuilderFactory(FullType(CustomType, [FullType(String)]), CustomType<String>.new)',
'..add(CustomType.serializer)',
],
);
expect(
type.serialize('value'),
'_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>)',
);
}); });
}); });
@ -38,6 +130,15 @@ void main() {
expect(type.name, 'String'); expect(type.name, 'String');
expect(type.fullType, 'FullType(String)'); expect(type.fullType, 'FullType(String)');
expect(type.serializers.toList(), const <String>[]);
expect(
type.serialize('value'),
'_jsonSerializers.serialize(value, specifiedType: const FullType(String))',
);
expect(
type.deserialize('value'),
'(_jsonSerializers.deserialize(value, specifiedType: const FullType(String))! as String)',
);
}); });
}); });
} }

2
packages/nextcloud/lib/src/api/dashboard.openapi.dart

@ -738,7 +738,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String), FullType(String),
FullType(BuiltList, [FullType(WidgetItem)]), FullType(BuiltList, [FullType(WidgetItem)]),
]), ]),
MapBuilder<String, BuiltList>.new, MapBuilder<String, BuiltList<WidgetItem>>.new,
) )
..addBuilderFactory( ..addBuilderFactory(
const FullType(DashboardApiGetWidgetItemsV2ResponseApplicationJson), const FullType(DashboardApiGetWidgetItemsV2ResponseApplicationJson),

2
packages/nextcloud/lib/src/api/files_sharing.openapi.dart

@ -5186,7 +5186,7 @@ final Serializers _serializers = (Serializers().toBuilder()
const FullType(BuiltList, [ const FullType(BuiltList, [
FullType(BuiltMap, [FullType(String), FullType(JsonObject)]), FullType(BuiltMap, [FullType(String), FullType(JsonObject)]),
]), ]),
ListBuilder<BuiltMap>.new, ListBuilder<BuiltMap<String, JsonObject>>.new,
) )
..addBuilderFactory( ..addBuilderFactory(
const FullType(ShareapiGetSharesResponseApplicationJson), const FullType(ShareapiGetSharesResponseApplicationJson),

2
packages/nextcloud/lib/src/api/provisioning_api.openapi.dart

@ -9148,7 +9148,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String), FullType(String),
FullType(BuiltList, [FullType(String)]), FullType(BuiltList, [FullType(String)]),
]), ]),
MapBuilder<String, BuiltList>.new, MapBuilder<String, BuiltList<String>>.new,
) )
..addBuilderFactory( ..addBuilderFactory(
const FullType(ContentString, [ const FullType(ContentString, [

8
packages/nextcloud/lib/src/api/spreed.openapi.dart

@ -23659,7 +23659,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String), FullType(String),
FullType(BuiltMap, [FullType(String), FullType(JsonObject)]), FullType(BuiltMap, [FullType(String), FullType(JsonObject)]),
]), ]),
MapBuilder<String, BuiltMap>.new, MapBuilder<String, BuiltMap<String, JsonObject>>.new,
) )
..addBuilderFactory(const FullType(BuiltMap, [FullType(String), FullType(int)]), MapBuilder<String, int>.new) ..addBuilderFactory(const FullType(BuiltMap, [FullType(String), FullType(int)]), MapBuilder<String, int>.new)
..addBuilderFactory(const FullType(BuiltList, [FullType(JsonObject)]), ListBuilder<JsonObject>.new) ..addBuilderFactory(const FullType(BuiltList, [FullType(JsonObject)]), ListBuilder<JsonObject>.new)
@ -24136,7 +24136,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String), FullType(String),
FullType(BuiltList, [FullType(ChatMessage)]), FullType(BuiltList, [FullType(ChatMessage)]),
]), ]),
MapBuilder<String, BuiltList>.new, MapBuilder<String, BuiltList<ChatMessage>>.new,
) )
..add(FederationAcceptShareApiVersion.serializer) ..add(FederationAcceptShareApiVersion.serializer)
..addBuilderFactory( ..addBuilderFactory(
@ -24258,7 +24258,7 @@ final Serializers _serializers = (Serializers().toBuilder()
const FullType(BuiltList, [ const FullType(BuiltList, [
FullType(BuiltMap, [FullType(String), FullType(JsonObject)]), FullType(BuiltMap, [FullType(String), FullType(JsonObject)]),
]), ]),
ListBuilder<BuiltMap>.new, ListBuilder<BuiltMap<String, JsonObject>>.new,
) )
..addBuilderFactory(const FullType(MatterbridgeProcessState), MatterbridgeProcessState.new) ..addBuilderFactory(const FullType(MatterbridgeProcessState), MatterbridgeProcessState.new)
..add(MatterbridgeProcessState.serializer) ..add(MatterbridgeProcessState.serializer)
@ -24411,7 +24411,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String), FullType(String),
FullType(BuiltList, [FullType(Reaction)]), FullType(BuiltList, [FullType(Reaction)]),
]), ]),
MapBuilder<String, BuiltList>.new, MapBuilder<String, BuiltList<Reaction>>.new,
) )
..add(ReactionReactApiVersion.serializer) ..add(ReactionReactApiVersion.serializer)
..addBuilderFactory( ..addBuilderFactory(

Loading…
Cancel
Save