cursor

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

Package cursor implements traversal over the AST of a JSON value.

Index

Constants

This section is empty.

Variables

View Source
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

func Path

func Path[T ast.Value](v ast.Value, path ...any) (T, error)

Path traverses a sequential path into the structure of v where path elements are as documented for the Cursor.Down method. This is a convenience wrapper for creating a cursor, applying path, and retrieving its value.

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 New

func New(origin ast.Value) *Cursor

New constructs a new Cursor to traverse the structure of origin.

func (*Cursor) AtOrigin

func (c *Cursor) AtOrigin() bool

AtOrigin reports whether c is at its origin.

func (*Cursor) Down

func (c *Cursor) Down(path ...any) *Cursor

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) Err

func (c *Cursor) Err() error

Err reports the error from the most recent traversal operation, if any.

func (*Cursor) Get

func (c *Cursor) Get() (ast.Value, error)

Get reports the current value under the cursor and the error, if any.

func (*Cursor) Origin

func (c *Cursor) Origin() ast.Value

Origin returns the origin value of c.

func (*Cursor) Path

func (c *Cursor) Path() []ast.Value

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.

func (*Cursor) Up

func (c *Cursor) Up() *Cursor

Up moves the cursor one position upward in the structure, if possible. It returns c to permit chaining.

func (*Cursor) Value

func (c *Cursor) Value() ast.Value

Value reports the current value under the cursor.

Jump to

Keyboard shortcuts

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