reflectx

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package reflectx holds pure reflection helpers extracted from the validate root package. It depends only on goutil and never imports the root package, keeping the dependency direction one-way (root -> internal/reflectx).

Index

Constants

This section is empty.

Variables

View Source
var ErrConvertFail = errors.New("convert value is failure")

ErrConvertFail error. Mirrors validate.ErrConvertFail; kept as a separate value because consumers only test err != nil, never the identity.

View Source
var NilRVal = reflect.ValueOf(nilObj)

NilRVal a reflect nil value (= reflect.ValueOf(NilObject{})).

Functions

func ConvToBasicType

func ConvToBasicType(val any) (value any, err error)

ConvToBasicType convert custom type to generic basic int, string, unit. returns string, int64 or error

func ConvToBasicTypeRV added in v2.0.1

func ConvToBasicTypeRV(rv reflect.Value) (value any, err error)

ConvToBasicTypeRV 同 ConvToBasicType,但直接吃 reflect.Value(复用调用方缓存, 免二次 reflect.ValueOf)。语义与 ConvToBasicType 字节级一致。

func ConvTypeByBaseKind

func ConvTypeByBaseKind(srcVal any, dstType reflect.Kind) (any, error)

ConvTypeByBaseKind convert value type by base kind

func GetVariadicKind

func GetVariadicKind(typ reflect.Type) reflect.Kind

GetVariadicKind name.

usage:

GetVariadicKind(reflect.TypeOf(v))

func IndirectValue

func IndirectValue(input any) any

IndirectValue dereferences a single pointer level of the input value.

func IndirectValueRV added in v2.0.1

func IndirectValueRV(rv reflect.Value, orig any) any

IndirectValueRV is the reflect.Value flavour of IndirectValue: it dereferences a single pointer level of rv and returns the value as any. It mirrors IndirectValue's pointer / nil-pointer / non-pointer branches; orig is the boxed value rv was reflected from and is returned as-is for the non-pointer branch (no fresh boxing alloc — and identical to IndirectValue, which returns the original input unchanged for non-pointers).

NOTE: callers pass a carrier's RealV() here (already de-pointered one level), so this applies the SECOND indirection level — matching the reflect.Call path where the public IsBool receives RealV().Interface() and then does its own IndirectValue. RealV() substitutes NilRVal for a nil src (never an invalid Value), so a nil field surfaces as NilObject{} not nil — same as reflect.Call.

func IsNilObj

func IsNilObj(val any) bool

IsNilObj check value is internal NilObject

func IsNilRV added in v2.0.1

func IsNilRV(rv reflect.Value) bool

IsNilRV reports whether rv is the nil sentinel — an invalid Value or a NilObject — using a box-free Type comparison (no rv.Interface()). The carrier substitutes NilRVal for an untyped-nil src, so this detects "nil field" purely from the reflect.Value.

func RemoveValuePtr

func RemoveValuePtr(t reflect.Value) reflect.Value

RemoveValuePtr removes value multiple pointer

func ValueCompare

func ValueCompare(srcVal, dstVal any, op string) (ok bool)

ValueCompare value compare.

only check for: int(X), uint(X), float(X), string.

Types

type NilObject

type NilObject struct{}

NilObject represent nil value for calling functions and should be reflected at custom filters as nil variable.

NOTE: validate.NilObject is a type alias of this type, so the public API and any val.(NilObject) assertion in user code keep working unchanged.

Jump to

Keyboard shortcuts

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