reflectx

package
v2.61.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 9 Imported by: 62

Documentation

Overview

Package reflectx contains a set of reflection utilities and well-known types.

Index

Constants

This section is empty.

Variables

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

func CallNoPanic(fn Func, args []any) (ret []any, err error)

CallNoPanic calls the given Func and catches any panic.

func FunctionName

func FunctionName(fn any) string

FunctionName returns the symbol name of a function. It panics if the given value is not a function.

func Interface

func Interface(list []reflect.Value) []any

Interface performs a per-element Interface call.

func IsComplex

func IsComplex(t reflect.Type) bool

IsComplex returns true iff the given type is complex64 or complex128.

func IsFloat

func IsFloat(t reflect.Type) bool

IsFloat returns true iff the given type is float32 or float64.

func IsInteger

func IsInteger(t reflect.Type) bool

IsInteger returns true iff the given type is an integer, such as uint16, int, or int64.

func IsNumber

func IsNumber(t reflect.Type) bool

IsNumber returns true iff the given type is an integer, float, or complex.

func LoadFunction

func LoadFunction(ptr uintptr, t reflect.Type) any

LoadFunction loads a function from a pointer and type. Assumes the pointer points to a valid function implementation.

func MakeSlice

func MakeSlice(t reflect.Type, values ...reflect.Value) reflect.Value

MakeSlice creates a slice of type []T with the given elements.

func RegisterFunc

func RegisterFunc(t reflect.Type, maker func(any) Func)

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

func RegisterStructWrapper(t reflect.Type, wrapper func(any) map[string]Func)

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

func ShallowClone(v any) any

ShallowClone creates a shallow copy of the given value. Most useful for slices and maps.

func SkipPtr

func SkipPtr(t reflect.Type) reflect.Type

SkipPtr returns the target of a Ptr type, if a Ptr. Otherwise itself.

func UnderlyingType

func UnderlyingType(value reflect.Value) reflect.Value

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.

func ValueOf

func ValueOf(list []any) []reflect.Value

ValueOf performs a per-element reflect.ValueOf.

func WrapMethods

func WrapMethods(fn any) (map[string]Func, bool)

WrapMethods takes in a struct value as an interface, and returns a map of method names to Funcs of those methods wrapped in a closure for the struct instance.

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.

func MakeFunc

func MakeFunc(fn any) Func

MakeFunc returns a Func for given function.

type Func0x0

type Func0x0 interface {
	Func
	Call0x0()
}

func MakeFunc0x0

func MakeFunc0x0(fn any) Func0x0

func ToFunc0x0

func ToFunc0x0(c Func) Func0x0

type Func0x1

type Func0x1 interface {
	Func
	Call0x1() any
}

func MakeFunc0x1

func MakeFunc0x1(fn any) Func0x1

func ToFunc0x1

func ToFunc0x1(c Func) Func0x1

type Func0x2

type Func0x2 interface {
	Func
	Call0x2() (any, any)
}

func MakeFunc0x2

func MakeFunc0x2(fn any) Func0x2

func ToFunc0x2

func ToFunc0x2(c Func) Func0x2

type Func0x3

type Func0x3 interface {
	Func
	Call0x3() (any, any, any)
}

func MakeFunc0x3

func MakeFunc0x3(fn any) Func0x3

func ToFunc0x3

func ToFunc0x3(c Func) Func0x3

type Func0x4 added in v2.39.0

type Func0x4 interface {
	Func
	Call0x4() (any, any, any, any)
}

func MakeFunc0x4 added in v2.39.0

func MakeFunc0x4(fn any) Func0x4

func ToFunc0x4 added in v2.39.0

func ToFunc0x4(c Func) Func0x4

type Func1x0

type Func1x0 interface {
	Func
	Call1x0(any)
}

func MakeFunc1x0

func MakeFunc1x0(fn any) Func1x0

func ToFunc1x0

func ToFunc1x0(c Func) Func1x0

type Func1x1

type Func1x1 interface {
	Func
	Call1x1(any) any
}

func MakeFunc1x1

func MakeFunc1x1(fn any) Func1x1

func ToFunc1x1

func ToFunc1x1(c Func) Func1x1

type Func1x2

type Func1x2 interface {
	Func
	Call1x2(any) (any, any)
}

func MakeFunc1x2

func MakeFunc1x2(fn any) Func1x2

func ToFunc1x2

func ToFunc1x2(c Func) Func1x2

type Func1x3

type Func1x3 interface {
	Func
	Call1x3(any) (any, any, any)
}

func MakeFunc1x3

func MakeFunc1x3(fn any) Func1x3

func ToFunc1x3

func ToFunc1x3(c Func) Func1x3

type Func1x4 added in v2.39.0

type Func1x4 interface {
	Func
	Call1x4(any) (any, any, any, any)
}

func MakeFunc1x4 added in v2.39.0

func MakeFunc1x4(fn any) Func1x4

func ToFunc1x4 added in v2.39.0

func ToFunc1x4(c Func) Func1x4

type Func2x0

type Func2x0 interface {
	Func
	Call2x0(any, any)
}

func MakeFunc2x0

func MakeFunc2x0(fn any) Func2x0

func ToFunc2x0

func ToFunc2x0(c Func) Func2x0

type Func2x1

type Func2x1 interface {
	Func
	Call2x1(any, any) any
}

func MakeFunc2x1

func MakeFunc2x1(fn any) Func2x1

func ToFunc2x1

func ToFunc2x1(c Func) Func2x1

type Func2x2

type Func2x2 interface {
	Func
	Call2x2(any, any) (any, any)
}

func MakeFunc2x2

func MakeFunc2x2(fn any) Func2x2

func ToFunc2x2

func ToFunc2x2(c Func) Func2x2

type Func2x3

type Func2x3 interface {
	Func
	Call2x3(any, any) (any, any, any)
}

func MakeFunc2x3

func MakeFunc2x3(fn any) Func2x3

func ToFunc2x3

func ToFunc2x3(c Func) Func2x3

type Func2x4 added in v2.39.0

type Func2x4 interface {
	Func
	Call2x4(any, any) (any, any, any, any)
}

func MakeFunc2x4 added in v2.39.0

func MakeFunc2x4(fn any) Func2x4

func ToFunc2x4 added in v2.39.0

func ToFunc2x4(c Func) Func2x4

type Func3x0

type Func3x0 interface {
	Func
	Call3x0(any, any, any)
}

func MakeFunc3x0

func MakeFunc3x0(fn any) Func3x0

func ToFunc3x0

func ToFunc3x0(c Func) Func3x0

type Func3x1

type Func3x1 interface {
	Func
	Call3x1(any, any, any) any
}

func MakeFunc3x1

func MakeFunc3x1(fn any) Func3x1

func ToFunc3x1

func ToFunc3x1(c Func) Func3x1

type Func3x2

type Func3x2 interface {
	Func
	Call3x2(any, any, any) (any, any)
}

func MakeFunc3x2

func MakeFunc3x2(fn any) Func3x2

func ToFunc3x2

func ToFunc3x2(c Func) Func3x2

type Func3x3

type Func3x3 interface {
	Func
	Call3x3(any, any, any) (any, any, any)
}

func MakeFunc3x3

func MakeFunc3x3(fn any) Func3x3

func ToFunc3x3

func ToFunc3x3(c Func) Func3x3

type Func3x4 added in v2.39.0

type Func3x4 interface {
	Func
	Call3x4(any, any, any) (any, any, any, any)
}

func MakeFunc3x4 added in v2.39.0

func MakeFunc3x4(fn any) Func3x4

func ToFunc3x4 added in v2.39.0

func ToFunc3x4(c Func) Func3x4

type Func4x0

type Func4x0 interface {
	Func
	Call4x0(any, any, any, any)
}

func MakeFunc4x0

func MakeFunc4x0(fn any) Func4x0

func ToFunc4x0

func ToFunc4x0(c Func) Func4x0

type Func4x1

type Func4x1 interface {
	Func
	Call4x1(any, any, any, any) any
}

func MakeFunc4x1

func MakeFunc4x1(fn any) Func4x1

func ToFunc4x1

func ToFunc4x1(c Func) Func4x1

type Func4x2

type Func4x2 interface {
	Func
	Call4x2(any, any, any, any) (any, any)
}

func MakeFunc4x2

func MakeFunc4x2(fn any) Func4x2

func ToFunc4x2

func ToFunc4x2(c Func) Func4x2

type Func4x3

type Func4x3 interface {
	Func
	Call4x3(any, any, any, any) (any, any, any)
}

func MakeFunc4x3

func MakeFunc4x3(fn any) Func4x3

func ToFunc4x3

func ToFunc4x3(c Func) Func4x3

type Func4x4 added in v2.39.0

type Func4x4 interface {
	Func
	Call4x4(any, any, any, any) (any, any, any, any)
}

func MakeFunc4x4 added in v2.39.0

func MakeFunc4x4(fn any) Func4x4

func ToFunc4x4 added in v2.39.0

func ToFunc4x4(c Func) Func4x4

type Func5x0

type Func5x0 interface {
	Func
	Call5x0(any, any, any, any, any)
}

func MakeFunc5x0

func MakeFunc5x0(fn any) Func5x0

func ToFunc5x0

func ToFunc5x0(c Func) Func5x0

type Func5x1

type Func5x1 interface {
	Func
	Call5x1(any, any, any, any, any) any
}

func MakeFunc5x1

func MakeFunc5x1(fn any) Func5x1

func ToFunc5x1

func ToFunc5x1(c Func) Func5x1

type Func5x2

type Func5x2 interface {
	Func
	Call5x2(any, any, any, any, any) (any, any)
}

func MakeFunc5x2

func MakeFunc5x2(fn any) Func5x2

func ToFunc5x2

func ToFunc5x2(c Func) Func5x2

type Func5x3

type Func5x3 interface {
	Func
	Call5x3(any, any, any, any, any) (any, any, any)
}

func MakeFunc5x3

func MakeFunc5x3(fn any) Func5x3

func ToFunc5x3

func ToFunc5x3(c Func) Func5x3

type Func5x4 added in v2.39.0

type Func5x4 interface {
	Func
	Call5x4(any, any, any, any, any) (any, any, any, any)
}

func MakeFunc5x4 added in v2.39.0

func MakeFunc5x4(fn any) Func5x4

func ToFunc5x4 added in v2.39.0

func ToFunc5x4(c Func) Func5x4

type Func6x0

type Func6x0 interface {
	Func
	Call6x0(any, any, any, any, any, any)
}

func MakeFunc6x0

func MakeFunc6x0(fn any) Func6x0

func ToFunc6x0

func ToFunc6x0(c Func) Func6x0

type Func6x1

type Func6x1 interface {
	Func
	Call6x1(any, any, any, any, any, any) any
}

func MakeFunc6x1

func MakeFunc6x1(fn any) Func6x1

func ToFunc6x1

func ToFunc6x1(c Func) Func6x1

type Func6x2

type Func6x2 interface {
	Func
	Call6x2(any, any, any, any, any, any) (any, any)
}

func MakeFunc6x2

func MakeFunc6x2(fn any) Func6x2

func ToFunc6x2

func ToFunc6x2(c Func) Func6x2

type Func6x3

type Func6x3 interface {
	Func
	Call6x3(any, any, any, any, any, any) (any, any, any)
}

func MakeFunc6x3

func MakeFunc6x3(fn any) Func6x3

func ToFunc6x3

func ToFunc6x3(c Func) Func6x3

type Func6x4 added in v2.39.0

type Func6x4 interface {
	Func
	Call6x4(any, any, any, any, any, any) (any, any, any, any)
}

func MakeFunc6x4 added in v2.39.0

func MakeFunc6x4(fn any) Func6x4

func ToFunc6x4 added in v2.39.0

func ToFunc6x4(c Func) Func6x4

type Func7x0

type Func7x0 interface {
	Func
	Call7x0(any, any, any, any, any, any, any)
}

func MakeFunc7x0

func MakeFunc7x0(fn any) Func7x0

func ToFunc7x0

func ToFunc7x0(c Func) Func7x0

type Func7x1

type Func7x1 interface {
	Func
	Call7x1(any, any, any, any, any, any, any) any
}

func MakeFunc7x1

func MakeFunc7x1(fn any) Func7x1

func ToFunc7x1

func ToFunc7x1(c Func) Func7x1

type Func7x2

type Func7x2 interface {
	Func
	Call7x2(any, any, any, any, any, any, any) (any, any)
}

func MakeFunc7x2

func MakeFunc7x2(fn any) Func7x2

func ToFunc7x2

func ToFunc7x2(c Func) Func7x2

type Func7x3

type Func7x3 interface {
	Func
	Call7x3(any, any, any, any, any, any, any) (any, any, any)
}

func MakeFunc7x3

func MakeFunc7x3(fn any) Func7x3

func ToFunc7x3

func ToFunc7x3(c Func) Func7x3

type Func7x4 added in v2.39.0

type Func7x4 interface {
	Func
	Call7x4(any, any, any, any, any, any, any) (any, any, any, any)
}

func MakeFunc7x4 added in v2.39.0

func MakeFunc7x4(fn any) Func7x4

func ToFunc7x4 added in v2.39.0

func ToFunc7x4(c Func) Func7x4

type Func8x0 added in v2.39.0

type Func8x0 interface {
	Func
	Call8x0(any, any, any, any, any, any, any, any)
}

func MakeFunc8x0 added in v2.39.0

func MakeFunc8x0(fn any) Func8x0

func ToFunc8x0 added in v2.39.0

func ToFunc8x0(c Func) Func8x0

type Func8x1 added in v2.39.0

type Func8x1 interface {
	Func
	Call8x1(any, any, any, any, any, any, any, any) any
}

func MakeFunc8x1 added in v2.39.0

func MakeFunc8x1(fn any) Func8x1

func ToFunc8x1 added in v2.39.0

func ToFunc8x1(c Func) Func8x1

type Func8x2 added in v2.39.0

type Func8x2 interface {
	Func
	Call8x2(any, any, any, any, any, any, any, any) (any, any)
}

func MakeFunc8x2 added in v2.39.0

func MakeFunc8x2(fn any) Func8x2

func ToFunc8x2 added in v2.39.0

func ToFunc8x2(c Func) Func8x2

type Func8x3 added in v2.39.0

type Func8x3 interface {
	Func
	Call8x3(any, any, any, any, any, any, any, any) (any, any, any)
}

func MakeFunc8x3 added in v2.39.0

func MakeFunc8x3(fn any) Func8x3

func ToFunc8x3 added in v2.39.0

func ToFunc8x3(c Func) Func8x3

type Func8x4 added in v2.39.0

type Func8x4 interface {
	Func
	Call8x4(any, any, any, any, any, any, any, any) (any, any, any, any)
}

func MakeFunc8x4 added in v2.39.0

func MakeFunc8x4(fn any) Func8x4

func ToFunc8x4 added in v2.39.0

func ToFunc8x4(c Func) Func8x4

Jump to

Keyboard shortcuts

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