Documentation
¶
Overview ¶
Package coerce provides loose type coercion and assignment into native Go types.
Primitive Types ¶
This package considers the following types to be primitive types:
bool float32 float64 int8 int16 int32 int64 int uint8 uint16 uint32 uint64 uint string
Coercion Logic ¶
All coercion functions use the same basic logic to coerce an incoming value v:
for {
v is primitive
return coerced value or error
v's underlying kind is primitive
// convert v to underlying kind and try again
v = UnderlyingKind(v); continue
v is a pointer
// dereference v and try again
v = *v; continue
v is a slice
// try again with last slice element
v = v[len(v)-1]; continue
ErrUnsupported
}
If v is a primitive type it will be coerced via type coercion, one or more calls to the strconv package, or a small amount of custom logic.
If v's underlying kind is primitive it is converted to the underlying primitive and the loop restarts with a continue statement.
If v is a pointer or any pointer chain it is followed until the final value and the loop restarts with a continue statement. A nil pointer shortcuts and returns an appropriate zero value.
If v is a slice then v is reassigned to the last element and the loop starts again with a continue statement. An empty slice shortcuts and returns an appropriate zero value.
All other types for v (e.g. chan, map, func, etc) return a zero value and ErrUnsupported.
Overflow ¶
During numeric coercions this package checks incoming values against the minimum and maximum value for the target type. If the incoming value is out of range for the target type ErrOverflow is returned.
Otherwise the coercion is made with type conversion.
String Parsing ¶
Where necessary this package will call into strconv to parse values for bool, int, float, or uint.
If a call to strconv returns error this package will return the zero value for the target type and either ErrOverflow or ErrInvalid depending on the error received from strconv.
This package may make multiple calls to strconv to parse an incoming string. For example it may first try strconv.ParseInt, followed by strconv.ParseUint, and finally strconv.ParseFloat to parse a string. If any of the calls succeed then type coercion continues with the parsed value.
Index ¶
- Variables
- func Bool(v interface{}) (bool, error)
- func Float32(v interface{}) (float32, error)
- func Float64(v interface{}) (float64, error)
- func FloatOverflowsInt(f float64, bitsize int) bool
- func FloatOverflowsUint(f float64, bitsize int) bool
- func Int(v interface{}) (int, error)
- func Int8(v interface{}) (int8, error)
- func Int16(v interface{}) (int16, error)
- func Int32(v interface{}) (int32, error)
- func Int64(v interface{}) (int64, error)
- func IntOverflowsInt(i int64, bitsize int) bool
- func IntOverflowsUint(i int64, bitsize int) bool
- func KindFloat32(v interface{}) float32
- func KindFloat64(v interface{}) float64
- func String(v interface{}) (string, error)
- func Uint(v interface{}) (uint, error)
- func Uint8(v interface{}) (uint8, error)
- func Uint16(v interface{}) (uint16, error)
- func Uint32(v interface{}) (uint32, error)
- func Uint64(v interface{}) (uint64, error)
- func UintOverflowsInt(u uint64, bitsize int) bool
- func UintOverflowsUint(u uint64, bitsize int) bool
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalid occurs when an attempted coercion is invalid. ErrInvalid = fmt.Errorf("coerce: invalid") // ErrOverflow occurs when an attempted coercion overflows the destination. ErrOverflow = fmt.Errorf("coerce: overflow") // ErrUnsupported occurs when a source type for coercion is unsupported. ErrUnsupported = fmt.Errorf("coerce: unsupported") )
The following errors are returned by this package.
They are typically wrapped and can be checked with errors.Is.
var ( TypeBool = reflect.TypeOf(false) TypeFloat32 = reflect.TypeOf(float32(0)) TypeFloat64 = reflect.TypeOf(float64(0)) TypeInt64 = reflect.TypeOf(int64(0)) TypeUint64 = reflect.TypeOf(uint64(0)) TypeString = reflect.TypeOf("") )
Calculate and cache some common reflect.Type values needed during type coercion.
Functions ¶
func FloatOverflowsInt ¶
FloatOverflowsInt tests if float-overflows-int.
func FloatOverflowsUint ¶
FloatOverflowsUint tests if float-overflows-uint.
func IntOverflowsInt ¶
IntOverflowsInt tests if int-overflows-int.
func IntOverflowsUint ¶
IntOverflowsUint tests if int-overflows-uint.
func KindFloat32 ¶
func KindFloat32(v interface{}) float32
KindFloat32 unpacks v whose underlying kind must be float32.
func KindFloat64 ¶
func KindFloat64(v interface{}) float64
KindFloat64 unpacks v whose underlying kind must be float64.
func UintOverflowsInt ¶
UintOverflowsInt tests if uint-overflows-int.
func UintOverflowsUint ¶
UintOverflowsUint tests if uint-overflows-uint.
Types ¶
This section is empty.