Documentation
¶
Overview ¶
Package cursor implements traversal over the AST of a JSON value.
Index ¶
- Variables
- func Path[T ast.Value](v ast.Value, path ...any) (T, error)
- type Cursor
- func (c *Cursor) AtOrigin() bool
- func (c *Cursor) Down(path ...any) *Cursor
- func (c *Cursor) Err() error
- func (c *Cursor) Get() (ast.Value, error)
- func (c *Cursor) Origin() ast.Value
- func (c *Cursor) Path() []ast.Value
- func (c *Cursor) Reset()
- func (c *Cursor) Up() *Cursor
- func (c *Cursor) Value() ast.Value
Constants ¶
This section is empty.
Variables ¶
var ErrKeyNotFound = errors.New("key not found")
ErrKeyNotFound is a sentinel error reported when a name or array index lookup fails into a value of the correct type.
Functions ¶
Types ¶
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
A Cursor is a pointer that navigates into the structure of a ast.Value.
func (*Cursor) Down ¶
Down traverses a sequential path into the structure of c starting from the current value, where path elements are either strings (denoting object keys), integers (denoting offsets into arrays), functions (see below), or nil. If the path is valid, the element reached is returned. If the path cannot be completely consumed, traversal stops and an error is recorded. Use Err to recover the error.
If a path element is a string, the corresponding value must be an object, and the string resolves an object member with that name. If this is the last element of the path, the member is returned; otherwise, subsequent path elements continue from the value of that member. Use a nil path element to resolve an object member at the end of a path.
By default, object members are compared case-sensitively. A string path element beginning with "%" requests a case-insensitive match. Double the leading "%" to escape this meaning.
If a path element is an integer, the corresponding value must be an array or object, and the integer resolves to an index in the array or object. Negative indices count backward from the end (-1 is last, -2 second last). An error is reported if the index is out of bounds.
If a path element is a function with this signature
func(ast.Text) bool
the corresponding value must be an object, and the function resolves the first object member whose key is reported true by the function.
If a path element is a function with this signature
func(ast.Value) (ast.Value, error)
the function is executed and its result becomes the next object in the sequence. If the function reports an error, traversal stops and the error is recorded.
func (*Cursor) Path ¶
Path reports the complete sequence of values from the origin to the current location in c.
func (*Cursor) Reset ¶
func (c *Cursor) Reset()
Reset resets the cursor to its origin and clears its error.