reflect

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotAFunction is returned when the type is not a function.
	ErrNotAFunction = errors.New("type is not a function")

	// ErrNotAStruct is returned when the type is not a struct.
	ErrNotAStruct = errors.New("type is not a struct")

	// ErrWrongNumArgs is returned when the number of arguments is wrong.
	ErrWrongNumArgs = errors.New("wrong number of arguments")

	// ErrInvalidArgType is returned when an argument type is invalid.
	ErrInvalidArgType = errors.New("invalid argument type")

	// ArgValueIsZeroErrMsg is the error message for an invalid argument value.
	ArgValueIsZeroErrMsg = "invalid argument value for type %s: got zero value"
)
View Source
var (
	TypeOf    = reflect.TypeOf
	ValueOf   = reflect.ValueOf
	MakeFunc  = reflect.MakeFunc
	MakeSlice = reflect.MakeSlice

	Interface = reflect.Interface
	Ptr       = reflect.Ptr
	Struct    = reflect.Struct
	Slice     = reflect.Slice
	Array     = reflect.Array
)

Functions

func GetFunctionName

func GetFunctionName(f any) string

GetFunctionName returns the name of the function.

func IsError

func IsError(t Type) bool

IsError returns true if the given type is an error.

func MakeInitializedSlice

func MakeInitializedSlice(
	sliceType reflect.Type, values ...reflect.Value,
) reflect.Value

MakeInitializedSlice creates a slice of the given type with the given values initialized.

Types

type Arg

type Arg struct {
	Type

	// Whether the argument is variadic.
	IsVariadic bool

	// Whether the argument is a slice.
	IsSlice bool

	// Whether the argument is an array.
	IsArray   bool
	ArraySize int
}

func NewArg

func NewArg(t Type, isVariadic bool) *Arg

func (*Arg) IsType

func (a *Arg) IsType(t Type, inferInterfaces bool) bool

IsType returns whether the type matches the argument type.

type Func

type Func struct {
	// Name is the name of the function.
	// It is formatted as "package.functionName".
	Name string

	// Args is the argument types of the function.
	Args []*Arg

	// Ret is a mapping of return types to values of the function.
	Ret map[Type]Value

	// IsVariadic is true if the function is variadic.
	IsVariadic bool

	// HasError is true if the function returns an error.
	HasError bool
	// contains filtered or unexported fields
}

A Func is a wrapper around a reflect function value that provides convenience functions to get metadata and execute a function.

func MakeNamedFunc

func MakeNamedFunc(args []Type, ret []Type, fn func([]Value) []Value, prefix string) *Func

MakeNamedFunc creates a new Func instance from the given argument and return values. It generates a function which when called consumes the specified args and returns the given return values. It assigns this function a name which is formatted as "GeneratedFuncArgs{argTypes...}Ret{retTypes...}".

func WrapFunc

func WrapFunc(f any) (*Func, error)

WrapFunc wraps an existing go function into a Func instance. It returns an error if the reflect.TypeOf(f) is not a function.

func (*Func) Call

func (f *Func) Call(inferInterfaces bool, args ...any) error

Call calls the original function with the given arguments.

type StructType

type StructType struct {
	Name string

	Type Type

	// Fields is a mapping of field types to their indices.
	Fields *utils.OrderedMap[Type, int]
}

func NewStruct

func NewStruct(s any) (*StructType, error)

func (*StructType) Constructor

func (s *StructType) Constructor() *Func

Constructor returns a function that constructs a new instance of the struct.

func (*StructType) Provider

func (s *StructType) Provider() *Func

Provider returns a function that takes in an instance of the struct and returns the value of each field as output.

type Type

type Type = reflect.Type

type Value

type Value = reflect.Value

Jump to

Keyboard shortcuts

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