1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 18:30:50 +00:00

Added support to parse slices of ints and strings in ParseForm func

This commit is contained in:
Sergey Shcherbina 2015-02-22 22:13:06 +05:00
parent f988f035e5
commit 0c31c2d689

View File

@ -358,11 +358,32 @@ func ParseForm(form url.Values, obj interface{}) error {
}
fieldV.Set(reflect.ValueOf(t))
}
case reflect.Slice:
if fieldT.Type == sliceOfInts {
formVals := form[tag]
fieldV.Set(reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(int(1))), len(formVals), len(formVals)))
for i := 0; i < len(formVals); i++ {
val, err := strconv.Atoi(formVals[i])
if err != nil {
return err
}
fieldV.Index(i).SetInt(int64(val))
}
} else if fieldT.Type == sliceOfStrings {
formVals := form[tag]
fieldV.Set(reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf("")), len(formVals), len(formVals)))
for i := 0; i < len(formVals); i++ {
fieldV.Index(i).SetString(formVals[i])
}
}
}
}
return nil
}
var sliceOfInts = reflect.TypeOf([]int(nil))
var sliceOfStrings = reflect.TypeOf([]string(nil))
var unKind = map[reflect.Kind]bool{
reflect.Uintptr: true,
reflect.Complex64: true,