Skip to content

Commit

Permalink
test: add ListNode tests checking generic type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Tienisto committed Dec 9, 2021
1 parent d547ec8 commit 9886d29
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 5.6.0

- feat: add CSV support
- feat: improve generic type detection for lists (e.g. `List<List<String>>`,`List<Map<String,String>>`)
- feat: improve generic type detection for lists (e.g. `List<List<String>>`,`List<Map<String, String>>`)
- feat: make optional class members (from interfaces) non-nullable when possible

## 5.5.0
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -407,7 +407,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
watcher:
dependency: transitive
description:
Expand Down
12 changes: 2 additions & 10 deletions lib/src/builder/translation_model_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -572,21 +572,13 @@ class TranslationModelBuilder {
}).toSet();
} else if (child is ListNode) {
parameters = {}; // lists never have parameters
if (child.genericType != null) {
returnType = 'List<${child.genericType}>';
} else {
returnType = 'List<dynamic>';
}
returnType = 'List<${child.genericType}>';
} else if (child is ObjectNode) {
switch (child.type) {
case ObjectNodeType.classType:
case ObjectNodeType.map:
parameters = {}; // objects never have parameters
if (child.genericType != null) {
returnType = 'Map<String, ${child.genericType}>';
} else {
returnType = 'Map<String, dynamic>';
}
returnType = 'Map<String, ${child.genericType}>';
break;
case ObjectNodeType.pluralCardinal:
case ObjectNodeType.pluralOrdinal:
Expand Down
8 changes: 3 additions & 5 deletions lib/src/generator/generate_translations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ void _generateClass(
'String$optional $key${_toParameterList(value.params, value.paramTypeMap)} => \'${value.content}\';');
}
} else if (value is ListNode) {
String type = value.genericType != null ? value.genericType! : 'dynamic';
buffer.write('List<$type>$optional get $key => ');
buffer.write('List<${value.genericType}>$optional get $key => ');
_generateList(config, base, locale, hasPluralResolver, buffer, queue,
className, value.entries, 0);
} else if (value is ObjectNode) {
Expand All @@ -146,9 +145,8 @@ void _generateClass(
break;
case ObjectNodeType.map:
// inline map
String type =
value.genericType != null ? value.genericType! : 'dynamic';
buffer.write('Map<String, $type>$optional get $key => ');
buffer
.write('Map<String, ${value.genericType}>$optional get $key => ');
_generateMap(config, base, locale, hasPluralResolver, buffer, queue,
childClassNoLocale, value.entries, 0);
break;
Expand Down
12 changes: 6 additions & 6 deletions lib/src/model/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ abstract class Node {
abstract class IterableNode extends Node {
/// If not null, then all its children have a specific interface.
/// This overwrites the [plainStrings] attribute.
String? _genericType;
String? get genericType => _genericType;
String _genericType;
String get genericType => _genericType;

IterableNode(String path, this._genericType) : super(path);

Expand Down Expand Up @@ -61,7 +61,7 @@ class ObjectNode extends IterableNode {
entries.values
.every((child) => child is TextNode && child.params.isEmpty)
? 'String'
: null);
: 'dynamic');

void setInterface(Interface interface) {
_interface = interface;
Expand All @@ -77,7 +77,7 @@ class ListNode extends IterableNode {
ListNode({required String path, required this.entries})
: super(path, _determineGenericType(entries));

static String? _determineGenericType(List<Node> entries) {
static String _determineGenericType(List<Node> entries) {
if (entries.every((child) => child is TextNode && child.params.isEmpty)) {
return 'String';
}
Expand All @@ -98,9 +98,9 @@ class ListNode extends IterableNode {
childGenericType = 'dynamic'; // default
}
}
return 'Map<String,$childGenericType>';
return 'Map<String, $childGenericType>';
}
return null;
return 'dynamic';
}

@override
Expand Down
14 changes: 7 additions & 7 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -98,7 +98,7 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
Expand Down Expand Up @@ -276,7 +276,7 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -435,21 +435,21 @@ packages:
name: test
url: "https://pub.dartlang.org"
source: hosted
version: "1.17.10"
version: "1.17.12"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
version: "0.4.3"
test_core:
dependency: transitive
description:
name: test_core
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.0"
version: "0.4.2"
timing:
dependency: transitive
description:
Expand All @@ -470,7 +470,7 @@ packages:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.1"
vm_service:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions test/integration/resources/expected.output
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ mixin PageData {

mixin EndData {
List<String> get stringPages;
List<Map<String,String>> get pages;
List<Map<String, String>> get pages;
}

// extensions for AppLocale
Expand Down Expand Up @@ -363,7 +363,7 @@ class _TranslationsEndEn with EndData {
'1st Page',
'2nd Page',
];
@override List<Map<String,String>> get pages => [
@override List<Map<String, String>> get pages => [
{
'unknown': 'Unknown Error',
},
Expand Down Expand Up @@ -450,7 +450,7 @@ class _TranslationsEndDe with EndData implements _TranslationsEndEn {
'1. Seite',
'2. Seite',
];
@override List<Map<String,String>> get pages => [
@override List<Map<String, String>> get pages => [
{
'unknown': 'Unbekannter Fehler',
},
Expand Down
Loading

0 comments on commit 9886d29

Please sign in to comment.