Convert str to int if it is when set field

This commit is contained in:
Jinzhu 2013-11-01 11:17:19 +08:00
parent c387c7d9ba
commit 32dcacc70b
1 changed files with 4 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"regexp" "regexp"
"strconv"
"time" "time"
) )
@ -244,6 +245,9 @@ func setFieldValue(field reflect.Value, value interface{}) {
if field.IsValid() { if field.IsValid() {
switch field.Kind() { switch field.Kind() {
case reflect.Int, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int32, reflect.Int64:
if str, ok := value.(string); ok {
value, _ = strconv.Atoi(str)
}
field.SetInt(reflect.ValueOf(value).Int()) field.SetInt(reflect.ValueOf(value).Int())
default: default:
field.Set(reflect.ValueOf(value)) field.Set(reflect.ValueOf(value))