walk

package
v4.4.11 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2023 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Value  interface{}
	Parent interface{} // Either map[string]interface{} or a slice
	Path   *Path       // Exact Path to the current element
	Name   string      // Name of the current element
	Index  int         // If parent is a slice, the index of the current element in the slice, else -1
	Found  FoundType   // True if the path could not be completely explored
}

Context information sent to walk function.

type FoundType

type FoundType int

FoundType adds extra information about not found elements whether what's not found is their parent or themselves.

const (
	// Found indicates the element could be found.
	Found FoundType = iota
	// ParentNotFound indicates one of the parents of the element could no be found.
	ParentNotFound
	// ElementNotFound indicates all parents of the element were found but the element
	// itself could not.
	ElementNotFound
)

type Path

type Path struct {
	Next  *Path
	Index *int
	Name  string
	Type  PathType
}

Path allows for complex untyped data structure exploration. An instance of this structure represents a step in exploration. Items NOT having `PathTypeElement` as a `Type` are expected to have a non-nil `Next`.

func Parse

func Parse(p string) (*Path, error)

Parse transform given path string representation into usable Path.

Example paths:

name
object.field
object.subobject.field
object.array[]
object.arrayOfObjects[].field

func (*Path) Clone

func (p *Path) Clone() *Path

Clone returns a deep clone of this Path.

func (*Path) HasArray

func (p *Path) HasArray() bool

HasArray returns true if a least one step in the path involves an array.

func (*Path) LastParent

func (p *Path) LastParent() *Path

LastParent returns the last step in the path that is not a PathTypeElement, excluding the first step in the path, or nil.

func (*Path) Tail

func (p *Path) Tail() *Path

Tail returns the last step in the path.

func (*Path) Walk

func (p *Path) Walk(currentElement interface{}, f func(Context))

Walk this path and execute the given behavior for each matching element. Elements are final, meaning they are the deepest explorable element using this path. Only `map[string]interface{}` and n-dimensional slices parents are supported. The given "f" function is executed for each final element matched. If the path cannot be completed because the step's name doesn't exist in the currently explored map, the function will be executed as well, with a the `Context`'s `NotFound` field set to `true`.

type PathType

type PathType int

PathType type of the element being explored.

const (
	// PathTypeElement the explored element is used as a final element (leaf).
	PathTypeElement PathType = iota

	// PathTypeArray the explored element is used as an array and not a final element.
	// All elements in the array will be explored using the next Path.
	PathTypeArray

	// PathTypeObject the explored element is used as an object (`map[string]interface{}`)
	// and not a final element.
	PathTypeObject
)

Jump to

Keyboard shortcuts

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