reflectwalk

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2015 License: MIT, MPL-2.0 Imports: 2 Imported by: 0

README

reflectwalk

reflectwalk is a Go library for "walking" a value in Go using reflection, in the same way a directory tree can be "walked" on the filesystem. Walking a complex structure can allow you to do manipulations on unknown structures such as those decoded from JSON.

Documentation

Overview

reflectwalk is a package that allows you to "walk" complex structures similar to how you may "walk" a filesystem: visiting every element one by one and calling callback functions allowing you to handle and manipulate those elements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Walk

func Walk(data, walker interface{}) (err error)

Walk takes an arbitrary value and an interface and traverses the value, calling callbacks on the interface if they are supported. The interface should implement one or more of the walker interfaces in this package, such as PrimitiveWalker, StructWalker, etc.

Types

type EnterExitWalker

type EnterExitWalker interface {
	Enter(Location) error
	Exit(Location) error
}

EnterExitWalker implementations are notified before and after they walk deeper into complex structures (into struct fields, into slice elements, etc.)

type Location

type Location uint
const (
	None Location = iota
	Map
	MapKey
	MapValue
	Slice
	SliceElem
	Struct
	StructField
	WalkLoc
)

func (Location) String

func (i Location) String() string

type MapWalker

type MapWalker interface {
	Map(m reflect.Value) error
	MapElem(m, k, v reflect.Value) error
}

MapWalker implementations are able to handle individual elements found within a map structure.

type PointerWalker

type PointerWalker interface {
	PointerEnter(bool) error
	PointerExit(bool) error
}

PointerWalker implementations are notified when the value they're walking is a pointer or not. Pointer is called for _every_ value whether it is a pointer or not.

type PrimitiveWalker

type PrimitiveWalker interface {
	Primitive(reflect.Value) error
}

PrimitiveWalker implementations are able to handle primitive values within complex structures. Primitive values are numbers, strings, booleans, funcs, chans.

These primitive values are often members of more complex structures (slices, maps, etc.) that are walkable by other interfaces.

type SliceWalker

type SliceWalker interface {
	Slice(reflect.Value) error
	SliceElem(int, reflect.Value) error
}

SliceWalker implementations are able to handle slice elements found within complex structures.

type StructWalker

type StructWalker interface {
	Struct(reflect.Value) error
	StructField(reflect.StructField, reflect.Value) error
}

StructWalker is an interface that has methods that are called for structs when a Walk is done.

Jump to

Keyboard shortcuts

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