Documentation
¶
Overview ¶
Package ast defines the JSONPath abstract syntax tree.
Index ¶
- Variables
- func ChildCount(node any) int
- func ForEachChild(node any, yield func(ChildNode) bool)
- func ForEachChildReverse(node any, yield func(ChildNode) bool)
- func JSONNull() jsonNull
- func NormalizeIndex(index int64, length int) (int, bool)
- type BasicExpr
- type ChildNode
- type CompExpr
- type CompOp
- type CompValue
- type ExistExpr
- type FilterExpr
- type FuncExpr
- type FuncType
- type FuncValue
- type Function
- type FunctionValue
- type LiteralValue
- type LogicalAnd
- type LogicalOr
- type NegFuncExpr
- type NonExistExpr
- type NotParenExpr
- type ParenExpr
- type PathQuery
- type QueryValue
- type Segment
- type Selector
- type SelectorKind
- type SingularQuery
- type SliceArgs
- type SliceBounds
- type TypedLogical
- type TypedNodes
- type TypedValue
Constants ¶
This section is empty.
Variables ¶
var ErrArgCount = errors.New("wrong number of arguments")
ErrArgCount indicates a function received the wrong number of arguments.
var ErrArgType = errors.New("incompatible argument type")
ErrArgType indicates an incompatible function argument type.
Functions ¶
func ChildCount ¶ added in v0.1.16
ChildCount returns the number of direct children in node.
func ForEachChild ¶ added in v0.1.16
ForEachChild calls yield for each direct child of node.
func ForEachChildReverse ¶ added in v0.1.16
ForEachChildReverse calls yield for direct children in stack-push order.
Types ¶
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.
type CompOp ¶
type CompOp uint8
CompOp is a comparison operator.
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.
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 ¶
NewFuncExpr creates a FuncExpr for the given function and arguments.
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 ¶
Eval implements BasicExpr for logical functions. Returns false if the function is not a logical function.
func (*FuncExpr) ResultType ¶
ResultType returns the return type of the underlying function.
type FuncType ¶
type FuncType uint8
FuncType describes the return type of a function expression per RFC 9535 §2.4.1.
type FuncValue ¶
type FuncValue struct {
Func *FuncExpr // Func is the function call to evaluate.
}
FuncValue is a function call that produces a value.
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.
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.
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 ¶
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) IsSingular ¶
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) Select ¶
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.
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 Descendant ¶
Descendant creates a descendant Segment that applies selectors recursively to all descendants.
func (*Segment) IsDescendant ¶
IsDescendant reports whether the segment is a descendant segment.
func (*Segment) IsSingular ¶
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.
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 ¶
IndexSelector returns a Selector for an array index.
func NameSelector ¶
NameSelector returns a Selector for a member name.
func SliceSelector ¶
SliceSelector returns a Selector for an array slice.
func WildcardSelector ¶
func WildcardSelector() Selector
WildcardSelector returns a wildcard Selector.
func (*Selector) IsSingular ¶
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
OutputEstimate returns an upper-bound estimate for how many nodes s can append when applied to node.
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
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.