Documentation
¶
Overview ¶
Package reflectutil provides utility functions for the reflect package.
Index ¶
- func Compare(a, b reflect.Value) int
- func ConvertValueCanInterface(v reflect.Value) (reflect.Value, bool)
- func GetBaseType(typ reflect.Type) reflect.Typedeprecated
- func GetUnderlyingType(typ reflect.Type) reflect.Type
- func TryValueInterface(v reflect.Value) (any, bool)
- func TryValueInterfaceFor[T any](v reflect.Value) (T, bool)
- func TypeFullName(typ reflect.Type) string
- func TypeFullNameFor[T any]() string
- func ValueInterfaceFor[T any](v reflect.Value) T
- type CompareFunc
- type ImplementsCache
- type MapEntries
- type MapEntry
- type Methods
- type StructFields
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶ added in v0.7.7
Compare compares two values of the same reflect.Type.
func ConvertValueCanInterface ¶ added in v0.6.0
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
GetBaseType is an alias for GetUnderlyingType.
Deprecated: use GetUnderlyingType instead.
func GetUnderlyingType ¶ added in v0.16.8
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
TryValueInterface calls ConvertValueCanInterface and reflect.Value.Interface.
func TryValueInterfaceFor ¶ added in v0.6.0
TryValueInterfaceFor calls ConvertValueCanInterface and ValueInterfaceFor.
func TypeFullName ¶
TypeFullName returns the full name of the type.
It contains the full package path if the type is defined in a package.
func TypeFullNameFor ¶
TypeFullNameFor returns the full name of type parameter.
func ValueInterfaceFor ¶ added in v0.6.0
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
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 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
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
All returns an iter.Seq2 that iterates over all [reflect.Method]s in the Methods.
func (Methods) Get ¶ added in v0.16.0
Get returns the reflect.Method at the given index.
func (Methods) GetByName ¶ added in v0.16.3
GetByName returns the reflect.Method with the given name.
func (Methods) Len ¶ added in v0.16.0
Len returns the number of reflect.Method in the Methods.
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
func (fs StructFields) All() iter.Seq2[int, reflect.StructField]
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.