Added support for unsigned integers in binding parameters of form posts. Also changed parsing of integer fields to take into account the size of the fields.

This commit is contained in:
Ethan Kan 2015-02-09 15:13:05 -08:00
parent b1758d3bfa
commit 70f280f880
2 changed files with 82 additions and 10 deletions

View File

@ -98,18 +98,54 @@ func mapForm(ptr interface{}, form map[string][]string) error {
return nil
}
func setIntField(val string, bitSize int, structField reflect.Value) error {
if val == "" {
val = "0"
}
intVal, err := strconv.ParseInt(val, 10, bitSize)
if err == nil {
structField.SetInt(intVal)
}
return err
}
func setUintField(val string, bitSize int, structField reflect.Value) error {
if val == "" {
val = "0"
}
uintVal, err := strconv.ParseUint(val, 10, bitSize)
if err == nil {
structField.SetUint(uintVal)
}
return err
}
func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value) error {
switch valueKind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if val == "" {
val = "0"
}
intVal, err := strconv.Atoi(val)
if err != nil {
return err
} else {
structField.SetInt(int64(intVal))
}
case reflect.Int:
return setIntField(val, 0, structField)
case reflect.Int8:
return setIntField(val, 8, structField)
case reflect.Int16:
return setIntField(val, 16, structField)
case reflect.Int32:
return setIntField(val, 32, structField)
case reflect.Int64:
return setIntField(val, 64, structField)
case reflect.Uint:
return setUintField(val, 0, structField)
case reflect.Uint8:
return setUintField(val, 8, structField)
case reflect.Uint16:
return setUintField(val, 16, structField)
case reflect.Uint32:
return setUintField(val, 32, structField)
case reflect.Uint64:
return setUintField(val, 64, structField)
case reflect.Bool:
if val == "" {
val = "false"

View File

@ -441,6 +441,42 @@ func TestBindingJSONMalformed(t *testing.T) {
}
}
func TestBindingForm(t *testing.T) {
body := bytes.NewBuffer([]byte("foo=bar&num=123&unum=1234567890"))
r := New()
r.POST("/binding/form", func(c *Context) {
var body struct {
Foo string `form:"foo"`
Num int `form:"num"`
Unum uint `form:"unum"`
}
if c.Bind(&body) {
c.JSON(200, H{"foo": body.Foo, "num": body.Num, "unum": body.Unum})
}
})
req, _ := http.NewRequest("POST", "/binding/form", body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Errorf("Response code should be Ok, was: %d", w.Code)
}
expected := "{\"foo\":\"bar\",\"num\":123,\"unum\":1234567890}\n"
if w.Body.String() != expected {
t.Errorf("Response should be %s, was %s", expected, w.Body.String())
}
if w.HeaderMap.Get("Content-Type") != "application/json; charset=utf-8" {
t.Errorf("Content-Type should be application/json, was %s", w.HeaderMap.Get("Content-Type"))
}
}
func TestClientIP(t *testing.T) {
r := New()