reflectx

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: MIT Imports: 6 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.StringVal(value); ok {
        fmt.Printf("String value: %s\n", str)
    }

    // Property checking
    if reflectx.HasLength(value) {
        if length, ok := reflectx.Length(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.Convert[string](123); err == nil {
        fmt.Printf("Converted: %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.IsBool(val)        // true if bool
reflectx.IsArray(val)       // true if array
reflectx.IsStruct(val)      // true if struct

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

// Value extraction
reflectx.StringVal(val)     // (string, ok)

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

// Pointer operations
reflectx.Deref(ptr)         // (value, ok)
reflectx.DerefAll(ptr)      // fully dereferenced value

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

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

Constants

This section is empty.

Variables

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

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

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

func Deref(v any) (any, bool)

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

func DerefAll(v any) any

DerefAll recursively follows pointer chains until a non-pointer value is reached. A nil input or nil pointer at any level yields nil.

func HasLength

func HasLength(v any) bool

HasLength reports whether v supports len() (string, array, or slice).

func HasSize

func HasSize(v any) bool

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

func IsArray

func IsArray(v any) bool

IsArray reports whether v is an array.

func IsBool

func IsBool(v any) bool

IsBool reports whether v is a bool.

func IsMap

func IsMap(v any) bool

IsMap reports whether v is a map.

func IsNil

func IsNil(v any) bool

IsNil reports whether v is nil, including typed nils (nil pointers, nil slices, nil maps, nil channels, nil functions, and nil interfaces).

func IsNumeric

func IsNumeric(v any) bool

IsNumeric reports whether v is any numeric type (signed/unsigned integer, float, complex, or big.Int).

func IsSlice

func IsSlice(v any) bool

IsSlice reports whether v is a slice.

func IsString

func IsString(v any) bool

IsString reports whether v is a string.

func IsStruct

func IsStruct(v any) bool

IsStruct reports whether v is a struct.

func Length

func Length(v any) (int, bool)

Length returns len(v) for strings, arrays, and slices. It returns (0, false) when v does not support len().

func ParsedCategory

func ParsedCategory(v any) string

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

func Size

func Size(v any) (int, bool)

Size returns len(v) for maps, channels, slices, and arrays. It returns (0, false) when v does not support size semantics.

func StringVal added in v0.6.0

func StringVal(v any) (string, bool)

StringVal returns the string value if v is a string. For nil or non-string values it returns ("", false).

Types

This section is empty.

Jump to

Keyboard shortcuts

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