Skip to content

Commit

Permalink
Fix clone method
Browse files Browse the repository at this point in the history
  • Loading branch information
zmtzawqlp committed Apr 4, 2021
1 parent 0fa95ed commit 00e9087
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Flutter/json_to_dart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 1.1.0

* Add clone method

## 1.0.0

* First Release
35 changes: 27 additions & 8 deletions Flutter/json_to_dart/lib/models/dart_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ class DartObject extends DartProperty {
(ConfigSetting().nullsafety && item.nullable),
);
final bool isGetSet = fss.startsWith('{');

if (item is DartObject) {
className = item.className;

Expand Down Expand Up @@ -319,11 +318,32 @@ class DartObject extends DartProperty {
<String>[typeString, name, lowName]));
fromJsonSb.writeLine(setString);

final bool nonNullAble = ConfigSetting().nullsafety && !item.nullable;
// String setNameTemp = setName;

// if (className != null) {
// String toJson = '=> e.toJson()';
// dynamic value = item.value;
// String typeString = className;
// while (value is List) {
// toJson = '=> e.map(($typeString e) $toJson)';
// typeString = 'List<$typeString>';
// if (value.isNotEmpty) {
// value = value.first;
// } else {
// break;
// }
// }
// toJson = toJson.replaceFirst('=>', '');
// toJson = toJson.replaceFirst('e', '');
// toJson = toJson.trim();

// final bool nonNullAble = ConfigSetting().nullsafety && !item.nullable;
// setNameTemp += '${nonNullAble ? '' : '?'}$toJson';
// }

toJsonSb.writeLine(stringFormat(DartHelper.toJsonSetString, <String>[
item.key,
setName +
(item is DartObject ? '${nonNullAble ? '' : '?'}.toJson()' : '')
setName,
]));
}

Expand Down Expand Up @@ -362,7 +382,8 @@ class DartObject extends DartProperty {
sb.writeLine(propertySb.toString());
sb.writeLine(DartHelper.classToString);
sb.writeLine(toJsonSb.toString());
sb.writeLine(stringFormat(DartHelper.classToClone, <String>[className]));
sb.writeLine(stringFormat(DartHelper.classToClone,
<String>[className, if (ConfigSetting().nullsafety) '!' else '']));
}

sb.writeLine(DartHelper.classFooter);
Expand Down Expand Up @@ -408,8 +429,6 @@ class DartObject extends DartProperty {
@override
List<Object?> get props => <Object?>[
className,
nullable,
propertyAccessorType,
type,
properties,
];
}
2 changes: 1 addition & 1 deletion Flutter/json_to_dart/lib/utils/dart_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import 'dart:convert';''';
' \n@override\nString toString() {\n return jsonEncode(this);\n }';

static const String classToClone =
'\n{0} clone() => {0}.fromJson(toJson());\n';
'\n{0} clone() => {0}.fromJson(asT<Map<String, dynamic>>(jsonDecode(jsonEncode(this))){1});\n';

static DartType converDartType(Type type) {
if (type == int) {
Expand Down
2 changes: 1 addition & 1 deletion Flutter/json_to_dart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: The tool to convert json to dart code.
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
version: 1.1.0

environment:
sdk: '>=2.12.0 <3.0.0'
Expand Down

0 comments on commit 00e9087

Please sign in to comment.