reflectx

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddrByTraversal

func AddrByTraversal(base unsafe.Pointer, steps []Step) unsafe.Pointer

AddrByTraversal returns the address of a field within a struct using pre-calculated steps.

SAFETY: This function operates on raw unsafe.Pointer addresses and allocates intermediate pointer fields via reflect.NewAt().Elem().Set() to maintain GC write barrier visibility. The caller MUST ensure:

  1. 'base' is derived from a heap-allocated reflect.New() Value (not a stack local).
  2. The reflect.Value that produced 'base' (via .Pointer()) remains rooted by a runtime.KeepAlive() call AFTER the Scan that writes through these pointers.
  3. The Steps slice was produced by the same Mapper.TypeMap that analysed the struct type, guaranteeing offset correctness.

Rooting chain: caller's vp (reflect.Value) → runtime.KeepAlive(vp) after Scan → base is reachable → all interior pointers allocated here are reachable.

func Deref

func Deref(t reflect.Type) reflect.Type

Deref is Indirect for reflect.Types

func FieldByIndexes

func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value

FieldByIndexes returns a value within a struct for the given index path.

func FieldByIndexesReadOnly

func FieldByIndexesReadOnly(v reflect.Value, indexes []int) reflect.Value

FieldByIndexesReadOnly returns a value for the field given by the struct traversal for the given value.

Types

type FieldInfo

type FieldInfo struct {
	Index      []int
	Path       string
	Field      reflect.StructField
	Zero       reflect.Value
	Name       string
	Options    map[string]string
	Embedded   bool
	Children   []*FieldInfo
	Parent     *FieldInfo
	Traversal  []Step       // Legacy support/pointer resolution
	Offset     uintptr      // Linear offset relative to base struct (0 for top-level pointer embeds)
	TargetType reflect.Type // The terminal type to scan into
	IsPtrPath  bool         // True if any node in the path (e.g. parent) was accessed via pointer
}

FieldInfo is metadata for a struct field.

type Mapper

type Mapper struct {
	// contains filtered or unexported fields
}

Mapper is a general purpose mapper of names to struct fields. A Mapper behaves like most other mappers in the Go ecosystem, mapping column names to struct fields using a set of rules.

func NewMapper

func NewMapper(tagName string) *Mapper

NewMapper returns a new Mapper with a given struct tag.

func NewMapperFunc

func NewMapperFunc(tagName string, f func(string) string) *Mapper

NewMapperFunc returns a new Mapper with a given struct tag and name mapping function.

func NewMapperFuncStrict

func NewMapperFuncStrict(tagName string, f func(string) string, strict bool) *Mapper

NewMapperFuncStrict returns a new Mapper with strict tag validation enabled. When strict is true, struct tags are validated for correctness during TypeMap.

func NewMapperTagFunc

func NewMapperTagFunc(tagName string, f func(string) string, tagMapFunc func(string) string) *Mapper

NewMapperTagFunc returns a new Mapper with a given struct tag, a name mapping function and a tag mapping function. The name mapping function (f) is used to transform field names when no tag is present. The tag mapping function (tagMapFunc) is applied to tag values after parsing.

func (*Mapper) FieldByName

func (m *Mapper) FieldByName(v reflect.Value, name string) reflect.Value

FieldByName returns a FieldInfo for the given column name.

func (*Mapper) FieldMap

func (m *Mapper) FieldMap(t reflect.Type) map[string]*FieldInfo

FieldMap returns the mapper's mapping of field names to FieldInfos.

func (*Mapper) TraversalsByName

func (m *Mapper) TraversalsByName(t reflect.Type, names []string) [][]int

TraversalsByName returns a slice index-path slices for the struct type and the given names.

func (*Mapper) TraversalsByNameFunc

func (m *Mapper) TraversalsByNameFunc(t reflect.Type, names []string, fn func(int, []int) error) error

TraversalsByNameFunc traverses the mapped names and calls fn with the index of each name and the struct traversal represented by that name. Panics if the struct cannot be mapped or if fn panics.

func (*Mapper) TypeMap

func (m *Mapper) TypeMap(t reflect.Type) *StructMap

TypeMap returns a StructMap for the given reflect.Type, creating it if necessary. This is the core function that handles recursive struct flattening.

type Step

type Step struct {
	Offset  uintptr
	Type    reflect.Type // If not nil, this step is a pointer that might need allocation of this Type
	PtrType reflect.Type // Cache of reflect.PointerTo(Type) for zero-allocation write barrier Set
}

Step represents a single level of dereferencing or offset in a struct traversal.

type StructMap

type StructMap struct {
	Tree   *FieldInfo
	Index  []*FieldInfo
	Paths  map[string]*FieldInfo
	Names  map[string]*FieldInfo
	Errors []error // Populated when strict tag validation is enabled
}

StructMap represents a mapped struct, with fields flattened from embedded structs and ready for name-based lookup.

Jump to

Keyboard shortcuts

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