mirror of https://github.com/spf13/cast.git
Error on unsigned conversions of negative values
This commit is contained in:
parent
d37435d953
commit
758cd85768
40
cast_test.go
40
cast_test.go
|
@ -37,6 +37,14 @@ func TestToUintE(t *testing.T) {
|
|||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{int(-8), 0, true},
|
||||
{int8(-8), 0, true},
|
||||
{int16(-8), 0, true},
|
||||
{int32(-8), 0, true},
|
||||
{int64(-8), 0, true},
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
@ -82,6 +90,14 @@ func TestToUint64E(t *testing.T) {
|
|||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{int(-8), 0, true},
|
||||
{int8(-8), 0, true},
|
||||
{int16(-8), 0, true},
|
||||
{int32(-8), 0, true},
|
||||
{int64(-8), 0, true},
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
@ -126,6 +142,14 @@ func TestToUint32E(t *testing.T) {
|
|||
{false, 0, false},
|
||||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
{int(-8), 0, true},
|
||||
{int8(-8), 0, true},
|
||||
{int16(-8), 0, true},
|
||||
{int32(-8), 0, true},
|
||||
{int64(-8), 0, true},
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
// errors
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
|
@ -172,6 +196,14 @@ func TestToUint16E(t *testing.T) {
|
|||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{int(-8), 0, true},
|
||||
{int8(-8), 0, true},
|
||||
{int16(-8), 0, true},
|
||||
{int32(-8), 0, true},
|
||||
{int64(-8), 0, true},
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
@ -217,6 +249,14 @@ func TestToUint8E(t *testing.T) {
|
|||
{"8", 8, false},
|
||||
{nil, 0, false},
|
||||
// errors
|
||||
{int(-8), 0, true},
|
||||
{int8(-8), 0, true},
|
||||
{int16(-8), 0, true},
|
||||
{int32(-8), 0, true},
|
||||
{int64(-8), 0, true},
|
||||
{float32(-8.31), 0, true},
|
||||
{float64(-8.31), 0, true},
|
||||
{"-8", 0, true},
|
||||
{"test", 0, true},
|
||||
{testing.T{}, 0, true},
|
||||
}
|
||||
|
|
105
caste.go
105
caste.go
|
@ -424,14 +424,29 @@ func ToUintE(i interface{}) (uint, error) {
|
|||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v to uint: %s", i, err)
|
||||
case int:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
||||
}
|
||||
return uint(s), nil
|
||||
case int64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
||||
}
|
||||
return uint(s), nil
|
||||
case int32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
||||
}
|
||||
return uint(s), nil
|
||||
case int16:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
||||
}
|
||||
return uint(s), nil
|
||||
case int8:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
||||
}
|
||||
return uint(s), nil
|
||||
case uint:
|
||||
return s, nil
|
||||
|
@ -444,8 +459,14 @@ func ToUintE(i interface{}) (uint, error) {
|
|||
case uint8:
|
||||
return uint(s), nil
|
||||
case float64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
||||
}
|
||||
return uint(s), nil
|
||||
case float32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
||||
}
|
||||
return uint(s), nil
|
||||
case bool:
|
||||
if s {
|
||||
|
@ -471,14 +492,29 @@ func ToUint64E(i interface{}) (uint64, error) {
|
|||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v to uint64: %s", i, err)
|
||||
case int:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
||||
}
|
||||
return uint64(s), nil
|
||||
case int64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
||||
}
|
||||
return uint64(s), nil
|
||||
case int32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
||||
}
|
||||
return uint64(s), nil
|
||||
case int16:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
||||
}
|
||||
return uint64(s), nil
|
||||
case int8:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
||||
}
|
||||
return uint64(s), nil
|
||||
case uint:
|
||||
return uint64(s), nil
|
||||
|
@ -491,8 +527,14 @@ func ToUint64E(i interface{}) (uint64, error) {
|
|||
case uint8:
|
||||
return uint64(s), nil
|
||||
case float32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
||||
}
|
||||
return uint64(s), nil
|
||||
case float64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
||||
}
|
||||
return uint64(s), nil
|
||||
case bool:
|
||||
if s {
|
||||
|
@ -518,14 +560,29 @@ func ToUint32E(i interface{}) (uint32, error) {
|
|||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v to uint32: %s", i, err)
|
||||
case int:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
||||
}
|
||||
return uint32(s), nil
|
||||
case int64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
||||
}
|
||||
return uint32(s), nil
|
||||
case int32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
||||
}
|
||||
return uint32(s), nil
|
||||
case int16:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
||||
}
|
||||
return uint32(s), nil
|
||||
case int8:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
||||
}
|
||||
return uint32(s), nil
|
||||
case uint:
|
||||
return uint32(s), nil
|
||||
|
@ -538,8 +595,14 @@ func ToUint32E(i interface{}) (uint32, error) {
|
|||
case uint8:
|
||||
return uint32(s), nil
|
||||
case float64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
||||
}
|
||||
return uint32(s), nil
|
||||
case float32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
||||
}
|
||||
return uint32(s), nil
|
||||
case bool:
|
||||
if s {
|
||||
|
@ -565,14 +628,29 @@ func ToUint16E(i interface{}) (uint16, error) {
|
|||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v to uint16: %s", i, err)
|
||||
case int:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
||||
}
|
||||
return uint16(s), nil
|
||||
case int64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
||||
}
|
||||
return uint16(s), nil
|
||||
case int32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
||||
}
|
||||
return uint16(s), nil
|
||||
case int16:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
||||
}
|
||||
return uint16(s), nil
|
||||
case int8:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
||||
}
|
||||
return uint16(s), nil
|
||||
case uint:
|
||||
return uint16(s), nil
|
||||
|
@ -585,8 +663,14 @@ func ToUint16E(i interface{}) (uint16, error) {
|
|||
case uint8:
|
||||
return uint16(s), nil
|
||||
case float64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
||||
}
|
||||
return uint16(s), nil
|
||||
case float32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
||||
}
|
||||
return uint16(s), nil
|
||||
case bool:
|
||||
if s {
|
||||
|
@ -612,14 +696,29 @@ func ToUint8E(i interface{}) (uint8, error) {
|
|||
}
|
||||
return 0, fmt.Errorf("unable to cast %#v to uint8: %s", i, err)
|
||||
case int:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
||||
}
|
||||
return uint8(s), nil
|
||||
case int64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
||||
}
|
||||
return uint8(s), nil
|
||||
case int32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
||||
}
|
||||
return uint8(s), nil
|
||||
case int16:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
||||
}
|
||||
return uint8(s), nil
|
||||
case int8:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
||||
}
|
||||
return uint8(s), nil
|
||||
case uint:
|
||||
return uint8(s), nil
|
||||
|
@ -632,8 +731,14 @@ func ToUint8E(i interface{}) (uint8, error) {
|
|||
case uint8:
|
||||
return s, nil
|
||||
case float64:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
||||
}
|
||||
return uint8(s), nil
|
||||
case float32:
|
||||
if s < 0 {
|
||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
||||
}
|
||||
return uint8(s), nil
|
||||
case bool:
|
||||
if s {
|
||||
|
|
Loading…
Reference in New Issue