From bbfc1259324b43faf7973ed91061c0acf34adcb4 Mon Sep 17 00:00:00 2001 From: Jordy de Jonghe Date: Thu, 13 Feb 2025 15:41:49 +0100 Subject: [PATCH] #151: added duplicate value check --- example/analysis_options.yaml | 1 - example/lib/navigator/main_navigator.dart | 19 ++++--------------- example/model_generator/enums.yaml | 9 +++++++++ lib/config/yml_generator_config.dart | 23 +++++++++++++++++++++++ test/writer/enum_model_reader_test.dart | 15 +++++++++++++++ 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/example/analysis_options.yaml b/example/analysis_options.yaml index d154700..4546fb3 100644 --- a/example/analysis_options.yaml +++ b/example/analysis_options.yaml @@ -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/**' diff --git a/example/lib/navigator/main_navigator.dart b/example/lib/navigator/main_navigator.dart index c1608ae..4ca94c5 100644 --- a/example/lib/navigator/main_navigator.dart +++ b/example/lib/navigator/main_navigator.dart @@ -30,13 +30,10 @@ class MainNavigatorWidgetState extends State { @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, ); } @@ -50,14 +47,6 @@ class MainNavigatorWidgetState extends State { } } - Future _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({result}) => navigationKey.currentState?.pop(result); diff --git a/example/model_generator/enums.yaml b/example/model_generator/enums.yaml index beed7ea..2c90c1e 100644 --- a/example/model_generator/enums.yaml +++ b/example/model_generator/enums.yaml @@ -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 \ No newline at end of file diff --git a/lib/config/yml_generator_config.dart b/lib/config/yml_generator_config.dart index 0522ad9..4ea7dd0 100644 --- a/lib/config/yml_generator_config.dart +++ b/lib/config/yml_generator_config.dart @@ -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, @@ -286,6 +291,24 @@ class YmlGeneratorConfig { }); } + void _checkDuplicateEnumValue({ + required List fields, + required String enumName, + }) { + final seenKeys = {}; + + 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 { diff --git a/test/writer/enum_model_reader_test.dart b/test/writer/enum_model_reader_test.dart index 3bbb867..769b329 100644 --- a/test/writer/enum_model_reader_test.dart +++ b/test/writer/enum_model_reader_test.dart @@ -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 """, )); });