From e47b2b677db77b7dcb9abd62f3cb6d622689cfbd Mon Sep 17 00:00:00 2001 From: miraclesu Date: Sat, 10 Aug 2013 11:42:25 +0800 Subject: [PATCH] Update ParserForm for new form tag style --- utils.go | 9 +++++++-- utils_test.go | 15 ++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/utils.go b/utils.go index 35f1aeb0..e0ead258 100644 --- a/utils.go +++ b/utils.go @@ -190,10 +190,15 @@ func ParseForm(form url.Values, obj interface{}) error { continue } fieldT := objT.Field(i) - tag := fieldT.Tag.Get("form") - if len(tag) == 0 { + + tags := strings.Split(fieldT.Tag.Get("form"), ",") + var tag string + if len(tags) == 0 || len(tags[0]) == 0 { tag = fieldT.Name + } else { + tag = tags[0] } + value := form.Get(tag) if len(value) == 0 { continue diff --git a/utils_test.go b/utils_test.go index d5750ee6..405c939c 100644 --- a/utils_test.go +++ b/utils_test.go @@ -106,8 +106,9 @@ func TestParseForm(t *testing.T) { Id int tag string `form:tag` Name interface{} `form:"username"` - Age int `form:"age"` + Age int `form:"age,text"` Email string + Intro string `form:",textarea"` } u := user{} @@ -116,6 +117,7 @@ func TestParseForm(t *testing.T) { "username": []string{"test"}, "age": []string{"40"}, "Email": []string{"test@gmail.com"}, + "Intro": []string{"I am an engineer!"}, } if err := ParseForm(form, u); err == nil { t.Fatal("nothing will be changed") @@ -127,15 +129,18 @@ func TestParseForm(t *testing.T) { t.Errorf("Id should equal 0 but got %v", u.Id) } if len(u.tag) != 0 { - t.Error("tag's length should equal 0 but got %v", len(u.tag)) + t.Errorf("tag's length should equal 0 but got %v", len(u.tag)) } if u.Name.(string) != "test" { - t.Error("Name should equal `test` but got `%v`", u.Name.(string)) + t.Errorf("Name should equal `test` but got `%v`", u.Name.(string)) } if u.Age != 40 { - t.Error("Age should equal 40 but got %v", u.Age) + t.Errorf("Age should equal 40 but got %v", u.Age) } if u.Email != "test@gmail.com" { - t.Error("Email should equal `test@gmail.com` but got `%v`", u.Email) + t.Errorf("Email should equal `test@gmail.com` but got `%v`", u.Email) + } + if u.Intro != "I am an engineer!" { + t.Errorf("Intro should equal `I am an engineer!` but got `%v`", u.Intro) } }