diff --git a/tools/goctl/api/dartgen/gendata.go b/tools/goctl/api/dartgen/gendata.go index 6b33e610..f8e4d538 100644 --- a/tools/goctl/api/dartgen/gendata.go +++ b/tools/goctl/api/dartgen/gendata.go @@ -19,12 +19,12 @@ class {{.Name}}{ }); factory {{.Name}}.fromJson(Map m) { return {{.Name}}({{range .Members}} - {{lowCamelCase .Name}}: {{if isDirectType .Type.Name}}m['{{tagGet .Tag "json"}}']{{else if isClassListType .Type.Name}}(m['{{tagGet .Tag "json"}}'] as List).map((i) => {{getCoreType .Type.Name}}.fromJson(i)){{else}}{{.Type.Name}}.fromJson(m['{{tagGet .Tag "json"}}']){{end}},{{end}} + {{lowCamelCase .Name}}: {{if isDirectType .Type.Name}}m['{{getPropertyFromMember .}}']{{else if isClassListType .Type.Name}}(m['{{getPropertyFromMember .}}'] as List).map((i) => {{getCoreType .Type.Name}}.fromJson(i)){{else}}{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}},{{end}} ); } Map toJson() { return { {{range .Members}} - '{{tagGet .Tag "json"}}': {{if isDirectType .Type.Name}}{{lowCamelCase .Name}}{{else if isClassListType .Type.Name}}{{lowCamelCase .Name}}.map((i) => i.toJson()){{else}}{{lowCamelCase .Name}}.toJson(){{end}},{{end}} + '{{getPropertyFromMember .}}': {{if isDirectType .Type.Name}}{{lowCamelCase .Name}}{{else if isClassListType .Type.Name}}{{lowCamelCase .Name}}.map((i) => i.toJson()){{else}}{{lowCamelCase .Name}}.toJson(){{end}},{{end}} }; } } diff --git a/tools/goctl/api/dartgen/util.go b/tools/goctl/api/dartgen/util.go index 5e5fe7e3..92cf2a82 100644 --- a/tools/goctl/api/dartgen/util.go +++ b/tools/goctl/api/dartgen/util.go @@ -34,18 +34,12 @@ func pathToFuncName(path string) string { return util.ToLower(camel[:1]) + camel[1:] } -func tagGet(tag, k string) string { - tags, err := spec.Parse(tag) +func getPropertyFromMember(member spec.Member) string { + name, err := member.GetPropertyName() if err != nil { - panic(k + " not exist") + panic(fmt.Sprintf("cannot get property name of %q", member.Name)) } - - v, err := tags.Get(k) - if err != nil { - panic(k + " value not exist") - } - - return v.Name + return name } func isDirectType(s string) bool { diff --git a/tools/goctl/api/dartgen/util_test.go b/tools/goctl/api/dartgen/util_test.go new file mode 100644 index 00000000..68a221fd --- /dev/null +++ b/tools/goctl/api/dartgen/util_test.go @@ -0,0 +1,39 @@ +package dartgen + +import ( + "testing" + + "github.com/zeromicro/go-zero/tools/goctl/api/spec" +) + +func Test_getPropertyFromMember(t *testing.T) { + tests := []struct { + name string + member spec.Member + want string + }{ + { + name: "json tag should be ok", + member: spec.Member{ + Tag: "`json:\"foo\"`", + Name: "Foo", + }, + want: "foo", + }, + { + name: "form tag should be ok", + member: spec.Member{ + Tag: "`form:\"bar\"`", + Name: "Bar", + }, + want: "bar", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getPropertyFromMember(tt.member); got != tt.want { + t.Errorf("getPropertyFromMember() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/tools/goctl/api/dartgen/vars.go b/tools/goctl/api/dartgen/vars.go index 78f95e1f..67aafde2 100644 --- a/tools/goctl/api/dartgen/vars.go +++ b/tools/goctl/api/dartgen/vars.go @@ -3,12 +3,12 @@ package dartgen import "text/template" var funcMap = template.FuncMap{ - "tagGet": tagGet, - "isDirectType": isDirectType, - "isClassListType": isClassListType, - "getCoreType": getCoreType, - "pathToFuncName": pathToFuncName, - "lowCamelCase": lowCamelCase, + "getPropertyFromMember": getPropertyFromMember, + "isDirectType": isDirectType, + "isClassListType": isClassListType, + "getCoreType": getCoreType, + "pathToFuncName": pathToFuncName, + "lowCamelCase": lowCamelCase, } const (