Skip to content

Commit

Permalink
fix ptr data value problem
Browse files Browse the repository at this point in the history
  • Loading branch information
li.fusheng committed Aug 13, 2024
1 parent a10b2a5 commit 3229e5e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
59 changes: 31 additions & 28 deletions a_basic_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,47 @@ func isInterface(refVal reflect.Value) bool {
// isString is_string
func isString(refVal reflect.Value) bool {
if isPtr(refVal) {
return isString(reflect.ValueOf(refVal.Elem().Interface()))
return isString(refVal.Elem())
}
return refVal.Kind() == reflect.String
}

// isArray is_array
func isArray(refVal reflect.Value) bool {
if isPtr(refVal) {
return isArray(reflect.ValueOf(refVal.Elem().Interface()))
return isArray(refVal.Elem())
}
return refVal.Kind() == reflect.Slice || refVal.Kind() == reflect.Array
}

// isBytes is_bytes
func isBytes(refVal reflect.Value) bool {
if isPtr(refVal) {
return isBytes(reflect.ValueOf(refVal.Elem().Interface()))
return isBytes(refVal.Elem())
}
return refVal.Kind() == reflect.Slice && refVal.Type().Elem().Kind() == reflect.Uint8
}

// isMap is_map
func isMap(refVal reflect.Value) bool {
if isPtr(refVal) {
return isMap(reflect.ValueOf(refVal.Elem().Interface()))
return isMap(refVal.Elem())
}
return refVal.Kind() == reflect.Map
}

// isStruct is_struct
func isStruct(refVal reflect.Value) bool {
if isPtr(refVal) {
return isStruct(reflect.ValueOf(refVal.Elem().Interface()))
return isStruct(refVal.Elem())
}
return refVal.Kind() == reflect.Struct
}

// isBoolean is_bool
func isBoolean(refVal reflect.Value) bool {
if isPtr(refVal) {
return isBoolean(reflect.ValueOf(refVal.Elem().Interface()))
return isBoolean(refVal.Elem())
}
return refVal.Kind() == reflect.Bool

Expand All @@ -70,7 +70,7 @@ func isBoolean(refVal reflect.Value) bool {
// isInt is_int
func isInt(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt(reflect.ValueOf(refVal.Elem().Interface()))
return isInt(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
Expand All @@ -83,7 +83,7 @@ func isInt(refVal reflect.Value) bool {
// isInt8 is_int8
func isInt8(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt8(reflect.ValueOf(refVal.Elem().Interface()))
return isInt8(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int8:
Expand All @@ -95,7 +95,7 @@ func isInt8(refVal reflect.Value) bool {
// isInt16 is_int16
func isInt16(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt16(reflect.ValueOf(refVal.Elem().Interface()))
return isInt16(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int16:
Expand All @@ -107,7 +107,7 @@ func isInt16(refVal reflect.Value) bool {
// isInt32 is_int32
func isInt32(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt32(reflect.ValueOf(refVal.Elem().Interface()))
return isInt32(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int32:
Expand All @@ -119,7 +119,7 @@ func isInt32(refVal reflect.Value) bool {
// isInt64 is_int64
func isInt64(refVal reflect.Value) bool {
if isPtr(refVal) {
return isInt64(reflect.ValueOf(refVal.Elem().Interface()))
return isInt64(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Int64:
Expand All @@ -131,7 +131,7 @@ func isInt64(refVal reflect.Value) bool {
// isUint is_uint
func isUint(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint(reflect.ValueOf(refVal.Elem().Interface()))
return isUint(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
Expand All @@ -141,7 +141,7 @@ func isUint(refVal reflect.Value) bool {
}
func isUint8(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint8(reflect.ValueOf(refVal.Elem().Interface()))
return isUint8(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint8:
Expand All @@ -153,7 +153,7 @@ func isUint8(refVal reflect.Value) bool {
// isUint16 is_uint16
func isUint16(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint16(reflect.ValueOf(refVal.Elem().Interface()))
return isUint16(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint16:
Expand All @@ -165,7 +165,7 @@ func isUint16(refVal reflect.Value) bool {
// isUint32 is_uint32
func isUint32(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint32(reflect.ValueOf(refVal.Elem().Interface()))
return isUint32(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint32:
Expand All @@ -177,7 +177,7 @@ func isUint32(refVal reflect.Value) bool {
// isUint64 is_uint64
func isUint64(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUint64(reflect.ValueOf(refVal.Elem().Interface()))
return isUint64(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uint64:
Expand All @@ -189,7 +189,7 @@ func isUint64(refVal reflect.Value) bool {
// isUintptr is_uintptr
func isUintptr(refVal reflect.Value) bool {
if isPtr(refVal) {
return isUintptr(reflect.ValueOf(refVal.Elem().Interface()))
return isUintptr(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Uintptr:
Expand All @@ -201,7 +201,7 @@ func isUintptr(refVal reflect.Value) bool {
// isFloat is_float
func isFloat(refVal reflect.Value) bool {
if isPtr(refVal) {
return isFloat(reflect.ValueOf(refVal.Elem().Interface()))
return isFloat(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Float32, reflect.Float64:
Expand All @@ -213,7 +213,7 @@ func isFloat(refVal reflect.Value) bool {
// isFloat32 is_float32
func isFloat32(refVal reflect.Value) bool {
if isPtr(refVal) {
return isFloat(reflect.ValueOf(refVal.Elem().Interface()))
return isFloat(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Float32:
Expand All @@ -225,7 +225,7 @@ func isFloat32(refVal reflect.Value) bool {
// isFloat64 is_float64
func isFloat64(refVal reflect.Value) bool {
if isPtr(refVal) {
return isFloat(reflect.ValueOf(refVal.Elem().Interface()))
return isFloat(refVal.Elem())
}
switch refVal.Kind() {
case reflect.Float64:
Expand All @@ -236,13 +236,16 @@ func isFloat64(refVal reflect.Value) bool {

// isNil is_nil
func isNil(refVal reflect.Value) bool {
if isPtr(refVal) {
return isNil(refVal.Elem())
}
return refVal.Kind() == reflect.Invalid
}

// isTime is_time
func isTime(refVal reflect.Value) bool {
if isPtr(refVal) {
return isTime(reflect.ValueOf(refVal.Elem().Interface()))
return isTime(refVal.Elem())
}
if refVal.Kind() == reflect.Struct {
timeType := reflect.TypeOf(time.Time{})
Expand All @@ -258,7 +261,7 @@ func isTime(refVal reflect.Value) bool {
func isNumeric(refVal reflect.Value) bool {
switch refVal.Kind() {
case reflect.Ptr:
return isNumeric(reflect.ValueOf(refVal.Elem().Interface()))
return isNumeric(refVal.Elem())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Float32, reflect.Float64:
Expand Down Expand Up @@ -323,7 +326,7 @@ func toFloat64(refVal reflect.Value) (float64, error) {
case reflect.Invalid:
return 0, nil
case reflect.Ptr:
return toFloat64(reflect.ValueOf(refVal.Elem().Interface()))
return toFloat64(refVal.Elem())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(refVal.Uint()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
Expand Down Expand Up @@ -360,7 +363,7 @@ func toBoolean(refVal reflect.Value) (bool, error) {
case reflect.Invalid:
return false, nil
case reflect.Ptr:
return toBoolean(reflect.ValueOf(refVal.Elem().Interface()))
return toBoolean(refVal.Elem())
case reflect.String:
// val to lower
val = strings.ToLower(refVal.String())
Expand Down Expand Up @@ -396,7 +399,7 @@ func toString(refVal reflect.Value) (string, error) {
case reflect.Invalid:
return "", nil
case reflect.Ptr:
return toString(reflect.ValueOf(refVal.Elem().Interface()))
return toString(refVal.Elem())
case reflect.String:
return refVal.String(), nil
case reflect.Slice:
Expand Down Expand Up @@ -431,7 +434,7 @@ func toTime(refVal reflect.Value) (time.Time, error) {
return time.Time{}, newError(refVal)
}
if isPtr(refVal) {
return toTime(reflect.ValueOf(refVal.Elem().Interface()))
return toTime(refVal.Elem())
}
var val any
val = refVal.Interface()
Expand All @@ -449,7 +452,7 @@ func toArray(refVal reflect.Value) ([]any, error) {
case reflect.Invalid:
return nil, nil
case reflect.Ptr:
return toArray(reflect.ValueOf(refVal.Elem().Interface()))
return toArray(refVal.Elem())
case reflect.Slice:
if isBytes(refVal) {
return toArray(reflect.ValueOf(string(refVal.Bytes())))
Expand Down Expand Up @@ -482,7 +485,7 @@ func toMap(refVal reflect.Value) (map[string]any, error) {
case reflect.Invalid:
return nil, nil
case reflect.Ptr:
return toMap(reflect.ValueOf(refVal.Elem().Interface()))
return toMap(refVal.Elem())
case reflect.Slice:
if isBytes(refVal) {
return toMap(reflect.ValueOf(string(refVal.Bytes())))
Expand Down
2 changes: 1 addition & 1 deletion serializer_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c *DataRaw) Value(ctx context.Context, field *schema.Field, dst reflect.Va
refVal := reflect.ValueOf(fieldValue)
switch refVal.Kind() {
case reflect.Ptr:
return c.Value(ctx, field, dst, refVal.Elem().Interface())
return c.Value(ctx, field, dst, refVal.Elem())
default:
val := refVal.Interface()
switch val.(type) {
Expand Down
8 changes: 8 additions & 0 deletions test/test_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,12 @@ func TestParseInt64(t *testing.T) {
t.Fatalf("it should be error: %v", err)
}
})
t.Run("NIL", func(t *testing.T) {
i := new(int64)
i = nil
convertedNum, _ := cdt.NewConvert(i).ToInt64E()
if convertedNum != 0 {
t.Fatalf("return must be 0 instead of %v", convertedNum)
}
})
}

0 comments on commit 3229e5e

Please sign in to comment.