0c31c2d6 by Sergey Shcherbina

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

1 parent f988f035
...@@ -358,11 +358,32 @@ func ParseForm(form url.Values, obj interface{}) error { ...@@ -358,11 +358,32 @@ func ParseForm(form url.Values, obj interface{}) error {
358 } 358 }
359 fieldV.Set(reflect.ValueOf(t)) 359 fieldV.Set(reflect.ValueOf(t))
360 } 360 }
361 case reflect.Slice:
362 if fieldT.Type == sliceOfInts {
363 formVals := form[tag]
364 fieldV.Set(reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf(int(1))), len(formVals), len(formVals)))
365 for i := 0; i < len(formVals); i++ {
366 val, err := strconv.Atoi(formVals[i])
367 if err != nil {
368 return err
369 }
370 fieldV.Index(i).SetInt(int64(val))
371 }
372 } else if fieldT.Type == sliceOfStrings {
373 formVals := form[tag]
374 fieldV.Set(reflect.MakeSlice(reflect.SliceOf(reflect.TypeOf("")), len(formVals), len(formVals)))
375 for i := 0; i < len(formVals); i++ {
376 fieldV.Index(i).SetString(formVals[i])
377 }
378 }
361 } 379 }
362 } 380 }
363 return nil 381 return nil
364 } 382 }
365 383
384 var sliceOfInts = reflect.TypeOf([]int(nil))
385 var sliceOfStrings = reflect.TypeOf([]string(nil))
386
366 var unKind = map[reflect.Kind]bool{ 387 var unKind = map[reflect.Kind]bool{
367 reflect.Uintptr: true, 388 reflect.Uintptr: true,
368 reflect.Complex64: true, 389 reflect.Complex64: true,
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!