reflectionhelpers

package
v0.0.0-...-0f1b10b Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2024 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IterateReferencedTypes

func IterateReferencedTypes(start reflect.Type, action ReferencedTypesIterationAction)

IterateReferencedTypes recursively iterates over all referenced types from an initial reflect.Type. The ReferencedTypesIterationAction argument is called for each referenced type. This argument can also control whether the iteration goes deeper into a type or not.

Types

type ReferencedTypesIterationAction

type ReferencedTypesIterationAction func(path TypePath, stack []reflect.Type) bool

ReferencedTypesIterationAction represents an action to be taken on each iteration of IterateReferencedTypes. This function should return true to go deeper into the current type's referenced types, or false to look no deeper at the current type's referenced types.

NOTE: The TypePath argument this function receives is passed by reference. If you intend to save this value for use after this function returns, you MUST call the Clone() method to keep a copy of the TypePath as you currently see it.

type TypePath

type TypePath []TypeSegment

TypePath represents a path of referenced types starting from an origin reflect.Type. Note the origin reflect.Type is not contained in TypePath.

func (TypePath) AddField

func (p TypePath) AddField(fieldName string) TypePath

AddField adds a named field segment to a TypePath. The modification is done using append, so this action may mutate the input TypePath.

NOTE: There is no guarantee that this constructed TypePath is valid. Use ResolveType to verify that after construction.

func (TypePath) AddMapKey

func (p TypePath) AddMapKey() TypePath

AddMapKey adds a map key segment to a TypePath. The modification is done using append, so this action may mutate the input TypePath.

NOTE: There is no guarantee that this constructed TypePath is valid. Use ResolveType to verify that after construction.

func (TypePath) AddValue

func (p TypePath) AddValue() TypePath

AddValue adds a map, array, or slice value segment to a TypePath. The modification is done using append, so this action may mutate the input TypePath.

NOTE: There is no guarantee that this constructed TypePath is valid. Use ResolveType to verify that after construction.

func (TypePath) Clone

func (p TypePath) Clone() TypePath

Clone creates a deep copy of a TypePath

func (TypePath) Equals

func (p TypePath) Equals(other TypePath) bool

Equals returns true if and only if the input TypePath has the exact same segments as this TypePath.

func (TypePath) ResolveType

func (p TypePath) ResolveType(base reflect.Type) reflect.Type

ResolveType follows the TypePath to its end and returns the reflect.Type of the last referenced type. The initial type, base, must be provided, since TypePath is a relative path. If the TypePath represents a chain of type references that is not valid, this will panic.

func (TypePath) ResolveValues

func (p TypePath) ResolveValues(base reflect.Value) []reflect.Value

ResolveValues follows the TypePath to its end and returns a slice of all the values at that location. The initial value, base, must have the type of the origin reflect.Type this TypePath was made for. If the TypePath represents a chain of type references that is not valid, this will panic.

This function returns a slice of values because some segments may map to many values. Field segments always map to a single value, but map key and (map, slice, or array) value segments may map to zero or more values, depending on the value of the input argument.

func (TypePath) String

func (p TypePath) String() string

type TypeSegment

type TypeSegment struct {
	Kind TypeSegmentKind
	// If Kind is FieldSegmentKind, then FieldName contains the name of the referenced field.
	FieldName string
}

TypeSegment represents a single segment in a TypePath. This segment is a single reference from one reflect.Type to another reflect.Type.

func (TypeSegment) String

func (s TypeSegment) String() string

type TypeSegmentKind

type TypeSegmentKind int

TypeSegmentKind is a enum for the types of TypeSegment

const (
	// FieldSegmentKind represents a referenced field type on a Go type
	FieldSegmentKind TypeSegmentKind = iota
	// MapKeySegmentKind represents the key type of a Go map
	MapKeySegmentKind
	// ValueSegmentKind represents the value type of a Go map, array, or slice
	ValueSegmentKind
)

Jump to

Keyboard shortcuts

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