Documentation
¶
Overview ¶
Package reflectx provides reflection utilities for type checking and value manipulation.
Key features:
- Type checking (IsNil, IsBool, IsString, IsNumeric, etc.)
- Pointer dereferencing (Deref, DerefAll)
- Value extraction (StringVal, Length, Size)
- Type conversion (Convert)
- Runtime type detection aligned with Zod's type system
Usage:
if reflectx.IsNumeric(value) {
// handle numeric value
}
val, ok := reflectx.Deref(maybePtr)
pt := reflectx.ParsedType(value)
Index ¶
- Variables
- func Convert[T any](v any) (T, error)
- func Deref(v any) (any, bool)
- func DerefAll(v any) any
- func HasLength(v any) bool
- func HasSize(v any) bool
- func IsArray(v any) bool
- func IsBool(v any) bool
- func IsMap(v any) bool
- func IsNil(v any) bool
- func IsNumeric(v any) bool
- func IsSlice(v any) bool
- func IsString(v any) bool
- func IsStruct(v any) bool
- func Length(v any) (int, bool)
- func ParsedCategory(v any) string
- func ParsedType(v any) core.ParsedType
- func Size(v any) (int, bool)
- func StringVal(v any) (string, bool)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNil indicates a nil value was passed to [Convert]. ErrNil = errors.New("cannot convert nil") // ErrUnsupported indicates the source type cannot be converted // to the target type via assignment or [reflect.Value.Convert]. ErrUnsupported = errors.New("unsupported conversion") )
Sentinel errors returned by Convert.
Functions ¶
func Convert ¶ added in v0.6.0
Convert converts v to type T. It tries direct assignment first, then falls back to reflect.Value.Convert for safe numeric/string casts. Returns the zero value of T and a wrapped sentinel error (ErrNil or ErrUnsupported) when conversion is impossible.
func Deref ¶
Deref dereferences a single pointer level. For non-pointer concrete values it returns (v, true). For nil, typed nil pointers, or nil pointer values it returns (nil, false).
func DerefAll ¶
DerefAll recursively follows pointer chains until a non-pointer value is reached. A nil input or nil pointer at any level yields nil.
func IsNil ¶
IsNil reports whether v is nil, including typed nils (nil pointers, nil slices, nil maps, nil channels, nil functions, and nil interfaces).
func IsNumeric ¶
IsNumeric reports whether v is any numeric type (signed/unsigned integer, float, complex, or big.Int).
func Length ¶
Length returns len(v) for strings, arrays, and slices. It returns (0, false) when v does not support len().
func ParsedCategory ¶
ParsedCategory returns a broad human-readable category for v: "nil", "bool", "string", "number", "array", "object", or "unknown".
func ParsedType ¶
func ParsedType(v any) core.ParsedType
ParsedType returns the core.ParsedType for runtime type detection. It corresponds to Zod v4's getParsedType() function. Pointers and interfaces are dereferenced recursively; nil yields core.ParsedTypeNil.
See: .reference/zod/packages/zod/src/v4/core/util.ts:412
Types ¶
This section is empty.