Documentation ¶
Overview ¶
Package reflectutil provides reflection utilities.
Index ¶
- Constants
- func AllFieldNames(rt reflect.Type, exported bool, names map[string]reflect.StructField) map[string]reflect.StructField
- func ApproxDataSize(rv reflect.Value) (sum int)
- func Coerce(val interface{}, typ interface{}) (interface{}, error)
- func CoerceRV(rval reflect.Value, rtyp reflect.Type) (reflect.Value, error)
- func DeepEqual(v1, v2 interface{}, strict bool) (err error)
- func ExpandSliceValue(s reflect.Value, num int) reflect.Value
- func GrowCap(oldCap, unit, num uint) (newCap uint)
- func ImplementsInterface(typ, iTyp reflect.Type) (success bool, indir int8)
- func Indir(rv reflect.Value, finalTyp reflect.Type, maxDepth int) reflect.Value
- func IndirIntf(v interface{}, finalTyp reflect.Type, maxDepth int) interface{}
- func IsEmptyValue(v reflect.Value, deref, checkStruct bool) bool
- func ToGeneric(in interface{}) (out interface{})
Constants ¶
const MaxArrayLen = 1<<((32<<(^uint(0)>>63))-1) - 1
MaxArrayLen is the size of uint, which determines the maximum length of any array.
Variables ¶
This section is empty.
Functions ¶
func AllFieldNames ¶
func AllFieldNames(rt reflect.Type, exported bool, names map[string]reflect.StructField, ) map[string]reflect.StructField
Return all the field names, mapped to their struct fields. It does this recursively, getting for anonymous fields also.
func ApproxDataSize ¶
func Coerce ¶
func Coerce(val interface{}, typ interface{}) (interface{}, error)
Coerce a value into one of a different type. Returns the coerced value. Delegates to CoerceRV. (Wrapper of CoerceRV, without need to import reflect) Example:
Coerce(int(0), bool(false)) ==> returns a bool Coerce([]interface{}{"a", "b", "c"}, []string(nil)) ==> []string{"a", "b", "c"}
func CoerceRV ¶
Coerce a value into one of a different type. See table below for source and possible destinations.
SOURCE POSSIBLE DESTINATIONS =================================== - intXXX: floatXXX, intXXX, uintXXX - floatXXX: floatXXX, intXXX, uintXXX - uintXXX: floatXXX, intXXX, uintXXX - string: ANY (using fmt.Sprintf) - bool: ANY (using strconv.Atob) - slice: slice of any of above - map: mapping of any of above, or struct - struct: set fields based on field names of struct, or of passed map
func DeepEqual ¶
This checks 2 interfaces to see if they are the same. If strict=true, then checks that they are also same type (while walking the interfaces). It skips functions, channels, and non-exported fields in structs. It is better than the DeepEqual in reflect because it gives contextual information back in the error on what was wrong.
TODO: let error be a multi error of all the differences in the value (not just first one).
func GrowCap ¶
GrowCap will return a new capacity for a slice, given the following:
- oldCap: current capacity
- unit: in-memory size of an element
- num: number of elements to add
func ImplementsInterface ¶
func Indir ¶
IndirIntf will take an interface and loop indirections on it till you get the real value. It is more critical for nil interfaces, as we can easily get lost in the indirection hell.
Types ¶
This section is empty.