mirror of https://github.com/spf13/cast.git
Use single defined error for negatives
This commit is contained in:
parent
758cd85768
commit
1ad7d3c5ed
73
caste.go
73
caste.go
|
@ -6,6 +6,7 @@
|
||||||
package cast
|
package cast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -14,6 +15,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errNegativeNotAllowed = errors.New("unable to cast negative value")
|
||||||
|
|
||||||
// ToTimeE casts an interface to a time.Time type.
|
// ToTimeE casts an interface to a time.Time type.
|
||||||
func ToTimeE(i interface{}) (tim time.Time, err error) {
|
func ToTimeE(i interface{}) (tim time.Time, err error) {
|
||||||
i = indirect(i)
|
i = indirect(i)
|
||||||
|
@ -425,27 +428,27 @@ func ToUintE(i interface{}) (uint, error) {
|
||||||
return 0, fmt.Errorf("unable to cast %#v to uint: %s", i, err)
|
return 0, fmt.Errorf("unable to cast %#v to uint: %s", i, err)
|
||||||
case int:
|
case int:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case int64:
|
case int64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case int32:
|
case int32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case int16:
|
case int16:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case int8:
|
case int8:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case uint:
|
case uint:
|
||||||
|
@ -460,12 +463,12 @@ func ToUintE(i interface{}) (uint, error) {
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case float64:
|
case float64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case float32:
|
case float32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint(s), nil
|
return uint(s), nil
|
||||||
case bool:
|
case bool:
|
||||||
|
@ -493,27 +496,27 @@ func ToUint64E(i interface{}) (uint64, error) {
|
||||||
return 0, fmt.Errorf("unable to cast %#v to uint64: %s", i, err)
|
return 0, fmt.Errorf("unable to cast %#v to uint64: %s", i, err)
|
||||||
case int:
|
case int:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case int64:
|
case int64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case int32:
|
case int32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case int16:
|
case int16:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case int8:
|
case int8:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case uint:
|
case uint:
|
||||||
|
@ -528,12 +531,12 @@ func ToUint64E(i interface{}) (uint64, error) {
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case float32:
|
case float32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case float64:
|
case float64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint64")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint64(s), nil
|
return uint64(s), nil
|
||||||
case bool:
|
case bool:
|
||||||
|
@ -561,27 +564,27 @@ func ToUint32E(i interface{}) (uint32, error) {
|
||||||
return 0, fmt.Errorf("unable to cast %#v to uint32: %s", i, err)
|
return 0, fmt.Errorf("unable to cast %#v to uint32: %s", i, err)
|
||||||
case int:
|
case int:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case int64:
|
case int64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case int32:
|
case int32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case int16:
|
case int16:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case int8:
|
case int8:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case uint:
|
case uint:
|
||||||
|
@ -596,12 +599,12 @@ func ToUint32E(i interface{}) (uint32, error) {
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case float64:
|
case float64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case float32:
|
case float32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint32")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint32(s), nil
|
return uint32(s), nil
|
||||||
case bool:
|
case bool:
|
||||||
|
@ -629,27 +632,27 @@ func ToUint16E(i interface{}) (uint16, error) {
|
||||||
return 0, fmt.Errorf("unable to cast %#v to uint16: %s", i, err)
|
return 0, fmt.Errorf("unable to cast %#v to uint16: %s", i, err)
|
||||||
case int:
|
case int:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case int64:
|
case int64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case int32:
|
case int32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case int16:
|
case int16:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case int8:
|
case int8:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case uint:
|
case uint:
|
||||||
|
@ -664,12 +667,12 @@ func ToUint16E(i interface{}) (uint16, error) {
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case float64:
|
case float64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case float32:
|
case float32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint16")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint16(s), nil
|
return uint16(s), nil
|
||||||
case bool:
|
case bool:
|
||||||
|
@ -697,27 +700,27 @@ func ToUint8E(i interface{}) (uint8, error) {
|
||||||
return 0, fmt.Errorf("unable to cast %#v to uint8: %s", i, err)
|
return 0, fmt.Errorf("unable to cast %#v to uint8: %s", i, err)
|
||||||
case int:
|
case int:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint8(s), nil
|
return uint8(s), nil
|
||||||
case int64:
|
case int64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint8(s), nil
|
return uint8(s), nil
|
||||||
case int32:
|
case int32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint8(s), nil
|
return uint8(s), nil
|
||||||
case int16:
|
case int16:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint8(s), nil
|
return uint8(s), nil
|
||||||
case int8:
|
case int8:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint8(s), nil
|
return uint8(s), nil
|
||||||
case uint:
|
case uint:
|
||||||
|
@ -732,12 +735,12 @@ func ToUint8E(i interface{}) (uint8, error) {
|
||||||
return s, nil
|
return s, nil
|
||||||
case float64:
|
case float64:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint8(s), nil
|
return uint8(s), nil
|
||||||
case float32:
|
case float32:
|
||||||
if s < 0 {
|
if s < 0 {
|
||||||
return 0, fmt.Errorf("unable to cast negative value to uint8")
|
return 0, errNegativeNotAllowed
|
||||||
}
|
}
|
||||||
return uint8(s), nil
|
return uint8(s), nil
|
||||||
case bool:
|
case bool:
|
||||||
|
|
Loading…
Reference in New Issue