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.
Showing
2 changed files
with
82 additions
and
31 deletions
| ... | @@ -368,37 +368,11 @@ func RenderForm(obj interface{}) template.HTML { | ... | @@ -368,37 +368,11 @@ func RenderForm(obj interface{}) template.HTML { |
| 368 | } | 368 | } |
| 369 | 369 | ||
| 370 | fieldT := objT.Field(i) | 370 | fieldT := objT.Field(i) |
| 371 | tags := strings.Split(fieldT.Tag.Get("form"), ",") | 371 | |
| 372 | label := fieldT.Name + ": " | 372 | label, name, fType, ignored := parseFormTag(fieldT) |
| 373 | name := fieldT.Name | 373 | if ignored { |
| 374 | fType := "text" | 374 | continue |
| 375 | 375 | } | |
| 376 | switch len(tags) { | ||
| 377 | case 1: | ||
| 378 | if tags[0] == "-" { | ||
| 379 | continue | ||
| 380 | } | ||
| 381 | if len(tags[0]) > 0 { | ||
| 382 | name = tags[0] | ||
| 383 | } | ||
| 384 | case 2: | ||
| 385 | if len(tags[0]) > 0 { | ||
| 386 | name = tags[0] | ||
| 387 | } | ||
| 388 | if len(tags[1]) > 0 { | ||
| 389 | fType = tags[1] | ||
| 390 | } | ||
| 391 | case 3: | ||
| 392 | if len(tags[0]) > 0 { | ||
| 393 | name = tags[0] | ||
| 394 | } | ||
| 395 | if len(tags[1]) > 0 { | ||
| 396 | fType = tags[1] | ||
| 397 | } | ||
| 398 | if len(tags[2]) > 0 { | ||
| 399 | label = tags[2] | ||
| 400 | } | ||
| 401 | } | ||
| 402 | 376 | ||
| 403 | raw = append(raw, fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, | 377 | raw = append(raw, fmt.Sprintf(`%v<input name="%v" type="%v" value="%v">`, |
| 404 | label, name, fType, fieldV.Interface())) | 378 | label, name, fType, fieldV.Interface())) |
| ... | @@ -406,6 +380,44 @@ func RenderForm(obj interface{}) template.HTML { | ... | @@ -406,6 +380,44 @@ func RenderForm(obj interface{}) template.HTML { |
| 406 | return template.HTML(strings.Join(raw, "</br>")) | 380 | return template.HTML(strings.Join(raw, "</br>")) |
| 407 | } | 381 | } |
| 408 | 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) { | ||
| 386 | tags := strings.Split(fieldT.Tag.Get("form"), ",") | ||
| 387 | label = fieldT.Name + ": " | ||
| 388 | name = fieldT.Name | ||
| 389 | fType = "text" | ||
| 390 | ignored = false; | ||
| 391 | |||
| 392 | switch len(tags) { | ||
| 393 | case 1: | ||
| 394 | if tags[0] == "-" { | ||
| 395 | ignored = true | ||
| 396 | } | ||
| 397 | if len(tags[0]) > 0 { | ||
| 398 | name = tags[0] | ||
| 399 | } | ||
| 400 | case 2: | ||
| 401 | if len(tags[0]) > 0 { | ||
| 402 | name = tags[0] | ||
| 403 | } | ||
| 404 | if len(tags[1]) > 0 { | ||
| 405 | fType = tags[1] | ||
| 406 | } | ||
| 407 | case 3: | ||
| 408 | if len(tags[0]) > 0 { | ||
| 409 | name = tags[0] | ||
| 410 | } | ||
| 411 | if len(tags[1]) > 0 { | ||
| 412 | fType = tags[1] | ||
| 413 | } | ||
| 414 | if len(tags[2]) > 0 { | ||
| 415 | label = tags[2] | ||
| 416 | } | ||
| 417 | } | ||
| 418 | return | ||
| 419 | } | ||
| 420 | |||
| 409 | func isStructPtr(t reflect.Type) bool { | 421 | func isStructPtr(t reflect.Type) bool { |
| 410 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct | 422 | return t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct |
| 411 | } | 423 | } | ... | ... |
| ... | @@ -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 | } | ... | ... |
-
Please register or sign in to post a comment