coerce

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2022 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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.

View Source
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 Bool

func Bool(v interface{}) (bool, error)

Bool coerces v to bool.

func Float32

func Float32(v interface{}) (float32, error)

Float32 coerces v to float32.

func Float64

func Float64(v interface{}) (float64, error)

Float64 coerces v to float64.

func FloatOverflowsInt

func FloatOverflowsInt(f float64, bitsize int) bool

FloatOverflowsInt tests if float-overflows-int.

func FloatOverflowsUint

func FloatOverflowsUint(f float64, bitsize int) bool

FloatOverflowsUint tests if float-overflows-uint.

func Int

func Int(v interface{}) (int, error)

Int coerces v to int.

func Int8

func Int8(v interface{}) (int8, error)

Int8 coerces v to int8.

func Int16

func Int16(v interface{}) (int16, error)

Int16 coerces v to int16.

func Int32

func Int32(v interface{}) (int32, error)

Int32 coerces v to int32.

func Int64

func Int64(v interface{}) (int64, error)

Int64 coerces v to int64.

func IntOverflowsInt

func IntOverflowsInt(i int64, bitsize int) bool

IntOverflowsInt tests if int-overflows-int.

func IntOverflowsUint

func IntOverflowsUint(i int64, bitsize int) bool

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 String

func String(v interface{}) (string, error)

String coerces v to string.

func Uint

func Uint(v interface{}) (uint, error)

Uint coerces v to uint.

func Uint8

func Uint8(v interface{}) (uint8, error)

Uint8 coerces v to uint8.

func Uint16

func Uint16(v interface{}) (uint16, error)

Uint16 coerces v to uint16.

func Uint32

func Uint32(v interface{}) (uint32, error)

Uint32 coerces v to uint32.

func Uint64

func Uint64(v interface{}) (uint64, error)

Uint64 coerces v to uint64.

func UintOverflowsInt

func UintOverflowsInt(u uint64, bitsize int) bool

UintOverflowsInt tests if uint-overflows-int.

func UintOverflowsUint

func UintOverflowsUint(u uint64, bitsize int) bool

UintOverflowsUint tests if uint-overflows-uint.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL