Skip to content

Commit

Permalink
修复了一个V1中的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CaiJingLong committed Aug 16, 2018
1 parent 8bf4ebe commit 88a48be
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
15 changes: 9 additions & 6 deletions lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ class Generator {
var fieldList = template.fieldList;
fieldList.forEach((filed) {
if (filed is MapField) {
DefaultTemplate template = DefaultTemplate(
srcJson: json.encode(filed.map), className: filed.typeString);
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.map), className: filed.typeString);
if (version == Version.v1) {
template = V1Template(srcJson: json.encode(filed.map), className: filed.typeString);
}
templateList.add(template);
refreshTemplate(template);
} else if (filed is ListField) {
if (filed.childIsObject) {
DefaultTemplate template = DefaultTemplate(
srcJson: json.encode(filed.list[0]), className: filed.typeName);
DefaultTemplate template = DefaultTemplate(srcJson: json.encode(filed.list[0]), className: filed.typeName);
if (version == Version.v1) {
template = V1Template(srcJson: json.encode(filed.list[0]), className: filed.typeName);
}
templateList.add(template);
refreshTemplate(template);
}
Expand All @@ -63,8 +67,7 @@ class Generator {

String get fileName => camelCase2UnderScoreCase(entityName);

static const String importString =
"import 'package:json_annotation/json_annotation.dart';";
static const String importString = "import 'package:json_annotation/json_annotation.dart';";

String get header => """$importString
Expand Down
22 changes: 7 additions & 15 deletions lib/template.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,22 @@ class DefaultTemplate extends Template {
if (this is ListTemplate) {
return this;
}
return ListTemplate(
srcJson: srcJson, className: className, delegateTemplate: this);
return ListTemplate(srcJson: srcJson, className: className, delegateTemplate: this);
}
}

class ListTemplate extends DefaultTemplate {
Template delegateTemplate;

ListTemplate(
{String srcJson, String className = "Entity", this.delegateTemplate})
: super(className: className, srcJson: srcJson);
ListTemplate({String srcJson, String className = "Entity", this.delegateTemplate}) : super(className: className, srcJson: srcJson);

@override
String declare() {
return _declareListMethod() + "\n" + delegateTemplate?.declare() ??
super.declare();
return _declareListMethod() + "\n" + delegateTemplate?.declare() ?? super.declare();
}

String _declareListMethod() {
var listMethod =
"""List<$className> get${className}List(List<dynamic> list){
var listMethod = """List<$className> get${className}List(List<dynamic> list){
List<$className> result = [];
list.forEach((item){
result.add($className.fromJson(item));
Expand Down Expand Up @@ -159,13 +154,11 @@ class ListTemplate extends DefaultTemplate {
}

@override
List<Field> get fieldList =>
FieldHelper(json.encode(json.decode(srcJson)[0])).getFields();
List<Field> get fieldList => FieldHelper(json.encode(json.decode(srcJson)[0])).getFields();
}

class V1Template extends DefaultTemplate {
V1Template({String srcJson, String className = "Entity"})
: super(className: className, srcJson: srcJson);
V1Template({String srcJson, String className = "Entity"}) : super(className: className, srcJson: srcJson);

@override
String interface() => "";
Expand All @@ -175,8 +168,7 @@ class V1Template extends DefaultTemplate {
var result = StringBuffer();
result.writeln(super.method());
result.writeln();
result.write(
" Map<String, dynamic> toJson() => _\$${className}ToJson(this);");
result.write(" Map<String, dynamic> toJson() => _\$${className}ToJson(this);");
return result.toString();
}
}
Expand Down

0 comments on commit 88a48be

Please sign in to comment.