reflect

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	InvalidOptionsFlag               = errors.New("Invalid optionsFlag")
	OPTIONS_FLAG       []optionsFlag = []optionsFlag{
		followPntrs,
		followInterfaces,
		recurseStructs,
		includeMapVals,
		includeSliceVals,
		includeArrayVals,
		unknownOptionsFlag,
	}
)
View Source
var InAddressableField = errors.New("The supplied value or field is inaddressable.")
View Source
var (
	InaddressableMapErr = customerr.Wrap(
		InAddressableField, "Maps are not addressable.",
	)
)
View Source
var (
	InaddressableValueErr = errors.New("Inaddressable value")
)

Functions

func ArrayElemInfo

func ArrayElemInfo[T any, A reflect.Value | *T](a A, keepVal bool) iter.Iter[ValInfo]

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

func ArrayElemKind[T any, A reflect.Value | *T](a A) (reflect.Kind, error)

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

func ArrayElemPntrs[T any, A reflect.Value | *T](a A) iter.Iter[any]

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

func ArrayElemType[T any, A reflect.Value | *T](a A) (reflect.Type, error)

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

func ArrayElemVals[T any, A reflect.Value | *T](a A) iter.Iter[any]

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

func CanBeNil(v reflect.Value) bool

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 GetErrorFromReflectValue

func GetErrorFromReflectValue(in *reflect.Value) error

func GetStructName

func GetStructName[T any, S reflect.Value | *T](s S) (string, error)

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

func IsArrayVal[T any, A reflect.Value | *T](a A) bool

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

func IsMapVal[T any, M reflect.Value | *T](m M) bool

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

func IsSliceVal[T any, S reflect.Value | *T](s S) bool

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

func IsStructVal[T any, S reflect.Value | *T](s S) bool

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

func MapElemInfo[T any, M reflect.Value | *T](
	m M,
	keepVal bool,
) iter.Iter[KeyValInfo]

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

func MapElemKeyInfo[T any, M reflect.Value | *T](
	m M,
	keepVal bool,
) iter.Iter[ValInfo]

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

func MapElemKeys[T any, M reflect.Value | *T](m M) iter.Iter[any]

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

func MapElemValInfo[T any, M reflect.Value | *T](
	m M,
	keepVal bool,
) iter.Iter[ValInfo]

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

func MapElemVals[T any, M reflect.Value | *T](m M) iter.Iter[any]

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

func MapElems[T any, M reflect.Value | *T](m M) iter.Iter[KeyValue]

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

func MapKeyKind[T any, M reflect.Value | *T](m M) (reflect.Kind, error)

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

func MapKeyType[T any, M reflect.Value | *T](m M) (reflect.Type, error)

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

func MapValKind[T any, M reflect.Value | *T](m M) (reflect.Kind, error)

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

func MapValType[T any, M reflect.Value | *T](m M) (reflect.Type, error)

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

func RecursiveArrayElemInfo[T any, A reflect.Value | *T](
	a A,
	keepVal bool,
) iter.Iter[ValInfo]

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

func RecursiveMapElemInfo[T any, M reflect.Value | *T](
	m M,
	keepVal bool,
) iter.Iter[KeyValInfo]

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

func RecursiveSliceElemInfo[T any, S reflect.Value | *T](
	s S,
	keepVal bool,
) iter.Iter[ValInfo]

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

func SliceElemInfo[T any, A reflect.Value | *T](a A, keepVal bool) iter.Iter[ValInfo]

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

func SliceElemKind[T any, A reflect.Value | *T](a A) (reflect.Kind, error)

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

func SliceElemPntrs[T any, A reflect.Value | *T](a A) iter.Iter[any]

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

func SliceElemType[T any, A reflect.Value | *T](a A) (reflect.Type, error)

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

func SliceElemVals[T any, S reflect.Value | *T](a S) iter.Iter[any]

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

func StructFieldInfo[T any, S reflect.Value | *T](
	s S,
	keepVal bool,
) iter.Iter[FieldInfo]

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

func StructFieldKinds[T any, S reflect.Value | *T](s S) iter.Iter[reflect.Kind]

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

func StructFieldNames[T any, S reflect.Value | *T](s S) iter.Iter[string]

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

func StructFieldPntrs[T any, S reflect.Value | *T](s S) iter.Iter[any]

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

func StructFieldTags[T any, S reflect.Value | *T](
	s S,
) iter.Iter[reflect.StructTag]

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

func StructFieldTypes[T any, S reflect.Value | *T](s S) iter.Iter[reflect.Type]

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

func StructFieldVals[T any, S reflect.Value | *T](s S) iter.Iter[any]

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

func StructHash[T any, S reflect.Value | *T](
	s S,
	opts *structHashOpts,
) (hash.Hash, error)

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:

  1. 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.

  1. Only exported fields are used. A struct with no exported fields will have

a hash of 0.

  1. nil values do not contribute to the hash value. A value of 0 does.
  2. Arrays, slices, and maps lengths will always be included in the

calculated hash.

  1. 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.

  1. 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

type KeyValInfo basic.Pair[ValInfo, ValInfo]

This type has fields that contain all the possible relevant information about a maps key value pair.

type KeyValue

type KeyValue basic.Pair[any, any]

A simple type to hold key value pairs from a map.

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

func NewValInfo(v reflect.Value, keepVal bool, addressableErr error) ValInfo

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.

Jump to

Keyboard shortcuts

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