e47b2b67 by miraclesu

Update ParserForm for new form tag style

1 parent 38f6f8ee
...@@ -190,10 +190,15 @@ func ParseForm(form url.Values, obj interface{}) error { ...@@ -190,10 +190,15 @@ func ParseForm(form url.Values, obj interface{}) error {
190 continue 190 continue
191 } 191 }
192 fieldT := objT.Field(i) 192 fieldT := objT.Field(i)
193 tag := fieldT.Tag.Get("form") 193
194 if len(tag) == 0 { 194 tags := strings.Split(fieldT.Tag.Get("form"), ",")
195 var tag string
196 if len(tags) == 0 || len(tags[0]) == 0 {
195 tag = fieldT.Name 197 tag = fieldT.Name
198 } else {
199 tag = tags[0]
196 } 200 }
201
197 value := form.Get(tag) 202 value := form.Get(tag)
198 if len(value) == 0 { 203 if len(value) == 0 {
199 continue 204 continue
......
...@@ -106,8 +106,9 @@ func TestParseForm(t *testing.T) { ...@@ -106,8 +106,9 @@ func TestParseForm(t *testing.T) {
106 Id int 106 Id int
107 tag string `form:tag` 107 tag string `form:tag`
108 Name interface{} `form:"username"` 108 Name interface{} `form:"username"`
109 Age int `form:"age"` 109 Age int `form:"age,text"`
110 Email string 110 Email string
111 Intro string `form:",textarea"`
111 } 112 }
112 113
113 u := user{} 114 u := user{}
...@@ -116,6 +117,7 @@ func TestParseForm(t *testing.T) { ...@@ -116,6 +117,7 @@ func TestParseForm(t *testing.T) {
116 "username": []string{"test"}, 117 "username": []string{"test"},
117 "age": []string{"40"}, 118 "age": []string{"40"},
118 "Email": []string{"test@gmail.com"}, 119 "Email": []string{"test@gmail.com"},
120 "Intro": []string{"I am an engineer!"},
119 } 121 }
120 if err := ParseForm(form, u); err == nil { 122 if err := ParseForm(form, u); err == nil {
121 t.Fatal("nothing will be changed") 123 t.Fatal("nothing will be changed")
...@@ -127,15 +129,18 @@ func TestParseForm(t *testing.T) { ...@@ -127,15 +129,18 @@ func TestParseForm(t *testing.T) {
127 t.Errorf("Id should equal 0 but got %v", u.Id) 129 t.Errorf("Id should equal 0 but got %v", u.Id)
128 } 130 }
129 if len(u.tag) != 0 { 131 if len(u.tag) != 0 {
130 t.Error("tag's length should equal 0 but got %v", len(u.tag)) 132 t.Errorf("tag's length should equal 0 but got %v", len(u.tag))
131 } 133 }
132 if u.Name.(string) != "test" { 134 if u.Name.(string) != "test" {
133 t.Error("Name should equal `test` but got `%v`", u.Name.(string)) 135 t.Errorf("Name should equal `test` but got `%v`", u.Name.(string))
134 } 136 }
135 if u.Age != 40 { 137 if u.Age != 40 {
136 t.Error("Age should equal 40 but got %v", u.Age) 138 t.Errorf("Age should equal 40 but got %v", u.Age)
137 } 139 }
138 if u.Email != "test@gmail.com" { 140 if u.Email != "test@gmail.com" {
139 t.Error("Email should equal `test@gmail.com` but got `%v`", u.Email) 141 t.Errorf("Email should equal `test@gmail.com` but got `%v`", u.Email)
142 }
143 if u.Intro != "I am an engineer!" {
144 t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro)
140 } 145 }
141 } 146 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!