Documentation ¶
Index ¶
- func Depth(p string) uint
- type Context
- type FoundType
- type Path
- func (p *Path) Clone() *Path
- func (p *Path) Depth() uint
- func (p *Path) First(currentElement any) *Context
- func (p *Path) HasArray() bool
- func (p *Path) LastParent() *Path
- func (p *Path) String() string
- func (p *Path) Tail() *Path
- func (p *Path) Truncate(depth uint) *Path
- func (p *Path) Walk(currentElement any, f func(*Context))
- type PathType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Context ¶
type Context struct { Value any Parent any // Either map[string]any 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 // contains filtered or unexported fields }
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.
type Path ¶
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 ¶
Parse transform given path string representation into usable Path.
Example paths:
name object.field object.subobject.field object.* object.array[] object.arrayOfObjects[].field [] [].field
func (*Path) Depth ¶
Depth returns the depth of the path. For each step in the path, increments the depth by one.
func (*Path) First ¶
First returns the first final element matched by the Path. Note that the returned Context may indicate that the value could not be found, so you should always check `Context.Found` before using `Context.Value`.
Bear in mind that map iteration order is not guaranteed. Using paths containing wildcards `*` will not always yield the same result.
func (*Path) LastParent ¶
LastParent returns the last step in the path that is not a PathTypeElement, excluding the first step in the path, or nil.
func (*Path) Truncate ¶
Truncate returns the n first steps of the path so the returned path's depth equals the given depth.
func (*Path) Walk ¶
Walk this path and execute the given callback for each matching element. Elements are final, meaning they are the deepest explorable element using this path. Only `map[string]any` 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]any`) // and not a final element. PathTypeObject )