reflex

package
v14.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	StringType     = reflect.TypeFor[string]()
	BytesType      = reflect.TypeFor[[]byte]()
	IntType        = reflect.TypeFor[int]()
	Int8Type       = reflect.TypeFor[int8]()
	Int16Type      = reflect.TypeFor[int16]()
	Int32Type      = reflect.TypeFor[int32]()
	Int64Type      = reflect.TypeFor[int64]()
	UintType       = reflect.TypeFor[uint]()
	Uint8Type      = reflect.TypeFor[uint8]()
	Uint16Type     = reflect.TypeFor[uint16]()
	Uint32Type     = reflect.TypeFor[uint32]()
	Uint64Type     = reflect.TypeFor[uint64]()
	Float32Type    = reflect.TypeFor[float32]()
	Float64Type    = reflect.TypeFor[float64]()
	BoolType       = reflect.TypeFor[bool]()
	TimeType       = reflect.TypeFor[time.Time]()
	WeekdayType    = reflect.TypeFor[time.Weekday]()
	DurationType   = reflect.TypeFor[time.Duration]()
	UUIDType       = reflect.TypeFor[uuid.UUID]()
	NetipAddrType  = reflect.TypeFor[netip.Addr]()
	StdContextType = reflect.TypeFor[stdContext.Context]()
	IpTyp          = reflect.TypeFor[net.IP]()
	JSONNumberTyp  = reflect.TypeFor[json.Number]() // gojson.Number is an alias of this.

	JSONRawMessageTyp = reflect.TypeFor[json.RawMessage]() // gojson.RawMessage is an alias of this.

	StringerTyp           = reflect.TypeFor[fmt.Stringer]()
	ArrayIntegerTyp       = reflect.TypeFor[[]int]()
	ArrayStringTyp        = reflect.TypeFor[[]string]()
	DoubleArrayIntegerTyp = reflect.TypeFor[[][]int]()
	DoubleArrayStringTyp  = reflect.TypeFor[[][]string]()
	ErrTyp                = reflect.TypeFor[error]()
)

Common reflect types for go standard data types.

View Source
var PathParamReplacer = strings.NewReplacer("/", "_", "{", "_", "}", "_", ":", "_", "(", "_", ")", "_")

PathParamReplacer is a replacer that replaces the characters in a path parameter name.

Functions

func DeepClone

func DeepClone[T any](t T) T

DeepClone recursively clones the fields of a struct.

func FirstStructFieldByTag

func FirstStructFieldByTag(fields []reflect.StructField, fieldTag string) (reflect.StructField, bool)

FirstStructFieldByTag returns the first field that contains the "fieldTag" tag.

func FuncFileLine

func FuncFileLine(v any) (file string, line int)

FuncFileLine returns the func's file and line information.

func FuncName

func FuncName(v any) string

FuncName returns the raw name of a function. Use the NameResolver to get the custom name.

func FuncPtrNameAndFileLine

func FuncPtrNameAndFileLine(ptr uintptr) (funcName, filename string, line int)

FuncPtrNameAndFileLine returns the function name, the filename and the line of the function at the given program counter.

Display-only fidelity: since Go 1.27 two distinct code-identical function literals may share a code pointer, in which case the reported name, file and line belong to whichever literal the compiler kept. Never use these values as a function identity; they are for tracing and debug output.

func IndirectType

func IndirectType(typ reflect.Type) reflect.Type

IndirectType returns the value of a pointer-type "typ". If "IndirectType" is a pointer, array, chan, map or slice it returns its Elem, otherwise returns the "typ" as it is.

func IndirectValue

func IndirectValue(v any) reflect.Value

IndirectValue returns the value a pointer points to (e.g. for a *User value it returns the User value). Non-pointer values are returned as they are and a nil pointer returns the zero reflect.Value.

func IsError

func IsError(typ interface{ Implements(reflect.Type) bool }) bool

IsError reports whether "typ" is an error type.

func IsFunc

func IsFunc(typ interface{ Kind() reflect.Kind }) bool

IsFunc reports whether the "kindable" is a type of function.

func IsStruct

func IsStruct(v any) bool

IsStruct reports whether the "v" is a type of struct.

func IsStructPtr

func IsStructPtr(v any) bool

IsStructPtr reports whether the "v" is a type of *struct.

func IsZero

func IsZero(v any) bool

IsZero reports whether "v" is zero value or no. The given "v" value can complete the Zeroer interface which can be used to customize the behavior for each type of "v".

func LookupFields

func LookupFields(typ reflect.Type, fieldTag string) []reflect.StructField

LookupFields returns a slice of all fields containing a struct field of the given "fieldTag" of the "typ" struct. The fields returned are flatted and reclusive over fields with value of struct. Panics if "typ" is not a type of Struct.

func LookupFieldsWithOptions

func LookupFieldsWithOptions(opts *LookupFieldsOptions, typ reflect.Type) ([]reflect.StructField, error)

LookupFieldsWithOptions returns a slice of all fields containing a struct field of the "typ" struct. The fields returned are flatted and reclusive over fields with value of struct. Panics if "typ" is not a type of Struct.

func LookupUnderlineValueType

func LookupUnderlineValueType(v reflect.Value) (reflect.Value, reflect.Type)

LookupUnderlineValueType returns the underline type of "v".

func NameOf

func NameOf(v any) string

NameOf returns the package name + the struct type's name.

func PointerToObject

func PointerToObject[T any](ptr unsafe.Pointer) T

PointerToObject converts an unsafe.Pointer to a value of T type.

func ShortNameOf

func ShortNameOf(v any) string

ShortNameOf returns the short name of a struct type.

func SnakeCase

func SnakeCase(camel string) string

SnakeCase converts a given string to a friendly snake case, e.g. - userId to user_id - ID to id - ProviderAPIKey to provider_api_key - Option to option

func StructFieldsCountByTag

func StructFieldsCountByTag(fields []reflect.StructField, fieldTag string) int

StructFieldsCountByTag returns the count of the fields that contains the "fieldTag" tag.

func StructFieldsImplementers

func StructFieldsImplementers(fields []reflect.StructField, iface reflect.Type) []reflect.StructField

StructFieldsImplementers returns a slice of all fields that their type implements the "iface" explicitly.

func TypeOf

func TypeOf(v any) reflect.Type

TypeOf returns the reflect.Type of the given value.

func TypeToPointer

func TypeToPointer(t reflect.Type) unsafe.Pointer

TypeToPointer converts a reflect.Type to unsafe.Pointer type.

func UnsafeClone

func UnsafeClone[T any](src *T) *T

UnsafeClone creates a new instance of a value by copying the memory block. 'src' is a pointer to the source value. The function returns a pointer to the newly created value. Note: The function does not perform a deep copy of the value. It only copies the memory block, so the new value will share pointers with the original value.

func UnsafeCopy

func UnsafeCopy(dst, src unsafe.Pointer, size uintptr)

UnsafeCopy copies a block of memory from 'src' to 'dst'. 'dst' and 'src' are pointers to the destination and source memory blocks, respectively, and 'size' is the number of bytes to copy.

func UnsafeSet

func UnsafeSet(structPtr unsafe.Pointer, offset uintptr, valuePtr unsafe.Pointer)

UnsafeSet sets a value at a specified offset within a struct. 'structPtr' is a pointer to the struct, 'offset' is the byte offset within the struct where the value should be set, and 'valuePtr' is a pointer to the value to be set.

func UnsafeSet2

func UnsafeSet2(memoryPtr unsafe.Pointer, offset uintptr, valuePtr unsafe.Pointer, size uintptr)

UnsafeSet2 sets a value at a specified offset within a memory block. 'memoryPtr' is a pointer to the start of the memory block, 'offset' is the byte offset within the block where the value should be set, 'valuePtr' is a pointer to the value to be set and 'size' is the number of bytes to copy.

func ValueOf

func ValueOf(v any) reflect.Value

ValueOf returns the reflect.Value of the given value.

func ValueToPointer

func ValueToPointer(v reflect.Value) unsafe.Pointer

ValueToPointer converts a reflect.Value to unsafe.Pointer type.

Types

type Frame

type Frame struct {
	// Function name.
	Function string
	// File name.
	File string
	// Line number.
	Line int
}

Frame represents a single frame of a stack trace.

func GetInternalStackTrace

func GetInternalStackTrace() []Frame

GetInternalStackTrace returns all the stack trace of the caller, including Iris components.

func GetStackTrace

func GetStackTrace() []Frame

GetStackTrace returns the stack trace of the caller, without the Iris internal frames. This is a very very simple implementation. See `Context.HandlerName` package-level function too.

func NewFrame

func NewFrame(f runtime.Frame) Frame

func (Frame) FileLine

func (f Frame) FileLine() string

FileLine returns the file and line number of the frame as a single string separated by a colon.

func (Frame) String

func (f Frame) String() string

String returns the frame as a string in the format "Function (File:Line)".

type FuncParam

type FuncParam struct {
	Index int
	Type  reflect.Type
}

FuncParam holds the properties of function input or output.

func LookupInputs

func LookupInputs(fn reflect.Type) []FuncParam

LookupInputs returns the index and type of each function's input argument. Panics if "fn" is not a type of Func.

func LookupOutputs

func LookupOutputs(fn reflect.Type) []FuncParam

LookupOutputs returns the index and type of each function's output argument. Panics if "fn" is not a type of Func.

type LookupFieldsOptions

type LookupFieldsOptions struct {
	// SkipUnexported reports whether to skip unexported fields.
	SkipUnexported bool
	// SkipTypes reports whether to skip fields that their type is in the list.
	SkipTypes []reflect.Type
	// SkipStructTypes reports whether to skip fields that their type is a struct and match the list.
	SkipStructTypes []reflect.Type
	// SkipEmbedded reports whether to skip embedded fields.
	SkipEmbedded bool
	// SkipPointers reports whether to skip pointer fields.
	SkipPointers bool
	// FieldTag is the tag name that the fields should contain.
	FieldTag string
	// SkipField reports whether to skip the field. If it returns true, the field will be skipped.
	SkipField func(field reflect.StructField) (bool, error)
	// FlatOffsets reports whether the caller intends to reach the returned fields
	// by pointer arithmetic (unsafe.Add) from the outermost struct value, using
	// the reported Offset.
	//
	// When true, a struct field held by POINTER is not flattened: its inner
	// fields live at a different allocation, so no offset relative to the outer
	// struct can reach them, and writing at such an offset would corrupt whatever
	// the outer struct actually holds there. The pointer field is reported as a
	// leaf instead, which lets the caller's field setter reject it cleanly.
	//
	// Struct fields held by value are flattened either way, and their Offset is
	// always reported relative to the outermost struct.
	FlatOffsets bool
}

LookupFieldsOptions is the options for the LookupFields function.

func (*LookupFieldsOptions) CanProccess

func (opts *LookupFieldsOptions) CanProccess(field reflect.StructField) (reflect.Type, bool, error)

CanProccess reports whether the given struct field should be processed and, if so, returns its indirect type.

type StructFieldValue

type StructFieldValue struct {
	Field reflect.StructField
	Value reflect.Value
}

StructFieldValue is a struct field value with its index. It's used in the StructFieldsValues function.

func StructFieldsValues

func StructFieldsValues(opts *StructFieldsValuesOptions, v reflect.Value, fields []reflect.StructField) []StructFieldValue

StructFieldsValues returns a slice of all fields values of the "v" struct, with their indexes.

type StructFieldsValuesOptions

type StructFieldsValuesOptions struct {
	// SkipNonZero reports whether to skip non zero struct's fields values.
	SkipNonZero bool
}

StructFieldsValuesOptions is the options for the StructFieldsValues function.

type UnsafeInterface

type UnsafeInterface struct {
	Type uintptr        // (*[2]uintptr)(unsafe.Pointer(&v))[0]
	Data unsafe.Pointer // (*[2]uintptr)(unsafe.Pointer(&v))[1]
}

UnsafeInterface is a mimic of the internal eface structure, made after a whole day of research. It is used to access the internal data of an any. It is not safe to use and sometimes its performance is worse than the safe way (casting), use it only if you know what you are doing.

Data is unsafe.Pointer rather than uintptr on purpose, mirroring the runtime's own eface. A uintptr is a plain integer that the garbage collector does not trace, so storing an address in one and converting it back to a pointer later is invalid under the unsafe.Pointer rules. Declaring it as a pointer keeps the word visible to the collector and makes Ptr a field read instead of a cast.

func UnsafeInterfaceOf

func UnsafeInterfaceOf(v any) *UnsafeInterface

UnsafeInterfaceOf returns the (unsafe) interface of the data.

func (*UnsafeInterface) Ptr

func (i *UnsafeInterface) Ptr() unsafe.Pointer

Ptr returns the unsafe.Pointer of the data.

func (*UnsafeInterface) UnsafeKind

func (i *UnsafeInterface) UnsafeKind() UnsafeKind

UnsafeKind returns the (unsafe) kind of the data.

func (*UnsafeInterface) UnsafeType

func (t *UnsafeInterface) UnsafeType() *UnsafeType

UnsafeType returns the (unsafe) type of the data.

func (*UnsafeInterface) ValueBool

func (i *UnsafeInterface) ValueBool() bool

ValueBool returns the bool value of the data.

func (*UnsafeInterface) ValueComplex64

func (i *UnsafeInterface) ValueComplex64() complex64

ValueComplex64 returns the complex64 value of the data.

func (*UnsafeInterface) ValueComplex128

func (i *UnsafeInterface) ValueComplex128() complex128

ValueComplex128 returns the complex128 value of the data.

func (*UnsafeInterface) ValueFloat32

func (i *UnsafeInterface) ValueFloat32() float32

ValueFloat32 returns the float32 value of the data.

func (*UnsafeInterface) ValueFloat64

func (i *UnsafeInterface) ValueFloat64() float64

ValueFloat64 returns the float64 value of the data.

func (*UnsafeInterface) ValueFunc

func (i *UnsafeInterface) ValueFunc() func()

ValueFunc returns the func value of the data.

func (*UnsafeInterface) ValueInt

func (i *UnsafeInterface) ValueInt() int

ValueInt returns the int value of the data.

func (*UnsafeInterface) ValueInt8

func (i *UnsafeInterface) ValueInt8() int8

ValueInt8 returns the int8 value of the data.

func (*UnsafeInterface) ValueInt16

func (i *UnsafeInterface) ValueInt16() int16

ValueInt16 returns the int16 value of the data.

func (*UnsafeInterface) ValueInt32

func (i *UnsafeInterface) ValueInt32() int32

ValueInt32 returns the int32 value of the data.

func (*UnsafeInterface) ValueInt64

func (i *UnsafeInterface) ValueInt64() int64

ValueInt64 returns the int64 value of the data.

func (*UnsafeInterface) ValueInterface

func (i *UnsafeInterface) ValueInterface() any

ValueInterface returns the any value of the data.

func (*UnsafeInterface) ValueString

func (i *UnsafeInterface) ValueString() string

ValueString returns the string value of the data.

func (*UnsafeInterface) ValueUUID

func (i *UnsafeInterface) ValueUUID() uuid.UUID

ValueUUID returns the uuid.UUID value of the data.

func (*UnsafeInterface) ValueUUIDPtr

func (i *UnsafeInterface) ValueUUIDPtr() *uuid.UUID

ValueUUIDPtr returns the *uuid.UUID value of the data.

func (*UnsafeInterface) ValueUint

func (i *UnsafeInterface) ValueUint() uint

ValueUint returns the uint value of the data.

func (*UnsafeInterface) ValueUint8

func (i *UnsafeInterface) ValueUint8() uint8

ValueUint8 returns the uint8 value of the data.

func (*UnsafeInterface) ValueUint16

func (i *UnsafeInterface) ValueUint16() uint16

ValueUint16 returns the uint16 value of the data.

func (*UnsafeInterface) ValueUint32

func (i *UnsafeInterface) ValueUint32() uint32

ValueUint32 returns the uint32 value of the data.

func (*UnsafeInterface) ValueUint64

func (i *UnsafeInterface) ValueUint64() uint64

ValueUint64 returns the uint64 value of the data.

func (*UnsafeInterface) ValueUintptr

func (i *UnsafeInterface) ValueUintptr() uintptr

ValueUintptr returns the uintptr value of the data.

func (*UnsafeInterface) ValueUnsafePointer

func (i *UnsafeInterface) ValueUnsafePointer() unsafe.Pointer

ValueUnsafePointer returns the unsafe.Pointer value of the data.

type UnsafeKind

type UnsafeKind uint8

UnsafeKind represents the (unsafe) kind of the data. See `UnsafeInterface.UnsafeKind` method.

const (
	// UnsafeInvalidKind is the invalid kind.
	UnsafeInvalidKind UnsafeKind = iota
	// UnsafeBoolKind is the bool kind.
	UnsafeBoolKind
	// UnsafeIntKind is the int kind.
	UnsafeIntKind
	// UnsafeInt8Kind is the int8 kind.
	UnsafeInt8Kind
	// UnsafeInt16Kind is the int16 kind.
	UnsafeInt16Kind
	// UnsafeInt32Kind is the int32 kind.
	UnsafeInt32Kind
	// UnsafeInt64Kind is the int64 kind.
	UnsafeInt64Kind
	// UnsafeUintKind is the uint kind.
	UnsafeUintKind
	// UnsafeUint8Kind is the uint8 kind.
	UnsafeUint8Kind
	// UnsafeUint16Kind is the uint16 kind.
	UnsafeUint16Kind
	// UnsafeUint32Kind is the uint32 kind.
	UnsafeUint32Kind
	// UnsafeUint64Kind is the uint64 kind.
	UnsafeUint64Kind
	// UnsafeUintptrKind is the uintptr kind.
	UnsafeUintptrKind
	// UnsafeFloat32Kind is the float32 kind.
	UnsafeFloat32Kind
	// UnsafeFloat64Kind is the float64 kind.
	UnsafeFloat64Kind
	// UnsafeComplex64Kind is the complex64 kind.
	UnsafeComplex64Kind
	// UnsafeComplex128Kind is the complex128 kind.
	UnsafeComplex128Kind
	// UnsafeArrayIntKind is the array(int) kind.
	UnsafeArrayIntKind
	// UnsafeChanIntKind is the chan(int) kind.
	UnsafeChanIntKind
	// UnsafeFuncKind is the func kind.
	UnsafeFuncKind
	// UnsafeInterfaceKind is the interface kind.
	UnsafeInterfaceKind
	// UnsafeMapKind is the map[int]int kind.
	UnsafeMapKind
	// UnsafePtrKind is the ptr kind.
	UnsafePtrKind
	// UnsafeSliceKind is the slice kind.
	UnsafeSliceKind
	// UnsafeStringKind is the string kind.
	UnsafeStringKind
	// UnsafeStructKind is the struct kind.
	UnsafeStructKind
	// UnsafeUnsafePointerKind is the unsafe.Pointer kind.
	UnsafeUnsafePointerKind
	// UnsafeUUIDKind is the uuid.UUID kind.
	UnsafeUUIDKind
)

func UnsafeKindOf

func UnsafeKindOf(v any) UnsafeKind

UnsafeKindOf returns the (unsafe) kind of the data.

func (UnsafeKind) String

func (k UnsafeKind) String() string

String returns the string representation of the (unsafe) kind.

type UnsafeType

type UnsafeType struct {
	// contains filtered or unexported fields
}

UnsafeType is a mimic of the internal rtype structure, made after a whole day of research. It is used to access the internal type's data of an any value. It is not safe to use, but it is useful for performance.

func (*UnsafeType) Kind

func (t *UnsafeType) Kind() reflect.Kind

Kind returns the reflect.Kind of the type.

func (*UnsafeType) Size

func (t *UnsafeType) Size() uintptr

Size returns the size of the type.

type Zeroer

type Zeroer interface {
	IsZero() bool
}

Zeroer can be implemented by custom types to report whether its current value is zero. Standard Time also implements that.

Jump to

Keyboard shortcuts

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