objwalker

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2022 License: MIT Imports: 5 Imported by: 0

README

Go Reference Coverage Status GoReportCard

Deep walk by object with reflection. Walker.Walk(v interface{}) call callback function for v object, every field if it struct, every item for slice/array and every key and item for map object. It walk for object recursive and call callback for every object in tree.

It has loop protection - for not hang on cycled structured, protection can be disabled if need.

WalkInfo - struct, send as argument to callback function include:

  • Value - reflection.Value object for read/manipulate with it.
  • DataPointer - direct pointer to underly data, for example - pointer to bytes under string, ot pointer to data under slice. It is danger to manipulate it, but can userful for example for compare objects.
  • Parent - parent of the value in travel tree
  • and some other hints about Value
    package main

    import "github.com/rekby/objwalker"

    func main() {
		type S struct {
			Val1  int
			Slice []string
		}

		val := S{
			Val1:  2,
			Slice: []string{"hello", "world"},
		}
		_ = New(func(info *WalkInfo) error {
			fmt.Println(info.Value.Interface())
			return nil
		}).Walk(val)

		// Output:
		// {2 [hello world]}
		// 2
		// [hello world]
		// hello
		// world
	}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrSkip - signal for skip iteration over value
	// can be returned for array, interface, map, map key, slice, struct, ptr,
	// for other kinds - unspecified behaviour and it may be change for feature versions
	ErrSkip = errors.New("skip value")

	// ErrUnknownKind mean reflect walk see unknown kind of type - need to update library
	ErrUnknownKind = errors.New("unknown kind")
)

Functions

This section is empty.

Types

type WalkFunc

type WalkFunc func(info *WalkInfo) error

WalkFunc is type of callback function

type WalkInfo

type WalkInfo struct {
	// Value - reflection Value for inspect/manupulate variable
	Value reflect.Value

	// Parent is info of prev node in travel tree hierarchy
	// Parent == nil for first visited value
	Parent *WalkInfo

	// DirectPointer hold address of Value data (Value.ptr) 0 if value not addressable
	DirectPointer unsafe.Pointer

	// IsVisited true if loop protection disabled and walker detect about value was visited already
	IsVisited bool
	// contains filtered or unexported fields
}

WalkInfo send to walk callback with every value

func (*WalkInfo) HasDirectPointer added in v0.0.3

func (w *WalkInfo) HasDirectPointer() bool

HasDirectPointer check if w.DirectPointer has non zero value

func (*WalkInfo) IsMapKey

func (w *WalkInfo) IsMapKey() bool

IsMapKey mean Value direct use as map key

func (*WalkInfo) IsMapValue

func (w *WalkInfo) IsMapValue() bool

IsMapValue mean Value direct use as map value

type Walker

type Walker struct {
	LoopProtection bool
	// contains filtered or unexported fields
}

Walker provide settings and state for Walk function

Example
type S struct {
	Val1  int
	Slice []string
}

val := S{
	Val1:  2,
	Slice: []string{"hello", "world"},
}
_ = New(func(info *WalkInfo) error {
	fmt.Println(info.Value.Interface())
	return nil
}).Walk(val)
Output:

{2 [hello world]}
2
[hello world]
hello
world

func New

func New(f WalkFunc) *Walker

New create new walker with f callback f will call for every field, item, etc of walked object f can called multiply times for same address with different item type for example: type T struct { Val int } f will called for struct T and for Pub int

if f return ErrSkip - skip the struct (, map, slice, ... see ErrSkip comment) if f return other non nil error - stop walk and return the error to walk caller

func (Walker) Walk

func (w Walker) Walk(v interface{}) error

Walk create new walker with empty state and run Walk over object

func (*Walker) WithDisableLoopProtection

func (w *Walker) WithDisableLoopProtection() *Walker

WithDisableLoopProtection disable loop protection. callback must self-detect loops and return ErrSkip

Jump to

Keyboard shortcuts

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