jsonpath

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package parser parses RFC 9535 JSONPath queries into parse trees. Most JSONPath users will use package github.com/theory/jsonpath instead of this package.

inlined from: https://github.com/theory/jsonpath/blob/48554e0502cd108828c99e48ef90f56382bc7e93/spec/selector.go#L272

Index

Constants

This section is empty.

Variables

View Source
var ErrPathParse = errors.New("jsonpath")

ErrPathParse errors are returned for path parse errors.

Functions

func Parse

func Parse(reg *registry.Registry, path string) (*spec.PathQuery, error)

Parse parses path, a JSONPath query string, into a spec.PathQuery. Returns a ErrPathParse on parse failure.

Types

type FilterSelector

type FilterSelector struct {
	LogicalOr
}

FilterSelector is a filter selector, e.g., ?(), as defined by RFC 9535 Section 2.3.5. Interfaces implemented:

func Filter

func Filter(expr ...LogicalAnd) *FilterSelector

Filter returns a new FilterSelector that ORs the evaluation of each expr.

func (*FilterSelector) Eval

func (f *FilterSelector) Eval(node, root any) bool

Eval evaluates the f's [LogicalOr] expression against node and root. Uses FilterSelector.Select as it iterates over nodes, and always passes the root value($) for filter expressions that reference it.

func (*FilterSelector) Select

func (f *FilterSelector) Select(current, root any) []any

Select selects and returns values that f filters from current. Filter expressions may evaluate the current value (@), the root value ($), or any path expression. Defined by the Selector interface.

func (*FilterSelector) SelectLocated

func (f *FilterSelector) SelectLocated(current, root any, parent NormalizedPath) []*LocatedNode

SelectLocated selects and returns [LocatedNode] values with values that f filters from current. Filter expressions may evaluate the current value (@), the root value ($), or any path expression. Defined by the Selector interface.

func (*FilterSelector) String

func (f *FilterSelector) String() string

String returns a string representation of f.

type Index

type Index int

Index is an array index selector, e.g., [3], as defined by RFC 9535 Section 2.3.3. Interfaces implemented:

func (Index) Select

func (i Index) Select(input, _ any) []any

Select selects i from input and returns it as a single value in a slice. Returns an empty slice if input is not a slice or if i it outside the bounds of input. Defined by the Selector interface.

func (Index) SelectLocated

func (i Index) SelectLocated(input, _ any, parent NormalizedPath) []*LocatedNode

SelectLocated selects i from input and returns it with its normalized path as a single [LocatedNode] in a slice. Returns an empty slice if input is not a slice or if i it outside the bounds of input. Defined by the Selector interface.

func (Index) String

func (i Index) String() string

String returns a string representation of i.

type Name

type Name string

Name is a key name selector, e.g., .name or ["name"], as defined by RFC 9535 Section 2.3.1. Interfaces implemented:

func (Name) Select

func (n Name) Select(input, _ any) []any

Select selects n from input and returns it as a single value in a slice. Returns an empty slice if input is not a map[string]any or if it does not contain n. Defined by the Selector interface.

func (Name) SelectLocated

func (n Name) SelectLocated(input, _ any, parent NormalizedPath) []*LocatedNode

SelectLocated selects n from input and returns it with its normalized path as a single [LocatedNode] in a slice. Returns an empty slice if input is not a map[string]any or if it does not contain n. Defined by the Selector interface.

func (Name) String

func (n Name) String() string

String returns the quoted string representation of n.

type Selector

type Selector interface {

	// Select selects values from current and/or root and returns them.
	Select(current, root any) []any

	// SelectLocated selects values from current and/or root and returns them
	// in [LocatedNode] values with their located normalized paths
	SelectLocated(current, root any, parent NormalizedPath) []*LocatedNode
	// contains filtered or unexported methods
}

Selector represents a single Selector in an RFC 9535 JSONPath query.

type SliceSelector

type SliceSelector struct {
	// contains filtered or unexported fields
}

SliceSelector is a slice selector, e.g., [0:100:5], as defined by RFC 9535 Section 2.3.4. Interfaces implemented:

func Slice

func Slice(args ...any) SliceSelector

Slice creates a new SliceSelector. Pass up to three integers or nils for the start, end, and step arguments. Subsequent arguments are ignored.

func (SliceSelector) Bounds

func (s SliceSelector) Bounds(length int) (int, int)

Bounds returns the lower and upper bounds for selecting from a slice of length.

func (SliceSelector) End

func (s SliceSelector) End() int

End returns the end position.

func (SliceSelector) Select

func (s SliceSelector) Select(input, _ any) []any

Select selects and returns the values from input for the indexes specified by s. Returns an empty slice if input is not a slice. Indexes outside the bounds of input will not be included in the return value. Defined by the Selector interface.

func (SliceSelector) SelectLocated

func (s SliceSelector) SelectLocated(input, _ any, parent NormalizedPath) []*LocatedNode

SelectLocated selects values from input for the indexes specified by s and returns thm with their normalized paths as [LocatedNode] values. Returns an empty slice if input is not a slice. Indexes outside the bounds of input will not be included in the return value. Defined by the Selector interface.

func (SliceSelector) Start

func (s SliceSelector) Start() int

Start returns the start position.

func (SliceSelector) Step

func (s SliceSelector) Step() int

Step returns the step value.

func (SliceSelector) String

func (s SliceSelector) String() string

String returns a quoted string representation of s.

type WildcardSelector

type WildcardSelector struct{}

WildcardSelector is a wildcard selector, e.g., * or [*], as defined by RFC 9535 Section 2.3.2. Interfaces implemented:

func Wildcard

func Wildcard() WildcardSelector

Wildcard returns a WildcardSelector singleton.

func (WildcardSelector) Select

func (WildcardSelector) Select(input, _ any) []any

Select selects the values from input and returns them in a slice. Returns an empty slice if input is not []any map[string]any. Defined by the Selector interface.

func (WildcardSelector) SelectLocated

func (WildcardSelector) SelectLocated(input, _ any, parent NormalizedPath) []*LocatedNode

SelectLocated selects the values from input and returns them with their normalized paths in a slice of [LocatedNode] values. Returns an empty slice if input is not []any map[string]any. Defined by the Selector interface.

func (WildcardSelector) String

func (WildcardSelector) String() string

String returns "*".

Jump to

Keyboard shortcuts

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