Skip to content

Commit 42d0e35

Browse files
committed
add DuplicateClassChecker
1 parent af4316a commit 42d0e35

File tree

3 files changed

+69
-88
lines changed

3 files changed

+69
-88
lines changed

Flutter/json_to_dart/lib/models/dart_object.dart

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:json_to_dart/models/config.dart';
44
import 'package:json_to_dart/utils/camel_under_score_converter.dart';
55
import 'package:json_to_dart/utils/dart_helper.dart';
66
import 'package:json_to_dart/utils/enums.dart';
7+
import 'package:json_to_dart/utils/error_check/error_checker.dart';
78
import 'package:json_to_dart/utils/error_check/text_editing_controller.dart';
89
import 'package:json_to_dart/utils/my_string_buffer.dart';
910
import 'package:json_to_dart/utils/string_helper.dart';
@@ -49,7 +50,8 @@ class DartObject extends DartProperty {
4950
updateNameByNamingConventionsType();
5051

5152
Get.find<MainController>().allObjects.add(this);
52-
53+
duplicateClassChecker = DuplicateClassChecker(this);
54+
errors.add(duplicateClassChecker);
5355
updateError(className);
5456
}
5557

@@ -59,7 +61,7 @@ class DartObject extends DartProperty {
5961
Map<String, _InnerObject>? get jObject =>
6062
_mergeObject != null ? _mergeObject! : _jObject;
6163

62-
DartObject? duplicateClass;
64+
late DuplicateClassChecker duplicateClassChecker;
6365

6466
RxString className = ''.obs;
6567

@@ -565,53 +567,12 @@ class DartObject extends DartProperty {
565567
List<Object?> get props => <Object?>[
566568
className,
567569
properties,
570+
uid,
568571
];
569572

570573
RxSet<String> classError = <String>{}.obs;
571574

572575
bool get hasClassError => classError.isNotEmpty;
573-
@override
574-
void updateError(RxString input) {
575-
super.updateError(input);
576-
// String errorInfo = '';
577-
578-
// if (duplicateClass != null) {
579-
// if (duplicateClass!.className != className) {
580-
// errorInfo = '';
581-
// duplicateClass!.classError.value = '';
582-
// duplicateClass!.duplicateClass = null;
583-
// duplicateClass = null;
584-
// }
585-
// } else if (sameName != null) {
586-
// if (sameName is DartObject) {
587-
// if (className.value != sameName!.name.value &&
588-
// name.value != (sameName as DartObject).className.value) {
589-
// errorInfo = '';
590-
// (sameName as DartObject).classError.value = '';
591-
// sameName!.sameName = null;
592-
// sameName = null;
593-
// }
594-
// } else {
595-
// if (className.value != sameName!.name.value) {
596-
// errorInfo = '';
597-
// sameName!.propertyError.value = '';
598-
// sameName!.sameName = null;
599-
// sameName = null;
600-
// }
601-
// }
602-
// } else if (className.isEmpty) {
603-
// errorInfo = appLocalizations.classNameAssert(uid);
604-
// }
605-
606-
// if (name.isEmpty) {
607-
// propertyError.value =
608-
// appLocalizations.propertyNameAssert('').replaceAll(':', '');
609-
// }
610-
611-
// classError.value = errorInfo;
612-
613-
//super.updateError();
614-
}
615576
}
616577

617578
class _InnerObject {

Flutter/json_to_dart/lib/models/dart_property.dart

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ class DartProperty extends Equatable {
2828
propertyAccessorType.value = ConfigSetting().propertyAccessorType.value;
2929
type.value = DartHelper.converDartType(keyValuePair.value.runtimeType);
3030
name.value = keyValuePair.key;
31+
3132
nameTextEditingController = PropertyNameCheckerTextEditingController(this);
3233
nameTextEditingController.text = name.value;
3334
value = keyValuePair.value;
3435
if (this is! DartObject) {
3536
Get.find<MainController>().allProperties.add(this);
3637
}
3738

38-
errors.add(EmptyErrorChecker(property: this));
39-
errors.add(ValidityChecker(property: this));
40-
updateError(name);
39+
errors.add(EmptyErrorChecker(this));
40+
errors.add(ValidityChecker(this));
4141
}
4242

4343
late String uid;
@@ -59,31 +59,10 @@ class DartProperty extends Equatable {
5959

6060
bool get hasPropertyError => propertyError.isNotEmpty;
6161

62-
/// property has the same name with some Class
63-
DartProperty? sameName;
64-
6562
void updateError(RxString input) {
6663
for (final DartErrorChecker error in errors) {
6764
error.checkError(input);
6865
}
69-
// if (errorMap.isNotEmpty) {}
70-
71-
// String errorInfo = '';
72-
// if (sameName != null && sameName is DartObject) {
73-
// if (sameName is DartObject) {
74-
// if (name.value != (sameName as DartObject).className.value) {
75-
// errorInfo = '';
76-
// (sameName as DartObject).classError.value = '';
77-
// sameName?.sameName = null;
78-
// sameName = null;
79-
// }
80-
// }
81-
// }
82-
// if (name.isEmpty) {
83-
// errorInfo = appLocalizations.propertyNameAssert('').replaceAll(':', '');
84-
// }
85-
86-
// propertyError.value = errorInfo;
8766
}
8867

8968
void updateNameByNamingConventionsType() {
@@ -107,6 +86,8 @@ class DartProperty extends Equatable {
10786
}
10887

10988
this.name.value = correctName(name, dartProperty: this);
89+
nameTextEditingController.text = this.name.value;
90+
updateError(this.name);
11091
}
11192

11293
void updatePropertyAccessorType() {
@@ -285,24 +266,11 @@ class DartProperty extends Equatable {
285266
nullable,
286267
propertyAccessorType,
287268
type,
269+
uid,
288270
];
289271

290272
@override
291273
String toString() {
292274
return 'DartProperty($key, $value, $nullable)';
293275
}
294276
}
295-
296-
enum ErrorType {
297-
/// property name is empty
298-
propertyEmpty,
299-
300-
/// class name is empty
301-
classEmpty,
302-
303-
/// there are same class name
304-
duplicateClass,
305-
306-
/// one property name is the same as class name
307-
snpc,
308-
}

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:equatable/equatable.dart';
2+
23
import 'package:get/get.dart';
4+
import 'package:json_to_dart/main_controller.dart';
35
import 'package:json_to_dart/models/dart_object.dart';
46
import 'package:json_to_dart/models/dart_property.dart';
57
import 'package:json_to_dart/utils/camel_under_score_converter.dart';
@@ -19,21 +21,20 @@ class DartError extends Equatable {
1921
}
2022

2123
abstract class DartErrorChecker {
22-
DartErrorChecker({required this.property});
24+
DartErrorChecker(this.property);
2325
final DartProperty property;
2426
void checkError(RxString input);
2527
}
2628

2729
class EmptyErrorChecker extends DartErrorChecker {
28-
EmptyErrorChecker({required DartProperty property})
29-
: super(property: property);
30+
EmptyErrorChecker(DartProperty property) : super(property);
3031

3132
@override
3233
void checkError(RxString input) {
3334
late String errorInfo;
3435
late RxSet<String> out;
3536
// property change
36-
if (input == property.name) {
37+
if (identical(input, property.name)) {
3738
errorInfo = appLocalizations.propertyNameAssert(property.uid);
3839
out = property.propertyError;
3940
}
@@ -53,15 +54,15 @@ class EmptyErrorChecker extends DartErrorChecker {
5354
}
5455

5556
class ValidityChecker extends DartErrorChecker {
56-
ValidityChecker({required DartProperty property}) : super(property: property);
57+
ValidityChecker(DartProperty property) : super(property);
5758

5859
@override
5960
void checkError(RxString input) {
6061
String? errorInfo;
6162
late RxSet<String> out;
6263
final String value = input.value;
6364
// property change
64-
if (input == property.name) {
65+
if (identical(input, property.name)) {
6566
if (propertyKeyWord.contains(value)) {
6667
errorInfo = appLocalizations.keywordCheckFailed(value);
6768
} else if (property is DartObject &&
@@ -108,3 +109,54 @@ class ValidityChecker extends DartErrorChecker {
108109
}
109110
}
110111
}
112+
113+
class DuplicateClassChecker extends DartErrorChecker {
114+
DuplicateClassChecker(DartObject property) : super(property);
115+
116+
DartObject get dartObject => property as DartObject;
117+
118+
Map<String, DartObject> duplicateClass = <String, DartObject>{};
119+
120+
@override
121+
void checkError(RxString input) {
122+
if (!identical(input, dartObject.className)) {
123+
return;
124+
}
125+
final MainController controller = Get.find<MainController>();
126+
final String errorInfo = appLocalizations.duplicateClasses;
127+
for (final DartObject oj in controller.allObjects) {
128+
if (oj == dartObject) {
129+
continue;
130+
}
131+
132+
if (!duplicateClass.containsKey(oj.uid) &&
133+
oj.className.value == dartObject.className.value) {
134+
duplicateClass[oj.uid] = oj;
135+
} else if (oj.className.value != dartObject.className.value &&
136+
duplicateClass.remove(oj.uid) != null) {
137+
oj.duplicateClassChecker.duplicateClass.remove(dartObject.uid);
138+
if (oj.duplicateClassChecker.duplicateClass.isEmpty) {
139+
oj.classError.remove(errorInfo);
140+
}
141+
}
142+
}
143+
144+
if (duplicateClass.isEmpty) {
145+
dartObject.classError.remove(errorInfo);
146+
} else {
147+
for (final MapEntry<String, DartObject> entry in duplicateClass.entries) {
148+
entry.value.duplicateClassChecker.duplicateClass[dartObject.uid] =
149+
dartObject;
150+
for (final MapEntry<String, DartObject> entry1
151+
in duplicateClass.entries) {
152+
if (entry1.key != entry.key) {
153+
entry.value.duplicateClassChecker.duplicateClass[entry1.key] =
154+
entry1.value;
155+
}
156+
}
157+
entry.value.classError.add(errorInfo);
158+
}
159+
dartObject.classError.add(errorInfo);
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)