reflect

package
v0.19.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(v reflect.Value, visitor Visitor, opts ...WalkOption) error

Walk traverses a reflect.Value recursively, calling the visitor for each value encountered.

Types

type DerefPointer

type DerefPointer func(reflect.Value) reflect.Value

DerefPointer represents a pointer dereference step in a WalkPath, implemented as a transformation function.

func (DerefPointer) IsNil

func (p DerefPointer) IsNil() bool

func (DerefPointer) StepInto

func (p DerefPointer) StepInto(other reflect.Value) reflect.Value

StepInto applies the pointer dereference transformation to the value.

type MapKey

type MapKey struct {
	reflect.Value
}

MapKey represents a map key step in a WalkPath.

func (MapKey) Compare

func (k MapKey) Compare(other MapKey) int

Compare returns the lexicographic comparison of this key's name with another.

func (MapKey) EmptyContainer

func (k MapKey) EmptyContainer() bool

EmptyContainer tells if WalkPath.WalkMap is running in an empty container with VisitEmptyContainers.

func (MapKey) Name

func (k MapKey) Name() string

Name returns the string representation of this map key (similar to StructField.Name).

func (MapKey) StepInto

func (k MapKey) StepInto(other reflect.Value) reflect.Value

StepInto retrieves the map value at this key, or returns a zero element for invalid keys.

type SliceIndex

type SliceIndex int

SliceIndex represents a slice/array index step in a WalkPath. Negative values represent zero elements.

func (SliceIndex) EmptyContainer

func (s SliceIndex) EmptyContainer() bool

EmptyContainer tells if WalkPath.WalkSlice is running in an empty container with VisitEmptyContainers.

func (SliceIndex) StepInto

func (s SliceIndex) StepInto(other reflect.Value) reflect.Value

StepInto retrieves the element at this index, or returns a zero element for negative indices.

type StructField

type StructField struct {
	reflect.StructField
	// contains filtered or unexported fields
}

StructField represents a struct field step in a WalkPath.

func (StructField) Compare

func (f StructField) Compare(other StructField) int

Compare returns the lexicographic comparison of this field's name with another.

func (StructField) StepInto

func (f StructField) StepInto(other reflect.Value) reflect.Value

StepInto retrieves the struct field value at this field's index.

type Visitor

type Visitor func(path WalkPath, v reflect.Value) error

Visitor is provided to Walk as a callback function. Descending further from this path on can be stopped prematurely by returning the error from WalkPath.Stop.

type WalkOption

type WalkOption func(*walkOpts)

WalkOption configures Walk behavior.

func VisitEmbeddedNilStructs

func VisitEmbeddedNilStructs() WalkOption

VisitEmbeddedNilStructs enables visiting fields of nil embedded struct pointers using zero values.

func VisitEmptyContainers

func VisitEmptyContainers() WalkOption

VisitEmptyContainers enables visiting empty slices, arrays, and maps with a synthetic zero-element step.

func WithRoot

func WithRoot(root WalkPath) WalkOption

WithRoot sets the initial path for walking.

type WalkPath

type WalkPath []WalkPathStep

WalkPath represents a path through a nested data structure.

func (WalkPath) IsRoot

func (p WalkPath) IsRoot() bool

IsRoot returns true if this path is empty (represents the root).

func (WalkPath) Join

func (p WalkPath) Join(others ...WalkPath) (joined WalkPath)

Join concatenates this path with other paths, returning a new combined path.

func (WalkPath) Stop

func (p WalkPath) Stop() error

Stop returns an error that signals the walk to stop descending further.

func (WalkPath) String

func (p WalkPath) String() (result string)

String returns a human-readable representation of the path.

func (WalkPath) TryTraverse

func (p WalkPath) TryTraverse(other any) (traversed reflect.Value, err error)

TryTraverse attempts to apply this path to another value, returning the traversed value or an error.

func (WalkPath) WalkMap

func (p WalkPath) WalkMap(v reflect.Value, visitor WithMapKeyVisitor, opts ...WalkOption) error

WalkMap walks through map key-value pairs, providing the key to the visitor using MapKey.StepInto.

func (WalkPath) WalkPointer

func (p WalkPath) WalkPointer(v reflect.Value, visitor Visitor, opts ...WalkOption) error

WalkPointer walks through pointer and interface values, dereferencing them if non-nil, otherwise falling back to the zero value pointed to, see DerefPointer.StepInto.

func (WalkPath) WalkSlice

func (p WalkPath) WalkSlice(v reflect.Value, visitor WithSliceIndexVisitor, opts ...WalkOption) error

WalkSlice walks through array and slice elements using SliceIndex.StepInto.

func (WalkPath) WalkStruct

func (p WalkPath) WalkStruct(v reflect.Value, visitor WithStructFieldVisitor, opts ...WalkOption) error

WalkStruct walks through exported struct fields, providing field metadata to the visitor using StructField.StepInto.

func (WalkPath) WrapError

func (p WalkPath) WrapError(err error) error

type WalkPathStep

type WalkPathStep interface {
	// StepInto applies this step to a value, returning the descended value.
	StepInto(other reflect.Value) reflect.Value
}

WalkPathStep represents a single step in a WalkPath and knows how to descend into values.

type WithMapKeyVisitor

type WithMapKeyVisitor = withVisitor[MapKey]

WithMapKeyVisitor is a visitor that receives map key metadata.

type WithSliceIndexVisitor

type WithSliceIndexVisitor = withVisitor[SliceIndex]

WithSliceIndexVisitor is a visitor that receives slice index metadata.

type WithStructFieldVisitor

type WithStructFieldVisitor = withVisitor[StructField]

WithStructFieldVisitor is a visitor that receives struct field metadata.

Jump to

Keyboard shortcuts

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