3fe9e6a2 by Vangelis Tsoumenis

extract func `parseFormTag` from templatefunc.RenderForm

Extracted a func `parseFormTag` that takes a reflect.StructField
and returns the different positional parts of the `form` structTag
with default values as documented in
http://beego.me/docs/mvc/view/view.md#renderform

This makes RenderForm shorter and makes it possible to test the
parsing separately.
1 parent 62e9c890
...@@ -368,15 +368,31 @@ func RenderForm(obj interface{}) template.HTML { ...@@ -368,15 +368,31 @@ func RenderForm(obj interface{}) template.HTML {
368 } 368 }
369 369
370 fieldT := objT.Field(i) 370 fieldT := objT.Field(i)
371
372 label, name, fType, ignored := parseFormTag(fieldT)
373 if ignored {
374 continue
375 }
376
377 raw = append(raw, fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`,
378 label, name, fType, fieldV.Interface()))
379 }
380 return template.HTML(strings.Join(raw, "</br>"))
381 }
382
383 // parseFormTag takes the stuct-tag of a StructField and parses the `form` value.
384 // returned are the form label, name-property, type and wether the field should be ignored.
385 func parseFormTag(fieldT reflect.StructField) (label, name, fType string, ignored bool) {
371 tags := strings.Split(fieldT.Tag.Get("form"), ",") 386 tags := strings.Split(fieldT.Tag.Get("form"), ",")
372 label := fieldT.Name + ": " 387 label = fieldT.Name + ": "
373 name := fieldT.Name 388 name = fieldT.Name
374 fType := "text" 389 fType = "text"
390 ignored = false;
375 391
376 switch len(tags) { 392 switch len(tags) {
377 case 1: 393 case 1:
378 if tags[0] == "-" { 394 if tags[0] == "-" {
379 continue 395 ignored = true
380 } 396 }
381 if len(tags[0]) > 0 { 397 if len(tags[0]) > 0 {
382 name = tags[0] 398 name = tags[0]
...@@ -399,11 +415,7 @@ func RenderForm(obj interface{}) template.HTML { ...@@ -399,11 +415,7 @@ func RenderForm(obj interface{}) template.HTML {
399 label = tags[2] 415 label = tags[2]
400 } 416 }
401 } 417 }
402 418 return
403 raw = append(raw, fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`,
404 label, name, fType, fieldV.Interface()))
405 }
406 return template.HTML(strings.Join(raw, "</br>"))
407 } 419 }
408 420
409 func isStructPtr(t reflect.Type) bool { 421 func isStructPtr(t reflect.Type) bool {
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
15 "net/url" 15 "net/url"
16 "testing" 16 "testing"
17 "time" 17 "time"
18 "reflect"
18 ) 19 )
19 20
20 func TestSubstr(t *testing.T) { 21 func TestSubstr(t *testing.T) {
...@@ -164,3 +165,41 @@ func TestRenderForm(t *testing.T) { ...@@ -164,3 +165,41 @@ func TestRenderForm(t *testing.T) {
164 t.Errorf("output should equal `%v` but got `%v`", result, output) 165 t.Errorf("output should equal `%v` but got `%v`", result, output)
165 } 166 }
166 } 167 }
168
169 func TestParseFormTag(t *testing.T) {
170 // create struct to contain field with different types of struct-tag `form`
171 type user struct {
172 All int `form:"name,text,年龄:"`
173 NoName int `form:",hidden,年龄:"`
174 OnlyLabel int `form:",,年龄:"`
175 OnlyName int `form:"name"`
176 Ignored int `form:"-"`
177 }
178
179 objT := reflect.TypeOf(&user{}).Elem()
180
181 label, name, fType, ignored := parseFormTag(objT.Field(0))
182 if !(name == "name" && label == "年龄:" && fType == "text" && ignored == false) {
183 t.Errorf("Form Tag with name, label and type was not correctly parsed.")
184 }
185
186 label, name, fType, ignored = parseFormTag(objT.Field(1))
187 if !(name == "NoName" && label == "年龄:" && fType == "hidden" && ignored == false) {
188 t.Errorf("Form Tag with label and type but without name was not correctly parsed.")
189 }
190
191 label, name, fType, ignored = parseFormTag(objT.Field(2))
192 if !(name == "OnlyLabel" && label == "年龄:" && fType == "text" && ignored == false) {
193 t.Errorf("Form Tag containing only label was not correctly parsed.")
194 }
195
196 label, name, fType, ignored = parseFormTag(objT.Field(3))
197 if !(name == "name" && label == "OnlyName: " && fType == "text" && ignored == false) {
198 t.Errorf("Form Tag containing only name was not correctly parsed.")
199 }
200
201 label, name, fType, ignored = parseFormTag(objT.Field(4))
202 if (ignored == false) {
203 t.Errorf("Form Tag that should be ignored was not correctly parsed.")
204 }
205 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!