diff options
Diffstat (limited to 'vendor/github.com/go-xorm/xorm/helpers.go')
-rw-r--r-- | vendor/github.com/go-xorm/xorm/helpers.go | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/vendor/github.com/go-xorm/xorm/helpers.go b/vendor/github.com/go-xorm/xorm/helpers.go index a015ca50b9..4e26e84143 100644 --- a/vendor/github.com/go-xorm/xorm/helpers.go +++ b/vendor/github.com/go-xorm/xorm/helpers.go @@ -17,74 +17,83 @@ import ( ) // str2PK convert string value to primary key value according to tp -func str2PK(s string, tp reflect.Type) (interface{}, error) { +func str2PKValue(s string, tp reflect.Type) (reflect.Value, error) { var err error var result interface{} + var defReturn = reflect.Zero(tp) + switch tp.Kind() { case reflect.Int: result, err = strconv.Atoi(s) if err != nil { - return nil, errors.New("convert " + s + " as int: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as int: %s", s, err.Error()) } case reflect.Int8: x, err := strconv.Atoi(s) if err != nil { - return nil, errors.New("convert " + s + " as int16: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as int8: %s", s, err.Error()) } result = int8(x) case reflect.Int16: x, err := strconv.Atoi(s) if err != nil { - return nil, errors.New("convert " + s + " as int16: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as int16: %s", s, err.Error()) } result = int16(x) case reflect.Int32: x, err := strconv.Atoi(s) if err != nil { - return nil, errors.New("convert " + s + " as int32: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as int32: %s", s, err.Error()) } result = int32(x) case reflect.Int64: result, err = strconv.ParseInt(s, 10, 64) if err != nil { - return nil, errors.New("convert " + s + " as int64: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as int64: %s", s, err.Error()) } case reflect.Uint: x, err := strconv.ParseUint(s, 10, 64) if err != nil { - return nil, errors.New("convert " + s + " as uint: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as uint: %s", s, err.Error()) } result = uint(x) case reflect.Uint8: x, err := strconv.ParseUint(s, 10, 64) if err != nil { - return nil, errors.New("convert " + s + " as uint8: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as uint8: %s", s, err.Error()) } result = uint8(x) case reflect.Uint16: x, err := strconv.ParseUint(s, 10, 64) if err != nil { - return nil, errors.New("convert " + s + " as uint16: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as uint16: %s", s, err.Error()) } result = uint16(x) case reflect.Uint32: x, err := strconv.ParseUint(s, 10, 64) if err != nil { - return nil, errors.New("convert " + s + " as uint32: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as uint32: %s", s, err.Error()) } result = uint32(x) case reflect.Uint64: result, err = strconv.ParseUint(s, 10, 64) if err != nil { - return nil, errors.New("convert " + s + " as uint64: " + err.Error()) + return defReturn, fmt.Errorf("convert %s as uint64: %s", s, err.Error()) } case reflect.String: result = s default: - panic("unsupported convert type") + return defReturn, errors.New("unsupported convert type") } - result = reflect.ValueOf(result).Convert(tp).Interface() - return result, nil + return reflect.ValueOf(result).Convert(tp), nil +} + +func str2PK(s string, tp reflect.Type) (interface{}, error) { + v, err := str2PKValue(s, tp) + if err != nil { + return nil, err + } + return v.Interface(), nil } func splitTag(tag string) (tags []string) { |