ast

package
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package ast defines the JSONPath abstract syntax tree.

Index

Constants

This section is empty.

Variables

View Source
var ErrArgCount = errors.New("wrong number of arguments")

ErrArgCount indicates a function received the wrong number of arguments.

View Source
var ErrArgType = errors.New("incompatible argument type")

ErrArgType indicates an incompatible function argument type.

Functions

func ChildCount added in v0.1.16

func ChildCount(node any) int

ChildCount returns the number of direct children in node.

func ForEachChild added in v0.1.16

func ForEachChild(node any, yield func(ChildNode) bool)

ForEachChild calls yield for each direct child of node.

func ForEachChildReverse added in v0.1.16

func ForEachChildReverse(node any, yield func(ChildNode) bool)

ForEachChildReverse calls yield for direct children in stack-push order.

func JSONNull

func JSONNull() jsonNull

JSONNull returns a sentinel value representing a literal JSON null.

func NormalizeIndex added in v0.1.16

func NormalizeIndex(index int64, length int) (int, bool)

NormalizeIndex converts a JSONPath array index to a Go slice index.

Types

type BasicExpr

type BasicExpr interface {
	Eval(current, root any) bool
}

BasicExpr is a filter expression that evaluates to a boolean.

type ChildNode added in v0.1.16

type ChildNode struct {
	Value any
	Name  string
	Index int
	Array bool
}

ChildNode is one direct child of a JSON object or array.

type CompExpr

type CompExpr struct {
	Left  CompValue // Left is the left operand.
	Op    CompOp    // Op is the comparison operator.
	Right CompValue // Right is the right operand.
}

CompExpr is a comparison expression.

func (*CompExpr) Eval

func (c *CompExpr) Eval(current, root any) bool

Eval evaluates the comparison expression.

type CompOp

type CompOp uint8

CompOp is a comparison operator.

const (
	Equal        CompOp = iota // ==
	NotEqual                   // !=
	Less                       // <
	LessEqual                  // <=
	Greater                    // >
	GreaterEqual               // >=
)

CompOp values.

func (CompOp) String added in v0.1.16

func (op CompOp) String() string

type CompValue

type CompValue interface {
	Value(current, root any) runtimeValue
}

CompValue represents a comparable value in a comparison expression.

type ExistExpr

type ExistExpr struct {
	Query *PathQuery // Query is evaluated against the current filter context.
}

ExistExpr tests if a query selects at least one node.

func (*ExistExpr) Eval

func (e *ExistExpr) Eval(current, root any) bool

Eval returns true if the query selects at least one node.

type FilterExpr

type FilterExpr struct {
	Or LogicalOr // Or is the top-level logical-or expression.
}

FilterExpr represents a filter expression tree (?logical-expr) per RFC 9535 §2.3.5.

func (*FilterExpr) Eval

func (f *FilterExpr) Eval(current, root any) bool

Eval evaluates the filter expression against the current node.

type FuncExpr

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

FuncExpr represents a function call in a filter expression per RFC 9535 §2.4.

func NewFuncExpr

func NewFuncExpr(fn Function, args ...any) *FuncExpr

NewFuncExpr creates a FuncExpr for the given function and arguments.

func (*FuncExpr) Args

func (fe *FuncExpr) Args() []any

Args returns the argument expressions.

func (*FuncExpr) Call

func (fe *FuncExpr) Call(current, root any) FunctionValue

Call evaluates the function with the given current and root nodes. It evaluates argument expressions and passes the results to the underlying function.

func (*FuncExpr) Eval

func (fe *FuncExpr) Eval(current, root any) bool

Eval implements BasicExpr for logical functions. Returns false if the function is not a logical function.

func (*FuncExpr) Func

func (fe *FuncExpr) Func() Function

Func returns the resolved Function.

func (*FuncExpr) Name

func (fe *FuncExpr) Name() string

Name returns the function name.

func (*FuncExpr) ResultType

func (fe *FuncExpr) ResultType() FuncType

ResultType returns the return type of the underlying function.

func (*FuncExpr) String

func (fe *FuncExpr) String() string

String returns the canonical string representation of fe.

type FuncType

type FuncType uint8

FuncType describes the return type of a function expression per RFC 9535 §2.4.1.

const (
	// Logical indicates the function returns a logical (bool) value.
	Logical FuncType = iota
	// Value indicates the function returns a single JSON value.
	Value
	// Nodes indicates the function returns a node list.
	Nodes
)

func (FuncType) String

func (ft FuncType) String() string

String returns the string representation of ft.

type FuncValue

type FuncValue struct {
	Func *FuncExpr // Func is the function call to evaluate.
}

FuncValue is a function call that produces a value.

func (*FuncValue) Value

func (f *FuncValue) Value(current, root any) runtimeValue

Value returns the result of the function call.

type Function

type Function interface {
	// Name returns the function name as used in JSONPath expressions.
	Name() string
	// ResultType returns the FuncType of the function's return value.
	ResultType() FuncType
	// ParameterCount returns the function's fixed arity.
	ParameterCount() int
	// ParameterType returns the semantic type of parameter index.
	ParameterType(index int) FuncType
	// Call evaluates the function at query time and returns the result.
	Call(args []FunctionValue) FunctionValue
}

Function defines a function that can be called in filter expressions. Implementations must be safe for concurrent use.

type FunctionValue added in v0.1.16

type FunctionValue interface {
	ResultType() FuncType
	// contains filtered or unexported methods
}

FunctionValue is one of the RFC 9535 function runtime value types.

type LiteralValue

type LiteralValue struct {
	Val any // Val is the literal JSON value.
}

LiteralValue is a literal value (string, number, bool, null).

func (*LiteralValue) Value

func (l *LiteralValue) Value(current, root any) runtimeValue

Value returns the literal value.

type LogicalAnd

type LogicalAnd []BasicExpr

LogicalAnd is a sequence of BasicExpr joined by &&. Short-circuits on first false.

func (LogicalAnd) Eval

func (la LogicalAnd) Eval(current, root any) bool

Eval returns true if all BasicExpr are true.

type LogicalOr

type LogicalOr []LogicalAnd

LogicalOr is a sequence of LogicalAnd expressions joined by ||. Short-circuits on first true.

func (LogicalOr) Eval

func (lo LogicalOr) Eval(current, root any) bool

Eval returns true if any LogicalAnd expression is true.

type NegFuncExpr

type NegFuncExpr struct {
	Func *FuncExpr // Func is the logical function call being negated.
}

NegFuncExpr is a negated logical function call expression (!match(), !search()).

func (*NegFuncExpr) Eval

func (n *NegFuncExpr) Eval(current, root any) bool

Eval evaluates the negated function call.

type NonExistExpr

type NonExistExpr struct {
	Query *PathQuery // Query is evaluated against the current filter context.
}

NonExistExpr tests if a query selects no nodes.

func (*NonExistExpr) Eval

func (e *NonExistExpr) Eval(current, root any) bool

Eval returns true if the query selects no nodes.

type NotParenExpr

type NotParenExpr struct {
	Expr *LogicalOr // Expr is the enclosed logical expression.
}

NotParenExpr is a negated parenthesized logical expression.

func (*NotParenExpr) Eval

func (n *NotParenExpr) Eval(current, root any) bool

Eval evaluates the negated parenthesized expression.

type ParenExpr

type ParenExpr struct {
	Expr *LogicalOr // Expr is the enclosed logical expression.
}

ParenExpr is a parenthesized logical expression.

func (*ParenExpr) Eval

func (p *ParenExpr) Eval(current, root any) bool

Eval evaluates the parenthesized expression.

type PathQuery

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

PathQuery is the root of a compiled JSONPath expression. It holds a sequence of segments and whether the query is rooted ($) or relative (@).

func NewPathQuery

func NewPathQuery(root bool, segments ...Segment) *PathQuery

NewPathQuery creates a PathQuery. When root is true it indicates a root-identifier ($) query; when false it indicates a current-node (@) query used in filter sub-expressions.

func (*PathQuery) IsRoot

func (q *PathQuery) IsRoot() bool

IsRoot reports whether the query starts from the root ($).

func (*PathQuery) IsSingular

func (q *PathQuery) IsSingular() bool

IsSingular reports whether the query always selects at most one node. A query is singular when every segment is singular (child segment with exactly one name or index selector) and no segment is a descendant segment.

func (*PathQuery) Segments

func (q *PathQuery) Segments() []Segment

Segments returns the query's segments.

func (*PathQuery) Select

func (q *PathQuery) Select(current, root any) []any

Select evaluates the query against the given current and root nodes. For root queries ($), it evaluates against root. For relative queries (@), it evaluates against current.

func (*PathQuery) Singular

func (q *PathQuery) Singular() *SingularQuery

Singular returns the SingularQuery variant of q if q is a singular query, or nil otherwise.

func (*PathQuery) String

func (q *PathQuery) String() string

String returns the canonical string representation of the query, e.g. $["a"][0] or @["name"].

type QueryValue

type QueryValue struct {
	Query *PathQuery // Query is evaluated against the current filter context.
}

QueryValue is a singular query that produces a single value.

func (*QueryValue) Value

func (q *QueryValue) Value(current, root any) runtimeValue

Value returns the first value selected by the query, or Nothing when the query does not select exactly one node.

type Segment

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

Segment represents a child or descendant segment as defined in RFC 9535 §1.4.2. A segment holds one or more selectors.

func Child

func Child(sel ...Selector) Segment

Child creates a child Segment that applies selectors to direct children.

func Descendant

func Descendant(sel ...Selector) Segment

Descendant creates a descendant Segment that applies selectors recursively to all descendants.

func (*Segment) Apply

func (s *Segment) Apply(nodes []any, root any) []any

Apply applies the segment to a list of nodes and returns the result.

func (*Segment) IsDescendant

func (s *Segment) IsDescendant() bool

IsDescendant reports whether the segment is a descendant segment.

func (*Segment) IsSingular

func (s *Segment) IsSingular() bool

IsSingular reports whether the segment selects at most one node. A segment is singular only if it is a child segment with exactly one singular selector.

func (*Segment) Selectors

func (s *Segment) Selectors() []Selector

Selectors returns the segment's selectors.

func (*Segment) String

func (s *Segment) String() string

String returns the canonical string representation of the segment.

type Selector

type Selector struct {
	Kind   SelectorKind // Kind identifies which selector field is active.
	Name   string       // Name is the member name for [Name] selectors.
	Index  int64        // Index is the array index for [Index] selectors.
	Slice  SliceArgs    // Slice holds the bounds for [Slice] selectors.
	Filter *FilterExpr  // Filter holds the predicate for [Filter] selectors.
}

Selector is a tagged union representing one of the five RFC 9535 selector types. Using a concrete struct instead of an interface keeps selector slices contiguous in memory for cache efficiency.

func FilterSelector

func FilterSelector(expr *FilterExpr) Selector

FilterSelector returns a filter Selector.

func IndexSelector

func IndexSelector(idx int64) Selector

IndexSelector returns a Selector for an array index.

func NameSelector

func NameSelector(name string) Selector

NameSelector returns a Selector for a member name.

func SliceSelector

func SliceSelector(args SliceArgs) Selector

SliceSelector returns a Selector for an array slice.

func WildcardSelector

func WildcardSelector() Selector

WildcardSelector returns a wildcard Selector.

func (*Selector) Apply

func (s *Selector) Apply(out []any, node, root any) []any

Apply applies the selector to a node and appends matching results to out.

func (*Selector) IsSingular

func (s *Selector) IsSingular() bool

IsSingular reports whether the selector can select at most one node. Only name and index selectors are singular.

func (*Selector) OutputEstimate added in v0.1.16

func (s *Selector) OutputEstimate(node any) int

OutputEstimate returns an upper-bound estimate for how many nodes s can append when applied to node.

func (*Selector) String

func (s *Selector) String() string

String returns the canonical string representation of s.

type SelectorKind

type SelectorKind uint8

SelectorKind identifies the variant stored in a Selector.

const (
	Name     SelectorKind = iota // member name selector
	Index                        // array index selector
	Slice                        // array slice selector
	Wildcard                     // wildcard selector
	Filter                       // filter selector
)

SelectorKind values.

type SingularQuery

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

SingularQuery is a JSONPath query that is guaranteed to select at most one node. It is composed of a flat list of name/index selectors extracted from singular segments. Per RFC 9535, singular queries can be used as comparison operands and as arguments to the value() function.

func NewSingularQuery

func NewSingularQuery(relative bool, selectors ...Selector) *SingularQuery

NewSingularQuery creates a SingularQuery. When relative is true, the query starts from the current node (@); otherwise from the root ($).

func (*SingularQuery) IsRelative

func (sq *SingularQuery) IsRelative() bool

IsRelative reports whether the query is relative (@) rather than rooted ($).

func (*SingularQuery) Selectors

func (sq *SingularQuery) Selectors() []Selector

Selectors returns the singular query's selectors.

func (*SingularQuery) String

func (sq *SingularQuery) String() string

String returns the canonical string representation of the singular query.

type SliceArgs

type SliceArgs struct {
	Start    int64 // Start is the explicit slice start when [SliceArgs.HasStart] is true.
	End      int64 // End is the explicit slice end when [SliceArgs.HasEnd] is true.
	Step     int64 // Step is the explicit slice step when [SliceArgs.HasStep] is true.
	HasStart bool  // HasStart reports whether Start was present in the query.
	HasEnd   bool  // HasEnd reports whether End was present in the query.
	HasStep  bool  // HasStep reports whether Step was present in the query.
}

SliceArgs holds the optional start, end, and step for a slice selector.

type SliceBounds added in v0.1.16

type SliceBounds struct {
	Start int64
	End   int64
	Step  int64
}

SliceBounds holds normalized slice selector bounds.

func ResolveSliceBounds added in v0.1.16

func ResolveSliceBounds(args SliceArgs, length int) (SliceBounds, bool)

ResolveSliceBounds normalizes a slice selector for an array length.

func (SliceBounds) Count added in v0.1.16

func (b SliceBounds) Count() int

Count returns the number of indexes selected by b.

func (SliceBounds) ForEachSliceIndex added in v0.1.16

func (b SliceBounds) ForEachSliceIndex(yield func(int) bool)

ForEachSliceIndex calls yield for each index selected by b.

type TypedLogical added in v0.1.16

type TypedLogical bool

TypedLogical is a logical function result.

func (TypedLogical) Bool added in v0.1.16

func (l TypedLogical) Bool() bool

Bool returns l as a bool.

func (TypedLogical) ResultType added in v0.1.16

func (TypedLogical) ResultType() FuncType

ResultType returns Logical.

type TypedNodes added in v0.1.16

type TypedNodes []any

TypedNodes is a function node-list value.

func (TypedNodes) ResultType added in v0.1.16

func (TypedNodes) ResultType() FuncType

ResultType returns Nodes.

type TypedValue added in v0.1.16

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

TypedValue is a JSON value or the absence of a value.

func NewValue added in v0.1.16

func NewValue(value any) TypedValue

NewValue returns a present JSON value for function execution.

func NoValue added in v0.1.16

func NoValue() TypedValue

NoValue returns an absent value for function execution.

func (TypedValue) Any added in v0.1.16

func (v TypedValue) Any() any

Any returns the underlying JSON value. It returns nil for both JSON null and an absent value; use TypedValue.IsNothing to distinguish them.

func (TypedValue) IsNothing added in v0.1.16

func (v TypedValue) IsNothing() bool

IsNothing reports whether v is absent rather than JSON null.

func (TypedValue) ResultType added in v0.1.16

func (TypedValue) ResultType() FuncType

ResultType returns Value.

Jump to

Keyboard shortcuts

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