From 5c859466efde26344c882c20301e839f2d65da9d Mon Sep 17 00:00:00 2001 From: miraclesu Date: Sun, 11 Aug 2013 22:20:05 +0800 Subject: [PATCH 1/2] ignore struct field if form tag value is '-' --- utils.go | 2 ++ utils_test.go | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/utils.go b/utils.go index 0afcb4ba..df23cb8e 100644 --- a/utils.go +++ b/utils.go @@ -195,6 +195,8 @@ func ParseForm(form url.Values, obj interface{}) error { var tag string if len(tags) == 0 || len(tags[0]) == 0 { tag = fieldT.Name + } else if tags[0] == "-" { + continue } else { tag = tags[0] } diff --git a/utils_test.go b/utils_test.go index 994846fe..6075b445 100644 --- a/utils_test.go +++ b/utils_test.go @@ -104,8 +104,8 @@ func TestInSlice(t *testing.T) { func TestParseForm(t *testing.T) { type user struct { - Id int - tag string `form:tag` + Id int `form:"-"` + tag string `form:"tag"` Name interface{} `form:"username"` Age int `form:"age,text"` Email string @@ -114,6 +114,8 @@ func TestParseForm(t *testing.T) { u := user{} form := url.Values{ + "Id": []string{"1"}, + "-": []string{"1"}, "tag": []string{"no"}, "username": []string{"test"}, "age": []string{"40"}, From 459b97858c4a8c26cfd1b4d89907606eb3263108 Mon Sep 17 00:00:00 2001 From: miraclesu Date: Sun, 11 Aug 2013 22:42:35 +0800 Subject: [PATCH 2/2] renderform ignore struct field if form tag value is '-' --- utils.go | 22 +++++++++++++--------- utils_test.go | 9 +++++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/utils.go b/utils.go index df23cb8e..0ab108ff 100644 --- a/utils.go +++ b/utils.go @@ -282,19 +282,23 @@ func RenderForm(obj interface{}) template.HTML { fieldT := objT.Field(i) tags := strings.Split(fieldT.Tag.Get("form"), ",") name := fieldT.Name - if len(tags) < 2 { - if len(tags) == 1 && len(tags[0]) > 0 { - name = tags[0] - } - raw = append(raw, fmt.Sprintf(`%v: `, - fieldT.Name, name, fieldV.Interface())) - } else { + fType := "text" + if len(tags) > 0 && tags[0] == "-" { + continue + } + + if len(tags) == 1 && len(tags[0]) > 0 { + name = tags[0] + } else if len(tags) >= 2 { if len(tags[0]) > 0 { name = tags[0] } - raw = append(raw, fmt.Sprintf(`%v: `, - fieldT.Name, name, tags[1], fieldV.Interface())) + if len(tags[1]) > 0 { + fType = tags[1] + } } + raw = append(raw, fmt.Sprintf(`%v: `, + fieldT.Name, name, fType, fieldV.Interface())) } return template.HTML(strings.Join(raw, "
")) } diff --git a/utils_test.go b/utils_test.go index 6075b445..22416ffe 100644 --- a/utils_test.go +++ b/utils_test.go @@ -150,10 +150,11 @@ func TestParseForm(t *testing.T) { func TestRenderForm(t *testing.T) { type user struct { - Id int - tag string `form:tag` + Id int `form:"-"` + tag string `form:"tag"` Name interface{} `form:"username"` Age int `form:"age,text"` + Sex string Email []string Intro string `form:",textarea"` } @@ -165,9 +166,9 @@ func TestRenderForm(t *testing.T) { } output = RenderForm(&u) result := template.HTML( - `Id:
` + - `Name:
` + + `Name:
` + `Age:
` + + `Sex:
` + `Intro: `) if output != result { t.Errorf("output should equal `%v` but got `%v`", result, output)