Documentation
¶
Index ¶
- Variables
- func ApplyIfString[T any](value any, fn func(string) T, defaultValue ...T) T
- func CollectMethods(target reflect.Value) map[string]reflect.Value
- func Contains(collection, element any) bool
- func ConvertValue(sourceValue reflect.Value, targetType reflect.Type) (reflect.Value, error)
- func Equal(a, b any) bool
- func FindMethod(target reflect.Value, name string) reflect.Value
- func Indirect(t reflect.Type) reflect.Type
- func IsEmpty(value any) bool
- func IsFloat(value any) bool
- func IsInteger(value any) bool
- func IsNotEmpty(value any) bool
- func IsNumeric(value any) bool
- func IsPointerToStruct(t reflect.Type) bool
- func IsSignedInt(value any) bool
- func IsSimilarType(t1, t2 reflect.Type) bool
- func IsTypeCompatible(sourceType, targetType reflect.Type) bool
- func IsUnsignedInt(value any) bool
- func ToDecimal(value any) decimal.Decimal
- func ToDecimalE(value any) (decimal.Decimal, error)
- func Visit(target reflect.Value, visitor Visitor, opts ...VisitorOption)
- func VisitFor[T any](visitor TypeVisitor, opts ...VisitorOption)
- func VisitOf(value any, visitor Visitor, opts ...VisitorOption)
- func VisitType(targetType reflect.Type, visitor TypeVisitor, opts ...VisitorOption)
- type FieldTypeVisitor
- type FieldVisitor
- type MethodTypeVisitor
- type MethodVisitor
- type StructTypeVisitor
- type StructVisitor
- type TagConfig
- type TraversalMode
- type TypeVisitor
- type VisitAction
- type Visitor
- type VisitorConfig
- type VisitorOption
Constants ¶
This section is empty.
Variables ¶
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.
var ErrCannotConvertType = errors.New("cannot convert source type to target type")
ErrCannotConvertType indicates source type cannot be converted to target type.
Functions ¶
func ApplyIfString ¶
ApplyIfString applies a function to a string value, returning defaultValue for non-strings.
func CollectMethods ¶
CollectMethods collects all methods from a target value as a name-to-value map.
func Contains ¶
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 ¶
ConvertValue converts a source value to the target type.
func Equal ¶
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 ¶
FindMethod finds a method on a target value (includes pointer receiver and promoted methods).
func IsEmpty ¶
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 IsNotEmpty ¶
IsNotEmpty returns true if the value is not empty.
func IsNumeric ¶
IsNumeric checks if a value is a numeric type (signed integer, unsigned integer, or float).
func IsPointerToStruct ¶
IsPointerToStruct checks if the given type is a pointer to a struct.
func IsSignedInt ¶
IsSignedInt checks if a value is a signed integer type (int, int8, int16, int32, int64).
func IsSimilarType ¶
IsSimilarType checks if two types are similar (identical or same generic base type).
func IsTypeCompatible ¶
IsTypeCompatible checks if sourceType is compatible with targetType.
func IsUnsignedInt ¶
IsUnsignedInt checks if a value is an unsigned integer type (uint, uint8, uint16, uint32, uint64, uintptr).
func ToDecimal ¶
ToDecimal converts an arbitrary value to decimal.Decimal, returning Zero on failure.
func ToDecimalE ¶
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 MethodVisitor ¶
type StructTypeVisitor ¶
type StructTypeVisitor func(structType reflect.Type, depth int) VisitAction
type StructVisitor ¶
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