Skip to content

Commit 88a48be

Browse files
committed
修复了一个V1中的bug
1 parent 8bf4ebe commit 88a48be

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

lib/generator.dart

+9-6
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ class Generator {
4646
var fieldList = template.fieldList;
4747
fieldList.forEach((filed) {
4848
if (filed is MapField) {
49-
DefaultTemplate template = DefaultTemplate(
50-
srcJson: json.encode(filed.map), className: filed.typeString);
49+
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.map), className: filed.typeString);
50+
if (version == Version.v1) {
51+
template = V1Template(srcJson: json.encode(filed.map), className: filed.typeString);
52+
}
5153
templateList.add(template);
5254
refreshTemplate(template);
5355
} else if (filed is ListField) {
5456
if (filed.childIsObject) {
55-
DefaultTemplate template = DefaultTemplate(
56-
srcJson: json.encode(filed.list[0]), className: filed.typeName);
57+
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.list[0]), className: filed.typeName);
58+
if (version == Version.v1) {
59+
template = V1Template(srcJson: json.encode(filed.list[0]), className: filed.typeName);
60+
}
5761
templateList.add(template);
5862
refreshTemplate(template);
5963
}
@@ -63,8 +67,7 @@ class Generator {
6367

6468
String get fileName => camelCase2UnderScoreCase(entityName);
6569

66-
static const String importString =
67-
"import 'package:json_annotation/json_annotation.dart';";
70+
static const String importString = "import 'package:json_annotation/json_annotation.dart';";
6871

6972
String get header => """$importString
7073

lib/template.dart

+7-15
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,22 @@ class DefaultTemplate extends Template {
108108
if (this is ListTemplate) {
109109
return this;
110110
}
111-
return ListTemplate(
112-
srcJson: srcJson, className: className, delegateTemplate: this);
111+
return ListTemplate(srcJson: srcJson, className: className, delegateTemplate: this);
113112
}
114113
}
115114

116115
class ListTemplate extends DefaultTemplate {
117116
Template delegateTemplate;
118117

119-
ListTemplate(
120-
{String srcJson, String className = "Entity", this.delegateTemplate})
121-
: super(className: className, srcJson: srcJson);
118+
ListTemplate({String srcJson, String className = "Entity", this.delegateTemplate}) : super(className: className, srcJson: srcJson);
122119

123120
@override
124121
String declare() {
125-
return _declareListMethod() + "\n" + delegateTemplate?.declare() ??
126-
super.declare();
122+
return _declareListMethod() + "\n" + delegateTemplate?.declare() ?? super.declare();
127123
}
128124

129125
String _declareListMethod() {
130-
var listMethod =
131-
"""List<$className> get${className}List(List<dynamic> list){
126+
var listMethod = """List<$className> get${className}List(List<dynamic> list){
132127
List<$className> result = [];
133128
list.forEach((item){
134129
result.add($className.fromJson(item));
@@ -159,13 +154,11 @@ class ListTemplate extends DefaultTemplate {
159154
}
160155

161156
@override
162-
List<Field> get fieldList =>
163-
FieldHelper(json.encode(json.decode(srcJson)[0])).getFields();
157+
List<Field> get fieldList => FieldHelper(json.encode(json.decode(srcJson)[0])).getFields();
164158
}
165159

166160
class V1Template extends DefaultTemplate {
167-
V1Template({String srcJson, String className = "Entity"})
168-
: super(className: className, srcJson: srcJson);
161+
V1Template({String srcJson, String className = "Entity"}) : super(className: className, srcJson: srcJson);
169162

170163
@override
171164
String interface() => "";
@@ -175,8 +168,7 @@ class V1Template extends DefaultTemplate {
175168
var result = StringBuffer();
176169
result.writeln(super.method());
177170
result.writeln();
178-
result.write(
179-
" Map<String, dynamic> toJson() => _\$${className}ToJson(this);");
171+
result.write(" Map<String, dynamic> toJson() => _\$${className}ToJson(this);");
180172
return result.toString();
181173
}
182174
}

0 commit comments

Comments
 (0)