ignore struct field if form tag value is '-'
Showing
2 changed files
with
6 additions
and
2 deletions
| ... | @@ -195,6 +195,8 @@ func ParseForm(form url.Values, obj interface{}) error { | ... | @@ -195,6 +195,8 @@ func ParseForm(form url.Values, obj interface{}) error { |
| 195 | var tag string | 195 | var tag string |
| 196 | if len(tags) == 0 || len(tags[0]) == 0 { | 196 | if len(tags) == 0 || len(tags[0]) == 0 { |
| 197 | tag = fieldT.Name | 197 | tag = fieldT.Name |
| 198 | } else if tags[0] == "-" { | ||
| 199 | continue | ||
| 198 | } else { | 200 | } else { |
| 199 | tag = tags[0] | 201 | tag = tags[0] |
| 200 | } | 202 | } | ... | ... |
| ... | @@ -104,8 +104,8 @@ func TestInSlice(t *testing.T) { | ... | @@ -104,8 +104,8 @@ func TestInSlice(t *testing.T) { |
| 104 | 104 | ||
| 105 | func TestParseForm(t *testing.T) { | 105 | func TestParseForm(t *testing.T) { |
| 106 | type user struct { | 106 | type user struct { |
| 107 | Id int | 107 | Id int `form:"-"` |
| 108 | tag string `form:tag` | 108 | tag string `form:"tag"` |
| 109 | Name interface{} `form:"username"` | 109 | Name interface{} `form:"username"` |
| 110 | Age int `form:"age,text"` | 110 | Age int `form:"age,text"` |
| 111 | Email string | 111 | Email string |
| ... | @@ -114,6 +114,8 @@ func TestParseForm(t *testing.T) { | ... | @@ -114,6 +114,8 @@ func TestParseForm(t *testing.T) { |
| 114 | 114 | ||
| 115 | u := user{} | 115 | u := user{} |
| 116 | form := url.Values{ | 116 | form := url.Values{ |
| 117 | "Id": []string{"1"}, | ||
| 118 | "-": []string{"1"}, | ||
| 117 | "tag": []string{"no"}, | 119 | "tag": []string{"no"}, |
| 118 | "username": []string{"test"}, | 120 | "username": []string{"test"}, |
| 119 | "age": []string{"40"}, | 121 | "age": []string{"40"}, | ... | ... |
-
Please register or sign in to post a comment