reflect

package
v0.0.0-...-cda96b6 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2025 License: BSD-3-Clause Imports: 6 Imported by: 4

Documentation

Index

Constants

View Source
const (
	FuzzyFloats = 1 << iota
	FuzzyInts
	FuzzyStrings
	Fuzzy = FuzzyFloats | FuzzyInts | FuzzyStrings
)

Variables

This section is empty.

Functions

func Copy

func Copy(dst, src any) error

Copy sets all fields in dst to any fields in src with the same name. It is a convenience and small optimization over using Set(). WIP: Currently just a simple struct-to-struct top-level copy. Additional details will be added as needed.

func Get

func Get(s any, h GetHandler)

Get iterates the fields in a struct, sending the results to a handler.

func GetAsMap

func GetAsMap(s any, h GetHandler, opts *MapOpts) map[string]any

GetAsMap iterates the fields in a struct, adding the results to a map based on a handler and returning the map. The handler can be a chain. The final element of the chain must be a Mapper, but you don't need to include it: If the chain doesn't end in a Mapper, then one will be added and provided the MapOpts.

func GetAsSlice

func GetAsSlice(s any, h GetHandler, opts *SliceOpts) []any

GetAsSlice iterates the fields in a struct, adding the results to a slice based on a handler and returning the slice. The handler can be a chain. The final element of the chain must be a Slicer, but you don't need to include it: If the chain doesn't end in a Slicer, then one will be added and provided the SliceOpts.

func GetFloat64

func GetFloat64(a any) (float64, error)

func Set

func Set(r SetRequest, dst any) error

Set sets the value of each field to the supplied value.

func SetJson

func SetJson(src, dst reflect.Value) error

SetJson takes a source string and a destination of any type and unmarshals the source to the destination.

Types

type Chain

type Chain []GetHandler

func NewChain

func NewChain(items ...any) Chain

NewChain takes a list of items and converts them into handlers, answering the new handler Chain. Items can be: * A Handler. It is added directly to the chain. * An *Opts (SlicerOpts, etc.). It is converted to the associated Handler and added to the chain. * A map[string]string. It is converted to a FilterMap Handler.

func (Chain) Handle

func (c Chain) Handle(name string, value any) (string, any)

type FilterMapOpts

type FilterMapOpts struct {
	F map[string]string
	// When passthrough is true, anything not filtered is passed on unchanged.
	Passthrough bool
}

type GetHandler

type GetHandler interface {
	// Handle the pair in some fashion, returning the (potentially
	// changed, filtered etc. data). The basic system
	// is designed to work without errors; if a client needs
	// any error handling, it should track that in an internal state.
	Handle(name string, value any) (string, any)
}

GetHandler is used to get name/value pairs from a struct.

func FilterMap

func FilterMap(opts FilterMapOpts) GetHandler

func Map

func Map(opts MapOpts) GetHandler

func Slice

func Slice(opts SliceOpts) GetHandler

type MapOpts

type MapOpts struct {
}

type Mapper

type Mapper interface {
	Map() map[string]any
}

Mapper can return a map.

type SetFunc

type SetFunc func(src, dst reflect.Value) error

SetFunc handles the work of assigning the destination value to the source value.

type SetRequest

type SetRequest struct {
	FieldNames []string
	NewValues  []any
	Assigns    []SetFunc // Optional: Provide assignment func for each value. Nil will use default.
	Flags      uint8
}

func SetRequestFrom

func SetRequestFrom(m map[string]any) SetRequest

func (SetRequest) Validate

func (r SetRequest) Validate() error

type SliceOpts

type SliceOpts struct {
	Assign  string
	Combine string
}

type Slicer

type Slicer interface {
	Slice() []any
}

Slicer can return a slice.

Jump to

Keyboard shortcuts

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