reflectutil

package
v0.25.2 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: MIT Imports: 10 Imported by: 5

Documentation

Overview

Package reflectutil provides utility functions for the reflect package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare added in v0.7.7

func Compare(a, b reflect.Value) int

Compare compares two values of the same reflect.Type.

func ConvertValueCanInterface added in v0.6.0

func ConvertValueCanInterface(v reflect.Value) (reflect.Value, bool)

ConvertValueCanInterface attempts to converts a reflect.Value so it can be used with reflect.Value.Interface.

The returned boolean indicates if the conversion was successful.

If the conversion was successful, the returned reflect.Value can be used with reflect.Value.Interface. If the conversion was not successful, the returned reflect.Value is the same as the input reflect.Value.

func GetBaseType deprecated added in v0.15.0

func GetBaseType(typ reflect.Type) reflect.Type

GetBaseType is an alias for GetUnderlyingType.

Deprecated: use GetUnderlyingType instead.

func GetUnderlyingType added in v0.16.8

func GetUnderlyingType(typ reflect.Type) reflect.Type

GetUnderlyingType returns the underlying type of the given type. E.g. for the type defined as `type MyType string`, it returns `string`. It returns the type itself if it's its own underlying type, or if the kind is invalid/interface/struct.

func TryValueInterface added in v0.6.0

func TryValueInterface(v reflect.Value) (any, bool)

TryValueInterface calls ConvertValueCanInterface and reflect.Value.Interface.

func TryValueInterfaceFor added in v0.6.0

func TryValueInterfaceFor[T any](v reflect.Value) (T, bool)

TryValueInterfaceFor calls ConvertValueCanInterface and ValueInterfaceFor.

func TypeFullName

func TypeFullName(typ reflect.Type) string

TypeFullName returns the full name of the type.

It contains the full package path if the type is defined in a package.

func TypeFullNameFor

func TypeFullNameFor[T any]() string

TypeFullNameFor returns the full name of type parameter.

func ValueInterfaceFor added in v0.6.0

func ValueInterfaceFor[T any](v reflect.Value) T

ValueInterfaceFor calls reflect.Value.Interface and returns the result as the specified type.

The caller must ensure that the type is correct.

Types

type CompareFunc added in v0.18.1

type CompareFunc func(a, b reflect.Value) int

CompareFunc represents a function that compares 2 reflect.Value of the same reflect.Type.

func GetCompareFunc added in v0.7.7

func GetCompareFunc(typ reflect.Type) CompareFunc

GetCompareFunc returns a function that compares two values of the given reflect.Type.

type ImplementsCache added in v0.14.0

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

ImplementsCache is a cache for checking if an interface is implemented by types.

It should be created with NewImplementsCache.

func NewImplementsCache added in v0.14.0

func NewImplementsCache(itf reflect.Type) *ImplementsCache

NewImplementsCache creates a new ImplementsCache for the given interface type.

func NewImplementsCacheFor added in v0.14.0

func NewImplementsCacheFor[T any]() *ImplementsCache

NewImplementsCacheFor creates a new ImplementsCache for the given type parameter.

func (*ImplementsCache) ImplementedBy added in v0.14.0

func (c *ImplementsCache) ImplementedBy(typ reflect.Type) bool

ImplementedBy checks if the interface is impleented by the given type.

type MapEntries added in v0.7.7

type MapEntries []MapEntry

MapEntries is a slice of MapEntry.

func GetMapEntries added in v0.7.7

func GetMapEntries(m reflect.Value) MapEntries

GetMapEntries returns the entries of a map.

func GetSortedMap added in v0.7.7

func GetSortedMap(m reflect.Value) MapEntries

GetSortedMap returns a sorted MapEntries of the given map.

func (MapEntries) Release added in v0.7.7

func (es MapEntries) Release()

Release releases the MapEntries.

It helps to reduce memory allocations.

type MapEntry added in v0.7.7

type MapEntry struct {
	Key   reflect.Value
	Value reflect.Value
}

MapEntry represents a key-value pair in a map.

type Methods added in v0.16.0

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

Methods represents a list of [reflect.Method]s of a type.

func GetMethods added in v0.16.0

func GetMethods(typ reflect.Type) Methods

GetMethods returns a Methods containing all [reflect.Method]s of the given type. If the type is nil or has no methods, it returns an empty Methods.

func (Methods) All added in v0.16.0

func (ms Methods) All() iter.Seq2[int, reflect.Method]

All returns an iter.Seq2 that iterates over all [reflect.Method]s in the Methods.

func (Methods) Get added in v0.16.0

func (ms Methods) Get(i int) reflect.Method

Get returns the reflect.Method at the given index.

func (Methods) GetByName added in v0.16.3

func (ms Methods) GetByName(name string) (reflect.Method, bool)

GetByName returns the reflect.Method with the given name.

func (Methods) Len added in v0.16.0

func (ms Methods) Len() int

Len returns the number of reflect.Method in the Methods.

func (Methods) Range added in v0.16.0

func (ms Methods) Range(yield func(int, reflect.Method) bool)

Range iterates over all [reflect.Method]s in the Methods and calls the given yield function.

type StructFields added in v0.16.0

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

StructFields represents a list of [reflect.StructField]s of a struct type.

func GetStructFields added in v0.16.0

func GetStructFields(typ reflect.Type) StructFields

GetStructFields returns a StructFields containing all [reflect.StructField]s of the given type. If the type is nil or has no fields, it returns an empty StructFields.

func (StructFields) All added in v0.16.0

All returns an iter.Seq2 that iterates over all [reflect.StructField]s in the StructFields.

func (StructFields) Get added in v0.16.0

func (fs StructFields) Get(i int) reflect.StructField

Get returns the reflect.StructField at the given index.

func (StructFields) GetByName added in v0.16.3

func (fs StructFields) GetByName(name string) (reflect.StructField, bool)

GetByName returns the reflect.StructField with the given name.

func (StructFields) Len added in v0.16.0

func (fs StructFields) Len() int

Len returns the number of [reflect.StructField]s in the StructFields.

func (StructFields) Range added in v0.16.0

func (fs StructFields) Range(yield func(int, reflect.StructField) bool)

Range iterates over all [reflect.StructField]s in the StructFields and calls the given yield function.

Jump to

Keyboard shortcuts

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