diff --git a/mathutil/convert.go b/mathutil/convert.go index 4778cb90a..4034e3918 100644 --- a/mathutil/convert.go +++ b/mathutil/convert.go @@ -3,7 +3,6 @@ package mathutil import ( "encoding/json" "fmt" - "math" "strconv" "strings" "time" @@ -20,12 +19,6 @@ func Int(in any) (int, error) { return ToInt(in) } -// SafeInt convert value to int, will ignore error -func SafeInt(in any) int { - val, _ := ToInt(in) - return val -} - // QuietInt convert value to int, will ignore error func QuietInt(in any) int { val, _ := ToInt(in) @@ -34,10 +27,7 @@ func QuietInt(in any) int { // MustInt convert value to int, will panic on error func MustInt(in any) int { - val, err := ToInt(in) - if err != nil { - panic(err) - } + val, _ := ToInt(in) return val } @@ -69,54 +59,29 @@ func ToInt(in any) (iVal int, err error) { case int32: iVal = int(tVal) case int64: - if tVal > math.MaxInt32 { - err = fmt.Errorf("value overflow int32. input: %v", tVal) - } else { - iVal = int(tVal) - } + iVal = int(tVal) case uint: - if tVal > math.MaxInt32 { - err = fmt.Errorf("value overflow int32. input: %v", tVal) - } else { - iVal = int(tVal) - } + iVal = int(tVal) case uint8: iVal = int(tVal) case uint16: iVal = int(tVal) case uint32: - if tVal > math.MaxInt32 { - err = fmt.Errorf("value overflow int32. input: %v", tVal) - } else { - iVal = int(tVal) - } + iVal = int(tVal) case uint64: - if tVal > math.MaxInt32 { - err = fmt.Errorf("value overflow int32. input: %v", tVal) - } else { - iVal = int(tVal) - } + iVal = int(tVal) case float32: iVal = int(tVal) case float64: iVal = int(tVal) case time.Duration: - if tVal > math.MaxInt32 { - err = fmt.Errorf("value overflow int32. input: %v", tVal) - } else { - iVal = int(tVal) - } + iVal = int(tVal) case string: iVal, err = strconv.Atoi(strings.TrimSpace(tVal)) case json.Number: var i64 int64 - if i64, err = tVal.Int64(); err == nil { - if i64 > math.MaxInt32 { - err = fmt.Errorf("value overflow int32. input: %v", tVal) - } else { - iVal = int(i64) - } - } + i64, err = tVal.Int64() + iVal = int(i64) default: err = comdef.ErrConvType } @@ -138,12 +103,6 @@ func Uint(in any) (uint64, error) { return ToUint(in) } -// SafeUint convert string to uint, will ignore error -func SafeUint(in any) uint64 { - val, _ := ToUint(in) - return val -} - // QuietUint convert string to uint, will ignore error func QuietUint(in any) uint64 { val, _ := ToUint(in) @@ -152,10 +111,7 @@ func QuietUint(in any) uint64 { // MustUint convert string to uint, will panic on error func MustUint(in any) uint64 { - val, err := ToUint(in) - if err != nil { - panic(err) - } + val, _ := ToUint(in) return val } @@ -202,11 +158,6 @@ func ToUint(in any) (u64 uint64, err error) { case string: u64, err = strconv.ParseUint(strings.TrimSpace(tVal), 10, 0) default: - // if iface, ok := in.(comdef.Int64able); ok { - // var i64 int64 - // i64, err = iface.Int64() - // u64 = uint64(i64) - // } else { err = comdef.ErrConvType } return @@ -235,14 +186,11 @@ func QuietInt64(in any) int64 { // MustInt64 convert value to int64, will panic on error func MustInt64(in any) int64 { - i64, err := ToInt64(in) - if err != nil { - panic(err) - } + i64, _ := ToInt64(in) return i64 } -// TODO AsInt64 strict convert to int64 +// TODO StrictInt64,AsInt64 strict convert to int64 // Int64OrErr convert string to int64, return error on failed func Int64OrErr(in any) (int64, error) { @@ -309,12 +257,9 @@ func FloatOrPanic(in any) float64 { return val } -// MustFloat convert value to float64, will panic on error +// MustFloat convert value to float64 TODO will panic on error func MustFloat(in any) float64 { - val, err := ToFloat(in) - if err != nil { - panic(err) - } + val, _ := ToFloat(in) return val } @@ -383,13 +328,19 @@ func StringOrPanic(val any) string { } // MustString convert intX/floatX value to string, will panic on error -func MustString(val any) string { return StringOrPanic(val) } +func MustString(val any) string { + return StringOrPanic(val) +} // ToString convert intX/floatX value to string, return error on failed -func ToString(val any) (string, error) { return TryToString(val, true) } +func ToString(val any) (string, error) { + return TryToString(val, true) +} // StringOrErr convert intX/floatX value to string, return error on failed -func StringOrErr(val any) (string, error) { return TryToString(val, true) } +func StringOrErr(val any) (string, error) { + return TryToString(val, true) +} // QuietString convert intX/floatX value to string, other type convert by fmt.Sprint func QuietString(val any) string { diff --git a/mathutil/convert_test.go b/mathutil/convert_test.go index 5c1d37a8a..c8aea30e6 100644 --- a/mathutil/convert_test.go +++ b/mathutil/convert_test.go @@ -2,7 +2,6 @@ package mathutil_test import ( "encoding/json" - "math" "testing" "time" @@ -23,115 +22,77 @@ func TestToInt(t *testing.T) { json.Number("2"), } errTests := []any{ + nil, "2a", []int{1}, } - overTests := []any{ - // case for overflow - int64(math.MaxInt32 + 1), - uint(math.MaxInt32 + 1), - uint32(math.MaxInt32 + 1), - uint64(math.MaxInt32 + 1), - time.Duration(math.MaxInt32 + 1), - json.Number("2147483648"), - } - // To int - t.Run("To int", func(t *testing.T) { - intVal, err := mathutil.Int("2") - is.Nil(err) - is.Eq(2, intVal) - - intVal, err = mathutil.ToInt("-2") - is.Nil(err) - is.Eq(-2, intVal) - - is.Eq(2, mathutil.StrInt("2")) - - intVal, err = mathutil.IntOrErr("-2") - is.Nil(err) - is.Eq(-2, intVal) - - is.Eq(0, mathutil.SafeInt(nil)) - is.Eq(-2, mathutil.MustInt("-2")) - is.Eq(-2, mathutil.IntOrPanic("-2")) - for _, in := range tests { - is.Eq(2, mathutil.MustInt(in)) - is.Eq(2, mathutil.QuietInt(in)) - } - for _, in := range errTests { - is.Eq(0, mathutil.SafeInt(in)) - } - for _, in := range overTests { - intVal, err = mathutil.ToInt(in) - is.Err(err, "input: %v", in) - is.Eq(0, intVal) - } - - is.Panics(func() { - mathutil.MustInt([]int{23}) - }) - is.Panics(func() { - mathutil.IntOrPanic([]int{23}) - }) - }) + intVal, err := mathutil.Int("2") + is.Nil(err) + is.Eq(2, intVal) + + intVal, err = mathutil.ToInt("-2") + is.Nil(err) + is.Eq(-2, intVal) + + is.Eq(2, mathutil.StrInt("2")) + + intVal, err = mathutil.IntOrErr("-2") + is.Nil(err) + is.Eq(-2, intVal) + + is.Eq(-2, mathutil.MustInt("-2")) + for _, in := range tests { + is.Eq(2, mathutil.MustInt(in)) + is.Eq(2, mathutil.QuietInt(in)) + } + for _, in := range errTests { + is.Eq(0, mathutil.MustInt(in)) + } // To uint - t.Run("To uint", func(t *testing.T) { - uintVal, err := mathutil.Uint("2") - is.Nil(err) - is.Eq(uint64(2), uintVal) - - uintVal, err = mathutil.UintOrErr("2") - is.Nil(err) - is.Eq(uint64(2), uintVal) - - _, err = mathutil.ToUint("-2") - is.Err(err) - - is.Eq(uint64(0), mathutil.QuietUint("-2")) - for _, in := range tests { - is.Eq(uint64(2), mathutil.SafeUint(in)) - } - for _, in := range errTests { - is.Eq(uint64(0), mathutil.QuietUint(in)) - } - - is.Eq(uint64(0), mathutil.QuietUint(nil)) - is.Eq(uint64(2), mathutil.MustUint("2")) - is.Panics(func() { - mathutil.MustUint([]int{23}) - }) - }) + uintVal, err := mathutil.Uint("2") + is.Nil(err) + is.Eq(uint64(2), uintVal) + + uintVal, err = mathutil.UintOrErr("2") + is.Nil(err) + is.Eq(uint64(2), uintVal) + + _, err = mathutil.ToUint("-2") + is.Err(err) + + is.Eq(uint64(0), mathutil.MustUint("-2")) + for _, in := range tests { + is.Eq(uint64(2), mathutil.MustUint(in)) + } + for _, in := range errTests { + is.Eq(uint64(0), mathutil.QuietUint(in)) + is.Eq(uint64(0), mathutil.MustUint(in)) + } // To int64 - t.Run("To int64", func(t *testing.T) { - i64Val, err := mathutil.ToInt64("2") - is.Nil(err) - is.Eq(int64(2), i64Val) - - i64Val, err = mathutil.Int64("-2") - is.Nil(err) - is.Eq(int64(-2), i64Val) - - i64Val, err = mathutil.Int64OrErr("-2") - is.Nil(err) - is.Eq(int64(-2), i64Val) - - for _, in := range tests { - is.Eq(int64(2), mathutil.MustInt64(in)) - } - for _, in := range errTests { - is.Eq(int64(0), mathutil.QuietInt64(in)) - is.Eq(int64(0), mathutil.SafeInt64(in)) - } - - is.Eq(int64(0), mathutil.QuietInt64(nil)) - is.Panics(func() { - mathutil.MustInt64([]int{23}) - }) - }) + i64Val, err := mathutil.ToInt64("2") + is.Nil(err) + is.Eq(int64(2), i64Val) + + i64Val, err = mathutil.Int64("-2") + is.Nil(err) + is.Eq(int64(-2), i64Val) + + i64Val, err = mathutil.Int64OrErr("-2") + is.Nil(err) + is.Eq(int64(-2), i64Val) + + for _, in := range tests { + is.Eq(int64(2), mathutil.MustInt64(in)) + } + for _, in := range errTests { + is.Eq(int64(0), mathutil.MustInt64(in)) + is.Eq(int64(0), mathutil.QuietInt64(in)) + is.Eq(int64(0), mathutil.SafeInt64(in)) + } } func TestToString(t *testing.T) { @@ -164,8 +125,6 @@ func TestToString(t *testing.T) { is.NoErr(err) is.Eq("", val) - is.Eq("[1]", mathutil.QuietString([]int{1})) - is.Panics(func() { mathutil.MustString("2") }) @@ -188,18 +147,9 @@ func TestToFloat(t *testing.T) { } is.Eq(123.5, mathutil.MustFloat("123.5")) - is.Eq(123.5, mathutil.FloatOrPanic("123.5")) is.Eq(123.5, mathutil.QuietFloat("123.5")) - is.Eq(float64(0), mathutil.QuietFloat(nil)) + is.Eq(float64(0), mathutil.MustFloat("invalid")) is.Eq(float64(0), mathutil.QuietFloat("invalid")) - is.Eq(float64(0), mathutil.QuietFloat([]int{23})) - - is.Panics(func() { - mathutil.MustFloat("invalid") - }) - is.Panics(func() { - mathutil.FloatOrPanic("invalid") - }) fltVal, err := mathutil.ToFloat("123.5") is.Nil(err) diff --git a/reflects/conv.go b/reflects/conv.go index 551cc2f91..1dfa303a5 100644 --- a/reflects/conv.go +++ b/reflects/conv.go @@ -2,7 +2,6 @@ package reflects import ( "fmt" - "math" "reflect" "strconv" @@ -73,16 +72,10 @@ func ValueByKind(val any, kind reflect.Kind) (rv reflect.Value, err error) { } case reflect.Int8: if dstV, err1 := mathutil.ToInt(val); err1 == nil { - if dstV > math.MaxInt8 { - return rv, fmt.Errorf("value overflow int8. val: %v", val) - } rv = reflect.ValueOf(int8(dstV)) } case reflect.Int16: if dstV, err1 := mathutil.ToInt(val); err1 == nil { - if dstV > math.MaxInt16 { - return rv, fmt.Errorf("value overflow int16. val: %v", val) - } rv = reflect.ValueOf(int16(dstV)) } case reflect.Int32: @@ -99,23 +92,14 @@ func ValueByKind(val any, kind reflect.Kind) (rv reflect.Value, err error) { } case reflect.Uint8: if dstV, err1 := mathutil.ToUint(val); err1 == nil { - if dstV > math.MaxUint8 { - return rv, fmt.Errorf("value overflow uint8. val: %v", val) - } rv = reflect.ValueOf(uint8(dstV)) } case reflect.Uint16: if dstV, err1 := mathutil.ToUint(val); err1 == nil { - if dstV > math.MaxUint16 { - return rv, fmt.Errorf("value overflow uint16. val: %v", val) - } rv = reflect.ValueOf(uint16(dstV)) } case reflect.Uint32: if dstV, err1 := mathutil.ToUint(val); err1 == nil { - if dstV > math.MaxUint32 { - return rv, fmt.Errorf("value overflow uint32. val: %v", val) - } rv = reflect.ValueOf(uint32(dstV)) } case reflect.Uint64: diff --git a/reflects/conv_test.go b/reflects/conv_test.go index db0c29e06..5a0f3eac2 100644 --- a/reflects/conv_test.go +++ b/reflects/conv_test.go @@ -1,7 +1,6 @@ package reflects_test import ( - "math" "reflect" "testing" @@ -135,28 +134,6 @@ func TestValueByKind(t *testing.T) { assert.Eq(t, e.want, val.Interface()) } - errTests := []struct { - give any - kind reflect.Kind - }{ - {"abc", reflect.Int}, - {true, reflect.Int}, - {23, reflect.Bool}, - // case for overflow - {143, reflect.Int8}, - {math.MaxInt16 + 1, reflect.Int16}, - {343, reflect.Uint8}, - {int64(math.MaxInt32 + 1), reflect.Int32}, - {int64(math.MaxUint16 + 1), reflect.Uint16}, - {int64(math.MaxUint32 + 1), reflect.Uint32}, - } - - for _, e := range errTests { - val, err := reflects.ValueByKind(e.give, e.kind) - assert.Err(t, err, "give: %v, kind: %v", e.give, e.kind) - assert.False(t, val.IsValid()) - } - val, err := reflects.ValueByKind("abc", reflect.Int) assert.Err(t, err) assert.False(t, val.IsValid())