Documentation
¶
Overview ¶
This package defines many helper functions that make it easier to get values from the standard libraries reflect package. To do this the iter package is used frequently.
Index ¶
- Variables
- func ArrayElemInfo[T any, A reflect.Value | *T](a A, keepVal bool) iter.Iter[ValInfo]
- func ArrayElemKind[T any, A reflect.Value | *T](a A) (reflect.Kind, error)
- func ArrayElemPntrs[T any, A reflect.Value | *T](a A) iter.Iter[any]
- func ArrayElemType[T any, A reflect.Value | *T](a A) (reflect.Type, error)
- func ArrayElemVals[T any, A reflect.Value | *T](a A) iter.Iter[any]
- func CanBeNil(v reflect.Value) bool
- func GetErrorFromReflectValue(in *reflect.Value) error
- func GetStructName[T any, S reflect.Value | *T](s S) (string, error)
- func IsArrayVal[T any, A reflect.Value | *T](a A) bool
- func IsMapVal[T any, M reflect.Value | *T](m M) bool
- func IsSliceVal[T any, S reflect.Value | *T](s S) bool
- func IsStructVal[T any, S reflect.Value | *T](s S) bool
- func MapElemInfo[T any, M reflect.Value | *T](m M, keepVal bool) iter.Iter[KeyValInfo]
- func MapElemKeyInfo[T any, M reflect.Value | *T](m M, keepVal bool) iter.Iter[ValInfo]
- func MapElemKeys[T any, M reflect.Value | *T](m M) iter.Iter[any]
- func MapElemValInfo[T any, M reflect.Value | *T](m M, keepVal bool) iter.Iter[ValInfo]
- func MapElemVals[T any, M reflect.Value | *T](m M) iter.Iter[any]
- func MapElems[T any, M reflect.Value | *T](m M) iter.Iter[KeyValue]
- func MapKeyKind[T any, M reflect.Value | *T](m M) (reflect.Kind, error)
- func MapKeyType[T any, M reflect.Value | *T](m M) (reflect.Type, error)
- func MapValKind[T any, M reflect.Value | *T](m M) (reflect.Kind, error)
- func MapValType[T any, M reflect.Value | *T](m M) (reflect.Type, error)
- func NewOptionsFlag() optionsFlag
- func NewStructHashOpts() *structHashOpts
- func RecursiveArrayElemInfo[T any, A reflect.Value | *T](a A, keepVal bool) iter.Iter[ValInfo]
- func RecursiveMapElemInfo[T any, M reflect.Value | *T](m M, keepVal bool) iter.Iter[KeyValInfo]
- func RecursiveSliceElemInfo[T any, S reflect.Value | *T](s S, keepVal bool) iter.Iter[ValInfo]
- func RecursiveStructFieldInfo[T any, S reflect.Value | *T](s S, keepVal bool) iter.Iter[FieldInfo]
- func SliceElemInfo[T any, A reflect.Value | *T](a A, keepVal bool) iter.Iter[ValInfo]
- func SliceElemKind[T any, A reflect.Value | *T](a A) (reflect.Kind, error)
- func SliceElemPntrs[T any, A reflect.Value | *T](a A) iter.Iter[any]
- func SliceElemType[T any, A reflect.Value | *T](a A) (reflect.Type, error)
- func SliceElemVals[T any, S reflect.Value | *T](a S) iter.Iter[any]
- func StructFieldInfo[T any, S reflect.Value | *T](s S, keepVal bool) iter.Iter[FieldInfo]
- func StructFieldKinds[T any, S reflect.Value | *T](s S) iter.Iter[reflect.Kind]
- func StructFieldNames[T any, S reflect.Value | *T](s S) iter.Iter[string]
- func StructFieldPntrs[T any, S reflect.Value | *T](s S) iter.Iter[any]
- func StructFieldTags[T any, S reflect.Value | *T](s S) iter.Iter[reflect.StructTag]
- func StructFieldTypes[T any, S reflect.Value | *T](s S) iter.Iter[reflect.Type]
- func StructFieldVals[T any, S reflect.Value | *T](s S) iter.Iter[any]
- func StructHash[T any, S reflect.Value | *T](s S, opts *structHashOpts) (hash.Hash, error)
- type FieldInfo
- type KeyValInfo
- type KeyValue
- type ValInfo
Constants ¶
This section is empty.
Variables ¶
var ( InvalidOptionsFlag = errors.New("Invalid optionsFlag") OPTIONS_FLAG []optionsFlag = []optionsFlag{ followPntrs, followInterfaces, recurseStructs, includeMapVals, includeSliceVals, includeArrayVals, unknownOptionsFlag, } )
var InAddressableField = errors.New("The supplied value or field is inaddressable.")
var ( InaddressableMapErr = customerr.Wrap( InAddressableField, "Maps are not addressable.", ) )
var (
InaddressableValueErr = errors.New("Inaddressable value")
)
Functions ¶
func ArrayElemInfo ¶
Returns an iterator that provides the value information for each element in the supplied array if an array is supplied as an argument, returns an error otherwise. As a special case if a reflect.Value is passed to this function then the iterator will iterate over the elements in the array it either contains or points to. Note that the field info Pntr field may not be able to be populated if the passed in value is not addressable. If you need the pointers to the array elements then make sure you either pass a pointer to an array or a reflect.Value that contains a pointer to an array.
func ArrayElemKind ¶
Returns the kind of the elements in the array that is passed to the function. If an array is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the kind of the elements in the array it either contains or points to will be returned.
func ArrayElemPntrs ¶
Returns an iterator that will iterate over the array elements returning a pointer at each index if an array is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return an iterator over the array it contains or an iterator over the array it points to if the reflect.Value contains a pointer to an array. Note that this function requires any reflect.Value handed to it to be addressable. This means that in most scenarios any reflect.Value passed to this function will need to contain a pointer to an array.
func ArrayElemType ¶
Returns the type of the elements in the array that is passed to the function. If an array is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the type of the elements in the array it either contains or points to will be returned.
func ArrayElemVals ¶
Returns an iterator that will iterate over the array elements returning the value at each index if an array is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return an iterator over the array it contains or an iterator over the array it points to if the reflect.Value contains a pointer to an array.
func CanBeNil ¶
Checks if the supplied value can be nil. Can be used as a safe guard to check if the stdlib's reflect.Value.IsNil function will panic or not.
func GetStructName ¶
Retrieves the struct name if a struct is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the name of the struct it contains or the name of the struct that it points to if the reflect.Value contains a pointer to a struct.
func IsArrayVal ¶
Returns true if the supplied value is a pointer to an array. As a special case, if a reflect.Value is passed to this function it will return true if that reflect value either contains an array or contains a pointer to an array.
func IsMapVal ¶
Returns true if the supplied value is a pointer to a map. As a special case, if a reflect.Value is passed to this function it will return true if that reflect value either contains a map or contains a pointer to a map.
func IsSliceVal ¶
Returns true if the supplied value is a pointer to a slice. As a special case, if a reflect.Value is passed to this function it will return true if that reflect value either contains a slice or contains a pointer to a slice.
func IsStructVal ¶
Returns true if the supplied value is a pointer to a struct. As a special case, if a reflect.Value is passed to this function it will return true if that reflect value either contains a struct or contains a pointer to a struct.
func MapElemInfo ¶
Returns an iterator that provides the key value information for each element in the supplied map if a map is supplied as an argument, returns an error otherwise. As a special case if a reflect.Value is passed to this function then the iterator will iterate over the elements in the map it either contains or points to.
func MapElemKeyInfo ¶
Returns an iterator that provides the key information for each element in the supplied map if a map is supplied as an argument, returns an error otherwise. As a special case if a reflect.Value is passed to this function then the iterator will iterate over the key elements in the map it either contains or points to.
func MapElemKeys ¶
Returns an iterator that will iterate over the supplied maps keys if a map is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return an iterator over the the map keys it contains or an iterator over the map keys it points to if the reflect.Value contains a pointer to a map.
func MapElemValInfo ¶
Returns an iterator that provides the value information for each element in the supplied map if a map is supplied as an argument, returns an error otherwise. As a special case if a reflect.Value is passed to this function then the iterator will iterate over the value elements in the map it either contains or points to.
func MapElemVals ¶
Returns an iterator that will iterate over the supplied maps values if a map is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return an iterator over the the map values it contains or an iterator over the map values it points to if the reflect.Value contains a pointer to a map.
func MapElems ¶
Returns an iterator that will iterate over the supplied maps key value pairs if a map is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return an iterator over the the map key value pairs it contains or an iterator over the map values it points to if the reflect.Value contains a pointer to a map.
func MapKeyKind ¶
Returns the kind of the keys in the map that is passed to the function. If a map is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the kind of the keys in the map it either contains or points to will be returned.
func MapKeyType ¶
Returns the type of the keys in the map that is passed to the function. If a map is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the type of the keys in the map it either contains or points to will be returned.
func MapValKind ¶
Returns the kind of the values in the map that is passed to the function. If a map is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the kind of the values in the map it either contains or points to will be returned.
func MapValType ¶
Returns the type of the values in the map that is passed to the function. If a map is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the type of the values in the map it either contains or points to will be returned.
func NewOptionsFlag ¶
func NewOptionsFlag() optionsFlag
func NewStructHashOpts ¶
func NewStructHashOpts() *structHashOpts
Returns a new structHashOpts struct initialized with the default values.
func RecursiveArrayElemInfo ¶
Returns an iterator that recursively provides the array elements val info if an array is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the recursively found val info of the arrays it contains or the recursively found val info of the array that it points to if the reflect.Value contains a pointer to an array. Any field that is an array value will be recursed on, pointers to arrays will not be recursed on. Note that in order to recursively access the fields the array needs to be addressable, as the fields that are arrays will be referenced through pointers. This is done to prevent excess memory use that would be caused by copying all sub-arrays by value.
func RecursiveMapElemInfo ¶
Returns an iterator that recursively provides the key value info if a map is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the recursively found val info of the maps it contains or the recursively found val info of the map that it points to if the reflect.Value contains a pointer to a map. Any value that is a map value will be recursed on, pointers to maps will not be recursed on.
func RecursiveSliceElemInfo ¶
Returns an iterator that recursively provides the slice elements val info if a slice is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the recursively found val info of the slices it contains or the recursively found val info of the slice that it points to if the reflect.Value contains a pointer to a slice. Any field that is a slice value will be recursed on, pointers to slices will not be recursed on.
func RecursiveStructFieldInfo ¶
func RecursiveStructFieldInfo[T any, S reflect.Value | *T]( s S, keepVal bool, ) iter.Iter[FieldInfo]
Returns an iterator that recursively provides the struct field info if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the recursively found field info of the struct it contains or the recursively found field info of the struct that it points to if the reflect.Value contains a pointer to a struct. Any field that is a struct value will be recursed on, pointers to structs will not be recursed on. Note that in order to recursively access the fields the struct needs to be addressable, as the fields that are structs will be referenced through pointers. This is done to prevent excess memory use that would be caused by copying all sub-structs by value.
func SliceElemInfo ¶
Returns an iterator that provides the value information for each element in the supplied slice if a slice is supplied as an argument, returns an error otherwise. As a special case if a reflect.Value is passed to this function then the iterator will iterate over the elements in the slice it either contains or points to.
func SliceElemKind ¶
Returns the kind of the elements in the slice that is passed to the function. If a slice is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the kind of the elements in the slice it either contains or points to will be returned.
func SliceElemPntrs ¶
Returns an iterator that will iterate over the slice elements returning a pointer at each index if a slice is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return an iterator over the slice it contains or an iterator over the slice it points to if the reflect.Value contains a pointer to a slice.
func SliceElemType ¶
Returns the type of the elements in the slice that is passed to the function. If a slice is not passed to the function an error will be returned. As a special case if a reflect.Value is passed to this function then the type of the elements in the slice it either contains or points to will be returned.
func SliceElemVals ¶
Returns an iterator that will iterate over the slice elements returning the value at each index if a slice is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return an iterator over the slice it contains or an iterator over the slice it points to if the reflect.Value contains a pointer to a slice.
func StructFieldInfo ¶
Returns an iterator that provides the struct field info if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the field info of the struct it contains or the field info of the struct that it points to if the reflect.Value contains a pointer to a struct. Note that the field info Pntr field may not be able to be populated if the passed in value is not addressable. If you need the pointers to the struct fields then make sure you either pass a pointer to a struct or a reflect.Value that contains a pointer to a struct.
func StructFieldKinds ¶
Returns an iterator that provides the struct field kinds if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the field kinds of the struct it contains or the field kinds of the struct that it points to if the reflect.Value contains a pointer to a struct.
func StructFieldNames ¶
Returns an iterator that provides the struct field names if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the field names of the struct it contains or the field names of the struct that it points to if the reflect.Value contains a pointer to a struct.
func StructFieldPntrs ¶
Returns an iterator that provides pointers to the struct field values if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return pointers to the fields of the struct it contains or pointers to the fields of the struct that it points to if the reflect.Value contains a pointer to a struct. Note that this function requires any reflect.Value handed to it to be addressable. This means that in most scenarios any reflect.Value passed to this function will need to contain a pointer to a struct.
func StructFieldTags ¶
Returns an iterator that provides the struct field tags if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the field tags of the struct it contains or the field tags of the struct that it points to if the reflect.Value contains a pointer to a struct.
func StructFieldTypes ¶
Returns an iterator that provides the struct field types if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the field types of the struct it contains or the field types of the struct that it points to if the reflect.Value contains a pointer to a struct.
func StructFieldVals ¶
Returns an iterator that provides the struct field values if a struct is is supplied as an argument, returns an error otherwise. As a special case, if a reflect.Value is passed to this function it will return the field values of the struct it contains or the field values of the struct that it points to if the reflect.Value contains a pointer to a struct.
func StructHash ¶
Returns a hash for the supplied struct. The hash is generated by reflexively iterating over exported structs fields. There are a couple rules that dictate the hash value that is generated:
- The widgets in the widgets package are used for generating hashes when
the underlying type is an exact match. Custom types of the built in types will not have a hash value generated.
- Only exported fields are used. A struct with no exported fields will have
a hash of 0.
- nil values do not contribute to the hash value. A value of 0 does.
- Arrays, slices, and maps lengths will always be included in the
calculated hash.
- The memory address of a slices data pointer will be included in the
calculated hash only if the underlying values are flagged to not be included.
- Channels and functions will be ignored.
The opts struct determines behaviors about which struct fields are used and how they are used.
Types ¶
type FieldInfo ¶
type FieldInfo struct {
ValInfo
// The name of the field.
Name string
// The tag associated with the struct field.
Tag reflect.StructTag
}
This struct has fields that contain all the possible relevant information about a structs field.
type KeyValInfo ¶
This type has fields that contain all the possible relevant information about a maps key value pair.
type ValInfo ¶
type ValInfo struct {
Type reflect.Type
Kind reflect.Kind
// The underlying value. This is a copy of the value, not the
// original value. To access the original value use the Pntr field.
Val func() (any, bool)
// Returns a pointer to the value, if possible. An error will be returned if
// the value is not addressable.
Pntr func() (any, error)
// contains filtered or unexported fields
}
func NewValInfo ¶
Makes a new val info struct with the supplied reflect value. Set keepVal to true to make a copy of the underlying reflect value, false to not make a copy. The addressableErr will be the error that is returned when trying to access get the address of a non-addressable reflect value.