reflectx

package
v2.24.0-RC3+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

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 []interface{}) (ret []interface{}, err error)

CallNoPanic calls the given Func and catches any panic.

func FindTaggedField

func FindTaggedField(t reflect.Type, values ...string) (reflect.StructField, bool)

FindTaggedField returns the field tagged with any of the given tag values, if any. The tags are all under the "beam" StructTag key.

func FunctionName

func FunctionName(fn interface{}) string

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

func HasTag

func HasTag(f reflect.StructField, values ...string) bool

HasTag returns true iff the given field contains one of the given tags under the "beam" key.

func HasTaggedField

func HasTaggedField(t reflect.Type, values ...string) bool

HasTaggedField returns true iff the given struct has a field with any of the given tag values.

func Interface

func Interface(list []reflect.Value) []interface{}

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) interface{}

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(interface{}) 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(interface{}) 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 SetFieldValue

func SetFieldValue(s reflect.Value, f reflect.StructField, value reflect.Value)

SetFieldValue sets s.f = value. Panics if not valid.

func SetTaggedFieldValue

func SetTaggedFieldValue(v reflect.Value, tag string, value reflect.Value)

SetTaggedFieldValue sets s.f = value, where f has the tag "beam:tag". Panics if not valid.

func ShallowClone

func ShallowClone(v interface{}) interface{}

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 UnmarshalJSON

func UnmarshalJSON(t reflect.Type, str string) (interface{}, error)

UnmarshalJSON decodes a json string as then given type. It is suitable for when the type is not statically known.

func UpdateMap

func UpdateMap(base, updates interface{})

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 []interface{}) []reflect.Value

ValueOf performs a per-element reflect.ValueOf.

func WrapMethods

func WrapMethods(fn interface{}) (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 []interface{}) []interface{}
}

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 interface{}) Func

MakeFunc returns a Func for given function.

type Func0x0

type Func0x0 interface {
	Func
	Call0x0()
}

func MakeFunc0x0

func MakeFunc0x0(fn interface{}) Func0x0

func ToFunc0x0

func ToFunc0x0(c Func) Func0x0

type Func0x1

type Func0x1 interface {
	Func
	Call0x1() interface{}
}

func MakeFunc0x1

func MakeFunc0x1(fn interface{}) Func0x1

func ToFunc0x1

func ToFunc0x1(c Func) Func0x1

type Func0x2

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

func MakeFunc0x2

func MakeFunc0x2(fn interface{}) Func0x2

func ToFunc0x2

func ToFunc0x2(c Func) Func0x2

type Func0x3

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

func MakeFunc0x3

func MakeFunc0x3(fn interface{}) Func0x3

func ToFunc0x3

func ToFunc0x3(c Func) Func0x3

type Func1x0

type Func1x0 interface {
	Func
	Call1x0(interface{})
}

func MakeFunc1x0

func MakeFunc1x0(fn interface{}) Func1x0

func ToFunc1x0

func ToFunc1x0(c Func) Func1x0

type Func1x1

type Func1x1 interface {
	Func
	Call1x1(interface{}) interface{}
}

func MakeFunc1x1

func MakeFunc1x1(fn interface{}) Func1x1

func ToFunc1x1

func ToFunc1x1(c Func) Func1x1

type Func1x2

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

func MakeFunc1x2

func MakeFunc1x2(fn interface{}) Func1x2

func ToFunc1x2

func ToFunc1x2(c Func) Func1x2

type Func1x3

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

func MakeFunc1x3

func MakeFunc1x3(fn interface{}) Func1x3

func ToFunc1x3

func ToFunc1x3(c Func) Func1x3

type Func2x0

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

func MakeFunc2x0

func MakeFunc2x0(fn interface{}) Func2x0

func ToFunc2x0

func ToFunc2x0(c Func) Func2x0

type Func2x1

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

func MakeFunc2x1

func MakeFunc2x1(fn interface{}) Func2x1

func ToFunc2x1

func ToFunc2x1(c Func) Func2x1

type Func2x2

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

func MakeFunc2x2

func MakeFunc2x2(fn interface{}) Func2x2

func ToFunc2x2

func ToFunc2x2(c Func) Func2x2

type Func2x3

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

func MakeFunc2x3

func MakeFunc2x3(fn interface{}) Func2x3

func ToFunc2x3

func ToFunc2x3(c Func) Func2x3

type Func3x0

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

func MakeFunc3x0

func MakeFunc3x0(fn interface{}) Func3x0

func ToFunc3x0

func ToFunc3x0(c Func) Func3x0

type Func3x1

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

func MakeFunc3x1

func MakeFunc3x1(fn interface{}) Func3x1

func ToFunc3x1

func ToFunc3x1(c Func) Func3x1

type Func3x2

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

func MakeFunc3x2

func MakeFunc3x2(fn interface{}) Func3x2

func ToFunc3x2

func ToFunc3x2(c Func) Func3x2

type Func3x3

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

func MakeFunc3x3

func MakeFunc3x3(fn interface{}) Func3x3

func ToFunc3x3

func ToFunc3x3(c Func) Func3x3

type Func4x0

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

func MakeFunc4x0

func MakeFunc4x0(fn interface{}) Func4x0

func ToFunc4x0

func ToFunc4x0(c Func) Func4x0

type Func4x1

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

func MakeFunc4x1

func MakeFunc4x1(fn interface{}) Func4x1

func ToFunc4x1

func ToFunc4x1(c Func) Func4x1

type Func4x2

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

func MakeFunc4x2

func MakeFunc4x2(fn interface{}) Func4x2

func ToFunc4x2

func ToFunc4x2(c Func) Func4x2

type Func4x3

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

func MakeFunc4x3

func MakeFunc4x3(fn interface{}) Func4x3

func ToFunc4x3

func ToFunc4x3(c Func) Func4x3

type Func5x0

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

func MakeFunc5x0

func MakeFunc5x0(fn interface{}) Func5x0

func ToFunc5x0

func ToFunc5x0(c Func) Func5x0

type Func5x1

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

func MakeFunc5x1

func MakeFunc5x1(fn interface{}) Func5x1

func ToFunc5x1

func ToFunc5x1(c Func) Func5x1

type Func5x2

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

func MakeFunc5x2

func MakeFunc5x2(fn interface{}) Func5x2

func ToFunc5x2

func ToFunc5x2(c Func) Func5x2

type Func5x3

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

func MakeFunc5x3

func MakeFunc5x3(fn interface{}) Func5x3

func ToFunc5x3

func ToFunc5x3(c Func) Func5x3

type Func6x0

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

func MakeFunc6x0

func MakeFunc6x0(fn interface{}) Func6x0

func ToFunc6x0

func ToFunc6x0(c Func) Func6x0

type Func6x1

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

func MakeFunc6x1

func MakeFunc6x1(fn interface{}) Func6x1

func ToFunc6x1

func ToFunc6x1(c Func) Func6x1

type Func6x2

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

func MakeFunc6x2

func MakeFunc6x2(fn interface{}) Func6x2

func ToFunc6x2

func ToFunc6x2(c Func) Func6x2

type Func6x3

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

func MakeFunc6x3

func MakeFunc6x3(fn interface{}) Func6x3

func ToFunc6x3

func ToFunc6x3(c Func) Func6x3

type Func7x0

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

func MakeFunc7x0

func MakeFunc7x0(fn interface{}) Func7x0

func ToFunc7x0

func ToFunc7x0(c Func) Func7x0

type Func7x1

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

func MakeFunc7x1

func MakeFunc7x1(fn interface{}) Func7x1

func ToFunc7x1

func ToFunc7x1(c Func) Func7x1

type Func7x2

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

func MakeFunc7x2

func MakeFunc7x2(fn interface{}) Func7x2

func ToFunc7x2

func ToFunc7x2(c Func) Func7x2

type Func7x3

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

func MakeFunc7x3

func MakeFunc7x3(fn interface{}) Func7x3

func ToFunc7x3

func ToFunc7x3(c Func) Func7x3

Jump to

Keyboard shortcuts

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