Skip to content

Commit a490e78

Browse files
committed
add DuplicatePropertyNameChecker
1 parent 42d0e35 commit a490e78

File tree

8 files changed

+55
-43
lines changed

8 files changed

+55
-43
lines changed

Flutter/json_to_dart/lib/l10n/app_en.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@
5151
}
5252
},
5353
"propertyCantSameAsType": "property can't the same as Type",
54-
"containsIllegalCharacters": "contains illegal characters"
54+
"containsIllegalCharacters": "contains illegal characters",
55+
"duplicateProperties":"There are duplicate properties"
5556
}

Flutter/json_to_dart/lib/l10n/app_zh.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@
5151
}
5252
},
5353
"propertyCantSameAsType": "属性名不能跟类型相同",
54-
"containsIllegalCharacters": "包含非法字符"
54+
"containsIllegalCharacters": "包含非法字符",
55+
"duplicateProperties":"属性名重复"
5556
}

Flutter/json_to_dart/lib/l10n/app_zh_Hant.arb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@
5151
}
5252
},
5353
"propertyCantSameAsType": "屬性名不能跟類型相同",
54-
"containsIllegalCharacters": "包含非法字符"
54+
"containsIllegalCharacters": "包含非法字符",
55+
"duplicateProperties":"屬性名重複"
5556
}

Flutter/json_to_dart/lib/main.dart

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,7 @@ class MyApp extends StatelessWidget {
3636
localizationsDelegates: AppLocalizations.localizationsDelegates,
3737
supportedLocales: AppLocalizations.supportedLocales,
3838
locale: ConfigSetting().locale.value,
39-
)
40-
41-
// MultiProvider(
42-
// providers: <SingleChildWidget>[
43-
// ChangeNotifierProvider<JsonToDartController>.value(
44-
// value: controller,
45-
// ),
46-
// ChangeNotifierProvider<ConfigSetting>.value(
47-
// value: ConfigSetting(),
48-
// )
49-
// ],
50-
// child: Selector<ConfigSetting, Locale>(
51-
// selector: (BuildContext c, ConfigSetting vm) => vm.locale,
52-
// builder: (BuildContext c, Locale value, Widget? child) {
53-
// return MaterialApp(
54-
// debugShowCheckedModeBanner: false,
55-
// title: 'Json To Dart',
56-
// theme: ThemeData(
57-
// primarySwatch: Colors.blue,
58-
// ),
59-
// navigatorKey: AppNavigator().key,
60-
// home: const MyHomePage(title: 'Json To Dart'),
61-
// localizationsDelegates: AppLocalizations.localizationsDelegates,
62-
// supportedLocales: AppLocalizations.supportedLocales,
63-
// locale: ConfigSetting().locale,
64-
// );
65-
// },
66-
// )),
67-
;
39+
);
6840
}
6941
}
7042

Flutter/json_to_dart/lib/models/dart_object.dart

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ class DartObject extends DartProperty {
2121
MapEntry<String, dynamic>? keyValuePair,
2222
required int depth,
2323
required bool nullable,
24+
DartObject? dartObject,
2425
}) : super(
2526
uid: uid!,
2627
keyValuePair: keyValuePair!,
2728
depth: depth,
2829
nullable: nullable,
30+
dartObject: dartObject,
2931
) {
3032
classNameTextEditingController =
3133
ClassNameCheckerTextEditingController(this);
@@ -104,10 +106,12 @@ class DartObject extends DartProperty {
104106
objectKeys[item.key] = temp;
105107
} else {
106108
final DartObject temp = DartObject(
107-
uid: uid + '_' + item.key,
108-
keyValuePair: MapEntry<String, dynamic>(item.key, item.value.data),
109-
nullable: item.value.nullable,
110-
depth: depth + 1);
109+
uid: uid + '_' + item.key,
110+
keyValuePair: MapEntry<String, dynamic>(item.key, item.value.data),
111+
nullable: item.value.nullable,
112+
depth: depth + 1,
113+
dartObject: this,
114+
);
111115
if (addProperty) {
112116
properties.add(temp);
113117
}
@@ -119,7 +123,8 @@ class DartObject extends DartProperty {
119123
uid: uid,
120124
keyValuePair: MapEntry<String, dynamic>(item.key, item.value.data),
121125
nullable: item.value.nullable,
122-
depth: depth));
126+
depth: depth,
127+
dartObject: this));
123128
}
124129
final List<dynamic> array = item.value.data as List<dynamic>;
125130
if (array.isNotEmpty) {
@@ -144,10 +149,12 @@ class DartObject extends DartProperty {
144149
} else {
145150
if (addProperty) {
146151
properties.add(DartProperty(
147-
uid: uid,
148-
keyValuePair: MapEntry<String, dynamic>(item.key, item.value.data),
149-
nullable: item.value.nullable,
150-
depth: depth));
152+
uid: uid,
153+
keyValuePair: MapEntry<String, dynamic>(item.key, item.value.data),
154+
nullable: item.value.nullable,
155+
depth: depth,
156+
dartObject: this,
157+
));
151158
}
152159
}
153160
}
@@ -566,7 +573,7 @@ class DartObject extends DartProperty {
566573
@override
567574
List<Object?> get props => <Object?>[
568575
className,
569-
properties,
576+
// properties,
570577
uid,
571578
];
572579

Flutter/json_to_dart/lib/models/dart_property.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class DartProperty extends Equatable {
2121
required this.depth,
2222
required this.keyValuePair,
2323
required this.nullable,
24+
this.dartObject,
2425
}) {
2526
key = keyValuePair.key;
2627
this.uid = uid + '_' + keyValuePair.key;
@@ -38,8 +39,10 @@ class DartProperty extends Equatable {
3839

3940
errors.add(EmptyErrorChecker(this));
4041
errors.add(ValidityChecker(this));
42+
errors.add(DuplicatePropertyNameChecker(this));
4143
}
4244

45+
final DartObject? dartObject;
4346
late String uid;
4447
late int depth;
4548
late final String key;

Flutter/json_to_dart/lib/pages/setting.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class SettingWidget extends StatelessWidget {
6666
onChanged: (Locale? value) {
6767
ConfigSetting().locale.value = value!;
6868
Get.updateLocale(ConfigSetting().locale.value);
69+
controller.formatJson();
6970
},
7071
),
7172
);

Flutter/json_to_dart/lib/utils/error_check/error_checker.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import 'package:dartx/dartx.dart';
12
import 'package:equatable/equatable.dart';
2-
33
import 'package:get/get.dart';
44
import 'package:json_to_dart/main_controller.dart';
55
import 'package:json_to_dart/models/dart_object.dart';
@@ -160,3 +160,29 @@ class DuplicateClassChecker extends DartErrorChecker {
160160
}
161161
}
162162
}
163+
164+
class DuplicatePropertyNameChecker extends DartErrorChecker {
165+
DuplicatePropertyNameChecker(DartProperty property) : super(property);
166+
@override
167+
void checkError(RxString input) {
168+
if (property.dartObject == null || !identical(input, property.name)) {
169+
return;
170+
}
171+
172+
final DartObject dartObject = property.dartObject!;
173+
174+
final Map<String, List<DartProperty>> groupProperies = dartObject.properties
175+
.groupBy((DartProperty element) => element.name.value);
176+
177+
for (final MapEntry<String, List<DartProperty>> item
178+
in groupProperies.entries) {
179+
for (final DartProperty element in item.value) {
180+
if (item.value.length > 1) {
181+
element.propertyError.add(appLocalizations.duplicateProperties);
182+
} else {
183+
element.propertyError.remove(appLocalizations.duplicateProperties);
184+
}
185+
}
186+
}
187+
}
188+
}

0 commit comments

Comments
 (0)