reflectx

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ToString  = cast.ToString
	ToStringE = cast.ToStringE

	ToInt    = cast.ToInt
	ToIntE   = cast.ToIntE
	ToInt8   = cast.ToInt8
	ToInt8E  = cast.ToInt8E
	ToInt16  = cast.ToInt16
	ToInt16E = cast.ToInt16E
	ToInt32  = cast.ToInt32
	ToInt32E = cast.ToInt32E
	ToInt64  = cast.ToInt64
	ToInt64E = cast.ToInt64E

	ToUint    = cast.ToUint
	ToUintE   = cast.ToUintE
	ToUint8   = cast.ToUint8
	ToUint8E  = cast.ToUint8E
	ToUint16  = cast.ToUint16
	ToUint16E = cast.ToUint16E
	ToUint32  = cast.ToUint32
	ToUint32E = cast.ToUint32E
	ToUint64  = cast.ToUint64
	ToUint64E = cast.ToUint64E

	ToFloat32  = cast.ToFloat32
	ToFloat32E = cast.ToFloat32E
	ToFloat64  = cast.ToFloat64
	ToFloat64E = cast.ToFloat64E

	ToBool  = cast.ToBool
	ToBoolE = cast.ToBoolE
)

Common type conversions delegated to cast. E suffix versions return errors; non-E versions return zero values on failure.

View Source
var ErrCannotConvertType = errors.New("cannot convert source type to target type")

ErrCannotConvertType indicates source type cannot be converted to target type.

Functions

func ApplyIfString

func ApplyIfString[T any](value any, fn func(string) T, defaultValue ...T) T

ApplyIfString applies a function to a string value, returning defaultValue for non-strings.

func CollectMethods

func CollectMethods(target reflect.Value) map[string]reflect.Value

CollectMethods collects all methods from a target value as a name-to-value map.

func Contains

func Contains(collection, element any) bool

Contains checks if a collection contains the given element. For slices and arrays: checks if any element is Equal to the target. For maps: checks if the key exists. For strings: checks if the element (must be a string) is a substring.

func ConvertValue

func ConvertValue(sourceValue reflect.Value, targetType reflect.Type) (reflect.Value, error)

ConvertValue converts a source value to the target type.

func Equal

func Equal(a, b any) bool

Equal performs a shallow comparison of two values. Numeric types of different kinds are compared within three categories: signed integers (int64), unsigned integers (uint64), and floats (float64). Nil values are considered equal regardless of type.

func FindMethod

func FindMethod(target reflect.Value, name string) reflect.Value

FindMethod finds a method on a target value (includes pointer receiver and promoted methods).

func Indirect

func Indirect(t reflect.Type) reflect.Type

Indirect returns the underlying type of pointer type.

func IsEmpty

func IsEmpty(value any) bool

IsEmpty checks if a value is considered empty. Returns true for: nil, zero values of basic types (0, "", false), empty collections (len == 0), and nil pointers/interfaces/channels/functions. Special case: *string is dereferenced — a pointer to an empty string is considered empty.

func IsFloat

func IsFloat(value any) bool

IsFloat checks if a value is a floating-point type (float32, float64).

func IsInteger

func IsInteger(value any) bool

IsInteger checks if a value is any integer type (signed or unsigned).

func IsNotEmpty

func IsNotEmpty(value any) bool

IsNotEmpty returns true if the value is not empty.

func IsNumeric

func IsNumeric(value any) bool

IsNumeric checks if a value is a numeric type (signed integer, unsigned integer, or float).

func IsPointerToStruct

func IsPointerToStruct(t reflect.Type) bool

IsPointerToStruct checks if the given type is a pointer to a struct.

func IsSignedInt

func IsSignedInt(value any) bool

IsSignedInt checks if a value is a signed integer type (int, int8, int16, int32, int64).

func IsSimilarType

func IsSimilarType(t1, t2 reflect.Type) bool

IsSimilarType checks if two types are similar (identical or same generic base type).

func IsTypeCompatible

func IsTypeCompatible(sourceType, targetType reflect.Type) bool

IsTypeCompatible checks if sourceType is compatible with targetType.

func IsUnsignedInt

func IsUnsignedInt(value any) bool

IsUnsignedInt checks if a value is an unsigned integer type (uint, uint8, uint16, uint32, uint64, uintptr).

func ToDecimal

func ToDecimal(value any) decimal.Decimal

ToDecimal converts an arbitrary value to decimal.Decimal, returning Zero on failure.

func ToDecimalE

func ToDecimalE(value any) (decimal.Decimal, error)

ToDecimalE converts an arbitrary value to decimal.Decimal. Handles nil and pointer/interface dereferencing, then delegates to decimal.NewFromAny.

func Visit

func Visit(target reflect.Value, visitor Visitor, opts ...VisitorOption)

Visit traverses a struct using visitor callbacks.

func VisitFor

func VisitFor[T any](visitor TypeVisitor, opts ...VisitorOption)

VisitFor visits a struct type T using type visitor callbacks.

func VisitOf

func VisitOf(value any, visitor Visitor, opts ...VisitorOption)

VisitOf visits a struct value using visitor callbacks.

func VisitType

func VisitType(targetType reflect.Type, visitor TypeVisitor, opts ...VisitorOption)

VisitType traverses a struct type using type visitor callbacks.

Types

type FieldTypeVisitor

type FieldTypeVisitor func(field reflect.StructField, depth int) VisitAction

type FieldVisitor

type FieldVisitor func(field reflect.StructField, fieldValue reflect.Value, depth int) VisitAction

type MethodTypeVisitor

type MethodTypeVisitor func(method reflect.Method, receiverType reflect.Type, depth int) VisitAction

type MethodVisitor

type MethodVisitor func(method reflect.Method, methodValue reflect.Value, depth int) VisitAction

type StructTypeVisitor

type StructTypeVisitor func(structType reflect.Type, depth int) VisitAction

type StructVisitor

type StructVisitor func(structType reflect.Type, structValue reflect.Value, depth int) VisitAction

type TagConfig

type TagConfig struct {
	Name  string
	Value string
}

TagConfig configures which tagged fields should be recursively traversed.

type TraversalMode

type TraversalMode int

TraversalMode defines the traversal strategy for visiting struct fields and methods.

const (
	DepthFirst TraversalMode = iota
	BreadthFirst
)

type TypeVisitor

type TypeVisitor struct {
	VisitStructType StructTypeVisitor
	VisitFieldType  FieldTypeVisitor
	VisitMethodType MethodTypeVisitor
}

TypeVisitor defines callback functions for type-only traversal.

type VisitAction

type VisitAction int

VisitAction represents the action to take after visiting a node.

const (
	Continue VisitAction = iota
	Stop
	SkipChildren
)

type Visitor

type Visitor struct {
	VisitStruct StructVisitor
	VisitField  FieldVisitor
	VisitMethod MethodVisitor
}

Visitor defines callback functions for struct traversal.

type VisitorConfig

type VisitorConfig struct {
	TraversalMode TraversalMode
	Recursive     bool
	DiveTag       TagConfig
	MaxDepth      int
}

VisitorConfig configures how the traversal should be performed.

type VisitorOption

type VisitorOption func(*VisitorConfig)

VisitorOption configures visitor behavior.

func WithDisableRecursive

func WithDisableRecursive() VisitorOption

func WithDiveTag

func WithDiveTag(tagName, tagValue string) VisitorOption

func WithMaxDepth

func WithMaxDepth(maxDepth int) VisitorOption

func WithTraversalMode

func WithTraversalMode(mode TraversalMode) VisitorOption

Jump to

Keyboard shortcuts

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