reflectx

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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 added in v0.18.0

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

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

func ConvertValue

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

ConvertValue converts a source value to the target 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 IsPointerToStruct added in v0.18.0

func IsPointerToStruct(t reflect.Type) bool

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

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