reflectx

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2025 License: MIT Imports: 8 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	InvalidValue = reflect.Value{}
	InvalidType  = reflect.TypeOf(nil)
)
View Source
var (
	ErrInvalidFlagKey        = errors.New("invalid flag key")
	ErrInvalidFlagValue      = errors.New("invalid flag value")
	ErrInvalidFlagName       = errors.New("invalid flag name")
	ErrInvalidOptionKey      = errors.New("invalid option key")
	ErrInvalidOptionValue    = errors.New("invalid option value")
	ErrInvalidOptionUnquoted = errors.New("invalid option unquoted")
)
View Source
var TypeZeroChecker = reflect.TypeFor[ZeroChecker]()

Functions

func CanCast added in v0.0.34

func CanCast[T any](v any) bool

func CanElemType added in v0.1.1

func CanElemType(v any) bool

CanElemType reports whether the given value's kind supports calling .Elem().

It accepts a value of any type, including `reflect.Kind`, `reflect.Type`, reflect.Value, or other Go values. It returns true if the underlying kind is one of: Array, Chan, Interface, Map, Pointer, or Slice. Otherwise, it returns false. Check if v CanElemType before use reflect.Type.Elem() is recommended to avoid panic

func CanNilValue added in v0.1.1

func CanNilValue(v any) bool

CanNilValue reports whether the given value can be nil.

It returns true for kinds that can have nil values, such as channels, functions, interfaces, maps, pointers, slices, and unsafe pointers. Otherwise, it returns false. If the input has no valid type information, it returns false.

func Deref

func Deref(t reflect.Type) reflect.Type

Deref returns the basic type of t

func Indirect

func Indirect(v any) reflect.Value

Indirect recursively dereferences a value until it reaches a concrete value that is either not a pointer or interface, or is a named type.

It accepts either a `reflect.Value` or any Go value. If the input is invalid (e.g., nil), it returns reflectx.InvalidValue.

This function differs from reflect.Indirect in that it recursively dereferences anonymous (unnamed) pointer and interface types until a named type or base value is encountered. This is useful when working with nested values such as interface{} holding a pointer to a pointer, etc.

It safely handles nil pointers and interfaces by returning InvalidValue instead of panicking.

func IndirectNew

func IndirectNew(v any) reflect.Value

IndirectNew returns the indirect value of v this function is safe and WILL NOT trigger panic. if the input is invalid, InvalidValue returns. validation of return is recommended.

func IsBytes

func IsBytes(v any) bool

func IsFloat

func IsFloat(v any) bool

func IsInteger

func IsInteger(v any) bool

func IsNumeric

func IsNumeric(v any) bool

IsNumeric reports whether the value v is of a numeric type. This includes all integer, unsigned integer, float, and complex number types.

func IsZero

func IsZero(v any) bool

IsZero checks whether the given value is zero or its underlying value is zero

If the value implements the ZeroChecker interface, IsZero will use its IsZero method to determine zero-ness. Special handling is provided for slices, maps, strings, and channels, which are considered zero if their length is zero.

func KindOf added in v0.1.1

func KindOf(v any) reflect.Kind

KindOf returns the kind of the given value.

It accepts inputs of type reflect.Value, reflect.Type, or any Go value, and returns the underlying kind accordingly.

func MustType added in v0.1.1

func MustType[T any](v any) T

func New

func New(t reflect.Type) reflect.Value

New a `reflect.Value` with reflect.Type not like reflect.New, but new all level pointer ref

func NewElem

func NewElem(t reflect.Type) reflect.Value

NewElem new the indirect type of t

func Typename

func Typename(rt reflect.Type) string

Typename returns the full type name of rt

Types

type Flag added in v0.1.0

type Flag struct {
	// contains filtered or unexported fields
}

Flag parsed tag element eg: `db:"f_column,default='0',width=10,precision=4,primary"` the result is

{
  key:     "db",
  value:   "f_column,default='0',width=12,precision=4,primary",
  name:    "f_column"
  options: {
    "default":   '0',
    "width":     12,
    "precision": 4,
    "primary":   nil,
  }
}

func (*Flag) Key added in v0.1.1

func (f *Flag) Key() string

func (*Flag) Name added in v0.1.0

func (f *Flag) Name() string

func (*Flag) Option added in v0.1.1

func (f *Flag) Option(key string) *Option

func (*Flag) OptionLen added in v0.1.1

func (f *Flag) OptionLen() int

func (*Flag) QuotedValue added in v0.1.1

func (f *Flag) QuotedValue() string

func (*Flag) String added in v0.1.1

func (f *Flag) String() string

func (*Flag) UnquotedValue added in v0.1.1

func (f *Flag) UnquotedValue() string

func (*Flag) Value added in v0.1.1

func (f *Flag) Value() string

type Option added in v0.1.1

type Option struct {
	// contains filtered or unexported fields
}

func NewOption added in v0.1.1

func NewOption(key string, val string, offset int) *Option

func (*Option) IsZero added in v0.1.1

func (o *Option) IsZero() bool

func (*Option) Key added in v0.1.1

func (o *Option) Key() string

func (*Option) RawValue added in v0.1.1

func (o *Option) RawValue() []byte

func (*Option) String added in v0.1.1

func (o *Option) String() string

func (*Option) Value added in v0.1.1

func (o *Option) Value() string

type Tag added in v0.1.1

type Tag map[string]*Flag

func ParseTag added in v0.1.1

func ParseTag(tag reflect.StructTag) Tag

ParseTag parses a struct tag into a map of flag keys and values. Each value is further parsed into a flag name and its options. Control characters are allowed only in option values. Flag keys, flag names, and option names may contain only letters, digits, and underscores. Other characters in option values must be wrapped in single quotes.

func (Tag) Get added in v0.1.1

func (t Tag) Get(key string) *Flag

type ZeroChecker added in v0.1.0

type ZeroChecker interface {
	IsZero() bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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