Documentation ¶
Overview ¶
Package reflectx contains a set of reflection utilities and well-known types.
Index ¶
- Variables
- func CallNoPanic(fn Func, args []any) (ret []any, err error)
- func FunctionName(fn any) string
- func Interface(list []reflect.Value) []any
- func IsComplex(t reflect.Type) bool
- func IsFloat(t reflect.Type) bool
- func IsInteger(t reflect.Type) bool
- func IsNumber(t reflect.Type) bool
- func LoadFunction(ptr uintptr, t reflect.Type) any
- func MakeSlice(t reflect.Type, values ...reflect.Value) reflect.Value
- func RegisterFunc(t reflect.Type, maker func(any) Func)
- func RegisterStructWrapper(t reflect.Type, wrapper func(any) map[string]Func)
- func ShallowClone(v any) any
- func SkipPtr(t reflect.Type) reflect.Type
- func UnderlyingType(value reflect.Value) reflect.Value
- func UpdateMap(base, updates any)
- func ValueOf(list []any) []reflect.Value
- func WrapMethods(fn any) (map[string]Func, bool)
- type Func
- type Func0x0
- type Func0x1
- type Func0x2
- type Func0x3
- type Func0x4
- type Func1x0
- type Func1x1
- type Func1x2
- type Func1x3
- type Func1x4
- type Func2x0
- type Func2x1
- type Func2x2
- type Func2x3
- type Func2x4
- type Func3x0
- type Func3x1
- type Func3x2
- type Func3x3
- type Func3x4
- type Func4x0
- type Func4x1
- type Func4x2
- type Func4x3
- type Func4x4
- type Func5x0
- type Func5x1
- type Func5x2
- type Func5x3
- type Func5x4
- type Func6x0
- type Func6x1
- type Func6x2
- type Func6x3
- type Func6x4
- type Func7x0
- type Func7x1
- type Func7x2
- type Func7x3
- type Func7x4
- type Func8x0
- type Func8x1
- type Func8x2
- type Func8x3
- type Func8x4
Constants ¶
This section is empty.
Variables ¶
var ( Bool = reflect.TypeOf((*bool)(nil)).Elem() Int = reflect.TypeOf((*int)(nil)).Elem() Int8 = reflect.TypeOf((*int8)(nil)).Elem() Int16 = reflect.TypeOf((*int16)(nil)).Elem() Int32 = reflect.TypeOf((*int32)(nil)).Elem() Int64 = reflect.TypeOf((*int64)(nil)).Elem() Uint = reflect.TypeOf((*uint)(nil)).Elem() Uint8 = reflect.TypeOf((*uint8)(nil)).Elem() Uint16 = reflect.TypeOf((*uint16)(nil)).Elem() Uint32 = reflect.TypeOf((*uint32)(nil)).Elem() Uint64 = reflect.TypeOf((*uint64)(nil)).Elem() Float32 = reflect.TypeOf((*float32)(nil)).Elem() Float64 = reflect.TypeOf((*float64)(nil)).Elem() String = reflect.TypeOf((*string)(nil)).Elem() Error = reflect.TypeOf((*error)(nil)).Elem() Context = reflect.TypeOf((*context.Context)(nil)).Elem() Type = reflect.TypeOf((*reflect.Type)(nil)).Elem() ByteSlice = reflect.TypeOf((*[]byte)(nil)).Elem() )
Well-known reflected types. Convenience definitions.
Functions ¶
func CallNoPanic ¶
CallNoPanic calls the given Func and catches any panic.
func FunctionName ¶
FunctionName returns the symbol name of a function. It panics if the given value is not a function.
func IsInteger ¶
IsInteger returns true iff the given type is an integer, such as uint16, int, or int64.
func LoadFunction ¶
LoadFunction loads a function from a pointer and type. Assumes the pointer points to a valid function implementation.
func RegisterFunc ¶
RegisterFunc registers an custom Func factory for the given type, such as "func(int)bool". If multiple Func factories are registered for the same type, the last registration wins.
func RegisterStructWrapper ¶
RegisterStructWrapper takes in the reflect.Type of a structural DoFn, and a wrapping function that will take an instance of that struct type and produce a map of method names to of closured Funcs that call the method on the instance of the struct.
The goal is to avoid the implicit reflective method invocation penalty that occurs when passing a method through the reflect package.
func ShallowClone ¶
ShallowClone creates a shallow copy of the given value. Most useful for slices and maps.
func UnderlyingType ¶
UnderlyingType drops value's type by converting it to an interface and then returning ValueOf() the untyped value.
func UpdateMap ¶
func UpdateMap(base, updates any)
UpdateMap merges two maps of type map[K]*V, with the second overwriting values into the first (and mutating it). If the overwriting value is nil, the key is deleted.
Types ¶
type Func ¶
type Func interface { // Name returns the name of the function. Name() string // Type returns the type. Type() reflect.Type // Call invokes the implicit fn with arguments. Call(args []any) []any }
Func represents a callable untyped function. This indirection allows us to avoid reflection call overhead for certain types as well as faster dynamic function implementations.