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) {
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}}
};
}
}

@ -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 {

@ -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"
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 (

Loading…
Cancel
Save