Skip to content

#151: added duplicate value check #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ analyzer:
missing_required_param: error
missing_return: error
todo: ignore
sdk_version_async_exported_from_core: ignore
exclude:
- '**.g.dart'
- 'lib/util/locale/**'
Expand Down
19 changes: 4 additions & 15 deletions example/lib/navigator/main_navigator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,10 @@ class MainNavigatorWidgetState extends State<MainNavigatorWidget> {

@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: _willPop,
child: Navigator(
key: navigationKey,
initialRoute: HomeScreen.routeName,
onGenerateRoute: onGenerateRoute,
),
return Navigator(
key: navigationKey,
initialRoute: HomeScreen.routeName,
onGenerateRoute: onGenerateRoute,
);
}

Expand All @@ -50,14 +47,6 @@ class MainNavigatorWidgetState extends State<MainNavigatorWidget> {
}
}

Future<bool> _willPop() async {
final currentState = navigationKey.currentState;
if (currentState == null) {
return true;
}
return !await currentState.maybePop();
}

void closeDialog() => Navigator.of(context, rootNavigator: true).pop();

void goBack<T>({result}) => navigationKey.currentState?.pop(result);
Expand Down
9 changes: 9 additions & 0 deletions example/model_generator/enums.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ DoubleStatus:
status_3:
properties:
value: 3.3

DuplicateEnum:
path: status
type: enum
values:
one: 1
two: 2
three: 3
#four: 3 # Duplicate value throws error
23 changes: 23 additions & 0 deletions lib/config/yml_generator_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ class YmlGeneratorConfig {
);
}

_checkDuplicateEnumValue(
fields: fields,
enumName: key,
);

final enumModel = EnumModel(
addJsonValueToProperties: value['use_default_json_value'] ?? true,
generateExtension: value['generate_extension'] == true,
Expand Down Expand Up @@ -286,6 +291,24 @@ class YmlGeneratorConfig {
});
}

void _checkDuplicateEnumValue({
required List<EnumField> fields,
required String enumName,
}) {
final seenKeys = <String>{};

for (final field in fields) {
final jsonValue = field.values
.firstWhereOrNull((field) => field.propertyName == 'jsonValue');
if (jsonValue == null) continue;
final key = '${jsonValue.value}-${jsonValue.propertyName}';
if (!seenKeys.add(key)) {
throw Exception(
'Duplicate jsonValue ${jsonValue.value} found on field ${field.name} on enum $enumName');
}
}
}

Field getField(String name, YamlMap property,
{required bool disallowNullForDefaults}) {
try {
Expand Down
15 changes: 15 additions & 0 deletions test/writer/enum_model_reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,21 @@ Gender:
values:
MALE: 1
FEMALE: female
""",
));

test(
'Enum cant have duplicate json value',
() => testEnumError(
expectedError:
'Exception: Duplicate jsonValue 1 found on field female on enum Gender',
enumYml: """
Gender:
path: user/person/
type: enum
values:
MALE: 1
FEMALE: 1
""",
));
});
Expand Down
Loading