Skip to content

Commit

Permalink
Merge pull request #1129 from nextcloud/fix/dynamite/nested_built_col…
Browse files Browse the repository at this point in the history
…lection_builder
  • Loading branch information
Leptopoda authored Nov 13, 2023
2 parents d720dc8 + 49abaec commit b76745d
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class TypeResultBase extends TypeResult {
@override
String? get _serializer => null;

@override
String serialize(final String object) => object;

@override
String encode(
final String object, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class TypeResultEnum extends TypeResult {
@override
String? get _builderFactory => null;

@override
String? get _serializer => '..add($className.serializer)';

@override
String encode(
final String object, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ class TypeResultList extends TypeResult {
final TypeResult subType, {
super.nullable,
super.isTypeDef,
super.builderName = 'ListBuilder',
}) : super(generics: [subType]);

TypeResult get subType => generics.first;

@override
String? get _builderFactory => '..addBuilderFactory($fullType, ListBuilder<${subType.className}>.new)';

@override
String? get _serializer => null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ class TypeResultMap extends TypeResult {
final TypeResult subType, {
super.nullable,
super.isTypeDef,
super.builderName = 'MapBuilder',
}) : super(generics: [TypeResultBase('String'), subType]);

TypeResult get subType => generics[1];

@override
String? get _builderFactory => '..addBuilderFactory($fullType, MapBuilder<String, ${subType.className}>.new)';

@override
String? get _serializer => null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ class TypeResultObject extends TypeResult {
'Use TypeResultBase instead',
);

@override
String? get _builderFactory => '..addBuilderFactory($fullType, $name.new)';

@override
String? get _serializer => '..add($className.serializer)';

@override
String encode(
final String object, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:intersperse/intersperse.dart';
import 'package:meta/meta.dart';

part 'base.dart';
Expand All @@ -14,31 +13,48 @@ sealed class TypeResult {
this.generics = const [],
this.nullable = 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.');

final String className;
final String builderName;
final List<TypeResult> generics;
final bool nullable;

/// Whether this type should be represented as a typedef.
final bool isTypeDef;

@nonVirtual
String get name {
if (generics.isNotEmpty) {
final buffer = StringBuffer('$className<')
..writeAll(generics.map((final c) => c.name).intersperse(', '))
..writeAll(generics.map((final c) => c.name), ', ')
..write('>');
return buffer.toString();
}

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 {
if (generics.isNotEmpty) {
final buffer = StringBuffer('FullType($className, [')
..writeAll(generics.map((final c) => c.fullType).intersperse(', '))
..writeAll(generics.map((final c) => c.fullType), ', ')
..write('])');

return buffer.toString();
Expand All @@ -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].
///
Expand Down
101 changes: 101 additions & 0 deletions packages/dynamite/dynamite/test/type_result_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@ void main() {

expect(type.name, 'BuiltList<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>>)',
);
});
});

Expand All @@ -19,6 +55,47 @@ void main() {

expect(type.name, 'BuiltMap<String, 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>>)',
);
});
});

Expand All @@ -29,6 +106,21 @@ void main() {

expect(type.name, 'CustomType<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>)',
);
});
});

Expand All @@ -38,6 +130,15 @@ void main() {

expect(type.name, '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 changes: 1 addition & 1 deletion packages/nextcloud/lib/src/api/dashboard.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String),
FullType(BuiltList, [FullType(WidgetItem)]),
]),
MapBuilder<String, BuiltList>.new,
MapBuilder<String, BuiltList<WidgetItem>>.new,
)
..addBuilderFactory(
const FullType(DashboardApiGetWidgetItemsV2ResponseApplicationJson),
Expand Down
2 changes: 1 addition & 1 deletion packages/nextcloud/lib/src/api/files_sharing.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5186,7 +5186,7 @@ final Serializers _serializers = (Serializers().toBuilder()
const FullType(BuiltList, [
FullType(BuiltMap, [FullType(String), FullType(JsonObject)]),
]),
ListBuilder<BuiltMap>.new,
ListBuilder<BuiltMap<String, JsonObject>>.new,
)
..addBuilderFactory(
const FullType(ShareapiGetSharesResponseApplicationJson),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9148,7 +9148,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String),
FullType(BuiltList, [FullType(String)]),
]),
MapBuilder<String, BuiltList>.new,
MapBuilder<String, BuiltList<String>>.new,
)
..addBuilderFactory(
const FullType(ContentString, [
Expand Down
8 changes: 4 additions & 4 deletions packages/nextcloud/lib/src/api/spreed.openapi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23659,7 +23659,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String),
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(BuiltList, [FullType(JsonObject)]), ListBuilder<JsonObject>.new)
Expand Down Expand Up @@ -24136,7 +24136,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String),
FullType(BuiltList, [FullType(ChatMessage)]),
]),
MapBuilder<String, BuiltList>.new,
MapBuilder<String, BuiltList<ChatMessage>>.new,
)
..add(FederationAcceptShareApiVersion.serializer)
..addBuilderFactory(
Expand Down Expand Up @@ -24258,7 +24258,7 @@ final Serializers _serializers = (Serializers().toBuilder()
const FullType(BuiltList, [
FullType(BuiltMap, [FullType(String), FullType(JsonObject)]),
]),
ListBuilder<BuiltMap>.new,
ListBuilder<BuiltMap<String, JsonObject>>.new,
)
..addBuilderFactory(const FullType(MatterbridgeProcessState), MatterbridgeProcessState.new)
..add(MatterbridgeProcessState.serializer)
Expand Down Expand Up @@ -24411,7 +24411,7 @@ final Serializers _serializers = (Serializers().toBuilder()
FullType(String),
FullType(BuiltList, [FullType(Reaction)]),
]),
MapBuilder<String, BuiltList>.new,
MapBuilder<String, BuiltList<Reaction>>.new,
)
..add(ReactionReactApiVersion.serializer)
..addBuilderFactory(
Expand Down

0 comments on commit b76745d

Please sign in to comment.