Skip to content

Commit bbfc125

Browse files
committed
#151: added duplicate value check
1 parent 5cf17a4 commit bbfc125

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

example/analysis_options.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ analyzer:
33
missing_required_param: error
44
missing_return: error
55
todo: ignore
6-
sdk_version_async_exported_from_core: ignore
76
exclude:
87
- '**.g.dart'
98
- 'lib/util/locale/**'

example/lib/navigator/main_navigator.dart

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ class MainNavigatorWidgetState extends State<MainNavigatorWidget> {
3030

3131
@override
3232
Widget build(BuildContext context) {
33-
return WillPopScope(
34-
onWillPop: _willPop,
35-
child: Navigator(
36-
key: navigationKey,
37-
initialRoute: HomeScreen.routeName,
38-
onGenerateRoute: onGenerateRoute,
39-
),
33+
return Navigator(
34+
key: navigationKey,
35+
initialRoute: HomeScreen.routeName,
36+
onGenerateRoute: onGenerateRoute,
4037
);
4138
}
4239

@@ -50,14 +47,6 @@ class MainNavigatorWidgetState extends State<MainNavigatorWidget> {
5047
}
5148
}
5249

53-
Future<bool> _willPop() async {
54-
final currentState = navigationKey.currentState;
55-
if (currentState == null) {
56-
return true;
57-
}
58-
return !await currentState.maybePop();
59-
}
60-
6150
void closeDialog() => Navigator.of(context, rootNavigator: true).pop();
6251

6352
void goBack<T>({result}) => navigationKey.currentState?.pop(result);

example/model_generator/enums.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,12 @@ DoubleStatus:
7575
status_3:
7676
properties:
7777
value: 3.3
78+
79+
DuplicateEnum:
80+
path: status
81+
type: enum
82+
values:
83+
one: 1
84+
two: 2
85+
three: 3
86+
#four: 3 # Duplicate value throws error

lib/config/yml_generator_config.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ class YmlGeneratorConfig {
226226
);
227227
}
228228

229+
_checkDuplicateEnumValue(
230+
fields: fields,
231+
enumName: key,
232+
);
233+
229234
final enumModel = EnumModel(
230235
addJsonValueToProperties: value['use_default_json_value'] ?? true,
231236
generateExtension: value['generate_extension'] == true,
@@ -286,6 +291,24 @@ class YmlGeneratorConfig {
286291
});
287292
}
288293

294+
void _checkDuplicateEnumValue({
295+
required List<EnumField> fields,
296+
required String enumName,
297+
}) {
298+
final seenKeys = <String>{};
299+
300+
for (final field in fields) {
301+
final jsonValue = field.values
302+
.firstWhereOrNull((field) => field.propertyName == 'jsonValue');
303+
if (jsonValue == null) continue;
304+
final key = '${jsonValue.value}-${jsonValue.propertyName}';
305+
if (!seenKeys.add(key)) {
306+
throw Exception(
307+
'Duplicate jsonValue ${jsonValue.value} found on field ${field.name} on enum $enumName');
308+
}
309+
}
310+
}
311+
289312
Field getField(String name, YamlMap property,
290313
{required bool disallowNullForDefaults}) {
291314
try {

test/writer/enum_model_reader_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,21 @@ Gender:
388388
values:
389389
MALE: 1
390390
FEMALE: female
391+
""",
392+
));
393+
394+
test(
395+
'Enum cant have duplicate json value',
396+
() => testEnumError(
397+
expectedError:
398+
'Exception: Duplicate jsonValue 1 found on field female on enum Gender',
399+
enumYml: """
400+
Gender:
401+
path: user/person/
402+
type: enum
403+
values:
404+
MALE: 1
405+
FEMALE: 1
391406
""",
392407
));
393408
});

0 commit comments

Comments
 (0)