Repository: zeromicro/go-zero
Pull request #1596 (2022-03-01 07:58:31)

fix: goctl api dart support `form` tag
Resolves: #1594


Patch:
diff --git a/readme.md b/readme.md
index 3e0045d2..3aff815d 100644
--- a/readme.md
+++ b/readme.md
@@ -10,6 +10,7 @@ English | [简体中文](readme-cn.md)
 [![Release](https://img.shields.io/github/v/release/zeromicro/go-zero.svg?style=flat-square)](https://github.com/zeromicro/go-zero)
 [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
 [![Discord](https://img.shields.io/discord/794530774463414292?label=chat&logo=discord)](https://discord.gg/4JQvC5A4Fe)
+<a href="https://www.producthunt.com/posts/go-zero?utm_source=badge-featured&utm_medium=badge&utm_souce=badge-go&#0045;zero" target="_blank"><img src="https://api.producthunt.com/widgets/embed-image/v1/featured.svg?post_id=334030&theme=light" alt="go&#0045;zero - A&#0032;web&#0032;&#0038;&#0032;rpc&#0032;framework&#0032;written&#0032;in&#0032;Go&#0046; | Product Hunt" style="width: 250px; height: 54px;" width="250" height="54" /></a>
 
 **Note: To meet the requirements of Open Source Foundation, we moved go-zero from tal-tech to zeromicro (a neutral GitHub organization).**
 
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<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}}
 		};
 	}
 }
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/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 (


Test patch:
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)
+			}
+		})
+	}
+}