diff --git a/a_basic_func.go b/a_basic_func.go index cb89d28..4aee062 100644 --- a/a_basic_func.go +++ b/a_basic_func.go @@ -21,7 +21,7 @@ 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 } @@ -29,7 +29,7 @@ func isString(refVal reflect.Value) bool { // 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 } @@ -37,7 +37,7 @@ func isArray(refVal reflect.Value) bool { // 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 } @@ -45,7 +45,7 @@ func isBytes(refVal reflect.Value) bool { // 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 } @@ -53,7 +53,7 @@ func isMap(refVal reflect.Value) bool { // 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 } @@ -61,7 +61,7 @@ func isStruct(refVal reflect.Value) bool { // 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 @@ -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, @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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{}) @@ -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: @@ -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: @@ -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()) @@ -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: @@ -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() @@ -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()))) @@ -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()))) diff --git a/serializer_gorm.go b/serializer_gorm.go index 08bdafe..0d1fad9 100644 --- a/serializer_gorm.go +++ b/serializer_gorm.go @@ -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) { diff --git a/test/test_int_test.go b/test/test_int_test.go index d5a807f..acfcc36 100644 --- a/test/test_int_test.go +++ b/test/test_int_test.go @@ -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) + } + }) }