fix: goctl api dart support `form` tag (#1596)

master
Fyn 3 years ago committed by GitHub
parent 877eb6ac56
commit fba6543b23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -19,12 +19,12 @@ class {{.Name}}{
}); });
factory {{.Name}}.fromJson(Map<String,dynamic> m) { factory {{.Name}}.fromJson(Map<String,dynamic> m) {
return {{.Name}}({{range .Members}} 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() { Map<String,dynamic> toJson() {
return { {{range .Members}} 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}}
}; };
} }
} }

@ -34,18 +34,12 @@ func pathToFuncName(path string) string {
return util.ToLower(camel[:1]) + camel[1:] return util.ToLower(camel[:1]) + camel[1:]
} }
func tagGet(tag, k string) string { func getPropertyFromMember(member spec.Member) string {
tags, err := spec.Parse(tag) name, err := member.GetPropertyName()
if err != nil { if err != nil {
panic(k + " not exist") panic(fmt.Sprintf("cannot get property name of %q", member.Name))
} }
return name
v, err := tags.Get(k)
if err != nil {
panic(k + " value not exist")
}
return v.Name
} }
func isDirectType(s string) bool { func isDirectType(s string) bool {

@ -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)
}
})
}
}

@ -3,12 +3,12 @@ package dartgen
import "text/template" import "text/template"
var funcMap = template.FuncMap{ var funcMap = template.FuncMap{
"tagGet": tagGet, "getPropertyFromMember": getPropertyFromMember,
"isDirectType": isDirectType, "isDirectType": isDirectType,
"isClassListType": isClassListType, "isClassListType": isClassListType,
"getCoreType": getCoreType, "getCoreType": getCoreType,
"pathToFuncName": pathToFuncName, "pathToFuncName": pathToFuncName,
"lowCamelCase": lowCamelCase, "lowCamelCase": lowCamelCase,
} }
const ( const (

Loading…
Cancel
Save