Skip to content

Commit

Permalink
fix: goctl api dart support form tag (zeromicro#1596)
Browse files Browse the repository at this point in the history
  • Loading branch information
fynxiu authored Mar 1, 2022
1 parent 877eb6a commit fba6543
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
4 changes: 2 additions & 2 deletions tools/goctl/api/dartgen/gendata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class {{.Name}}{
});
factory {{.Name}}.fromJson(Map<String,dynamic> 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<dynamic>).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<dynamic>).map((i) => {{getCoreType .Type.Name}}.fromJson(i)){{else}}{{.Type.Name}}.fromJson(m['{{getPropertyFromMember .}}']){{end}},{{end}}
);
}
Map<String,dynamic> 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}}
};
}
}
Expand Down
14 changes: 4 additions & 10 deletions tools/goctl/api/dartgen/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
39 changes: 39 additions & 0 deletions tools/goctl/api/dartgen/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
12 changes: 6 additions & 6 deletions tools/goctl/api/dartgen/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit fba6543

Please sign in to comment.