reflectx

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 5 Imported by: 0

README

ReflectX

Type-safe, modern helpers for Go reflection and value extraction.

Usage Example

package main

import (
    "fmt"
    "github.com/kaptinlin/gozod/pkg/reflectx"
    "github.com/kaptinlin/gozod/pkg/coerce"
)

func main() {
    var value any = "hello world"

    // Type checking
    if reflectx.IsString(value) {
        fmt.Println("Is string type")
    }

    // Value extraction
    if str, ok := reflectx.ExtractString(value); ok {
        fmt.Printf("String value: %s\n", str)
    }

    // Property checking
    if reflectx.HasLength(value) {
        if length, ok := reflectx.GetLength(value); ok {
            fmt.Printf("Length: %d\n", length)
        }
    }

    // Pointer operations
    ptr := &value
    if deref, ok := reflectx.Deref(ptr); ok {
        fmt.Printf("Dereferenced: %v\n", deref)
    }

    // Generic type conversion
    if result, err := reflectx.ConvertToGeneric[string](123); err == nil {
        fmt.Printf("Generic conversion: %s\n", result)
    }

    // Integration with coerce for conversions
    if reflectx.IsNumeric(123) {
        if str, err := coerce.ToString(123); err == nil {
            fmt.Printf("Number as string: %s\n", str)
        }
    }
}

Quick Reference

import "github.com/kaptinlin/gozod/pkg/reflectx"

// Type checking
reflectx.IsString(val)      // true if string
reflectx.IsNumeric(val)     // true if numeric type
reflectx.IsMap(val)         // true if map
reflectx.IsSlice(val)       // true if slice
reflectx.IsNil(val)         // true if nil
reflectx.IsZero(val)        // true if zero value
reflectx.IsPointer(val)     // true if pointer

// Property
reflectx.HasLength(val)     // true if has length property
length, _ := reflectx.GetLength(val)

// Value extraction
reflectx.ExtractString(val) // (string, ok)
reflectx.ExtractInt(val)    // (int, ok)
reflectx.ExtractSlice(val)  // (slice, ok)
reflectx.ExtractMap(val)    // (map, ok)

// Size helpers
size, _ := reflectx.GetSize(val)

// Pointer operations
reflectx.Deref(ptr)         // (value, ok)
reflectx.DerefAll(ptr)      // (value, ok)
reflectx.ToPointer(val)     // pointer to value

// Conversion
reflectx.ConvertToGeneric[T](val) // (T, error)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilValue              = errors.New("cannot convert nil")
	ErrUnsupportedConversion = errors.New("unsupported conversion")
)

Sentinel errors returned by ConvertToGeneric.

Functions

func ConvertToGeneric

func ConvertToGeneric[T any](v any) (T, error)

ConvertToGeneric converts arbitrary value v into type T using reflection only when necessary. It returns zero value of T plus an error when conversion is impossible. Fast paths are kept extremely short for performance.

func Deref

func Deref(v any) (val any, ok bool)

Deref returns the value pointed to by v. If v is nil, not a pointer, or the pointer is nil, it returns (nil, false). When v is not a pointer the value is returned as-is with ok = true.

func DerefAll

func DerefAll(v any) any

DerefAll recursively follows pointer chains until a non-pointer value (or nil) is reached. A nil input or nil pointer yields nil.

func ExtractString

func ExtractString(v any) (str string, ok bool)

ExtractString returns the string value if v is a string. Otherwise ok=false.

func GetLength

func GetLength(v any) (length int, ok bool)

GetLength returns len(v) and ok=true when HasLength(v) is true.

func GetSize

func GetSize(v any) (size int, ok bool)

GetSize returns len(v) and ok=true when HasSize(v) is true.

func HasLength

func HasLength(v any) bool

HasLength reports whether v supports the built-in len() function (string, array, slice).

func HasSize

func HasSize(v any) bool

HasSize reports whether v has size semantics (map, chan, slice, array).

func IsArray

func IsArray(v any) bool

IsArray checks if a value is an array

func IsBigInt

func IsBigInt(v any) bool

IsBigInt checks if a value is a big.Int type

func IsBool

func IsBool(v any) bool

IsBool checks if a value is a bool type

func IsChan

func IsChan(v any) bool

IsChan checks if a value is a channel

func IsComparable

func IsComparable(v any) bool

IsComparable checks if a value's type is comparable

func IsComplex

func IsComplex(v any) bool

IsComplex checks if a value is any complex type

func IsError

func IsError(v any) bool

IsError checks if a value implements the error interface

func IsFloat

func IsFloat(v any) bool

IsFloat checks if a value is any float type

func IsFunc

func IsFunc(v any) bool

IsFunc checks if a value is a function

func IsInt

func IsInt(v any) bool

IsInt checks if a value is any integer type

func IsInterface

func IsInterface(v any) bool

IsInterface checks if a value is an interface

func IsIterable

func IsIterable(v any) bool

IsIterable checks if a value can be iterated (array, slice, map, string)

func IsMap

func IsMap(v any) bool

IsMap checks if a value is a map

func IsNil

func IsNil(v any) bool

IsNil checks if a value is nil (including typed nil)

func IsNumber added in v0.3.1

func IsNumber(v any) bool

IsNumber checks if a value is any numeric type (alias for IsNumeric)

func IsNumeric

func IsNumeric(v any) bool

IsNumeric checks if a value is any numeric type

func IsPointer

func IsPointer(v any) bool

IsPointer checks if a value is a pointer

func IsSlice

func IsSlice(v any) bool

IsSlice checks if a value is a slice

func IsString

func IsString(v any) bool

IsString checks if a value is a string type

func IsStringer

func IsStringer(v any) bool

IsStringer checks if a value implements the fmt.Stringer interface

func IsStruct

func IsStruct(v any) bool

IsStruct checks if a value is a struct

func IsUint

func IsUint(v any) bool

IsUint checks if a value is any unsigned integer type

func IsValid

func IsValid(v any) bool

IsValid checks if a reflect.Value is valid

func IsZero

func IsZero(v any) bool

IsZero checks if a value is the zero value for its type

func ParsedCategory

func ParsedCategory(v any) string

ParsedCategory returns the general category of a type

func ParsedKind

func ParsedKind(v any) reflect.Kind

ParsedKind returns the reflect.Kind of a value

func ParsedType

func ParsedType(v any) core.ParsedType

ParsedType returns the parsed type for runtime type detection This corresponds to Zod v4's getParsedType() function which returns ParsedTypes See: .reference/zod/packages/zod/src/v4/core/util.ts:412

Types

This section is empty.

Jump to

Keyboard shortcuts

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