Documentation
¶
Overview ¶
Package ast provides an abstract syntax tree for SQL/JSON paths.
Largely ported from PostgreSQL's jsonpath.c, it provides objects for every node parsed from an SQL/JSON path. The [parser] constructs these nodes as it parses a path, and constructs an AST object from the root node.
Note that errors returned by AST are not wrapped, as they're expected to be wrapped by parser.
The complete list of types that implement Node:
- ConstNode
- MethodNode
- StringNode
- VariableNode
- KeyNode
- NumericNode
- IntegerNode
- AnyNode
- BinaryNode
- UnaryNode
- RegexNode
- ArrayIndexNode
Here's a starter recursive function for processing nodes.
func processNode(node ast.Node) { switch node := node.(type) { case *ast.ConstNode: case *ast.MethodNode: case *ast.StringNode: case *ast.VariableNode: case *ast.KeyNode: case *ast.NumericNode: case *ast.IntegerNode: case *ast.AnyNode: case *ast.BinaryNode: if node.Left() != nil { processNode(node.Left()) } if node.Right() != nil { processNode(node.Right()) } case *ast.UnaryNode: processNode(node.Operand()) case *ast.RegexNode: processNode(node.Operand()) case *ast.ArrayIndexNode: for _, n := range node.Subscripts() { processNode(n) } } if next := node.Next(); next != nil { processNode(next) } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AST ¶
type AST struct {
// contains filtered or unexported fields
}
AST represents the complete abstract syntax tree for a parsed SQL/JSON path.
func New ¶
New creates a new AST with n as its root. If lax is true it's considered a lax path query, and if pred is true it's considered a predicate query.
func (*AST) IsPredicate ¶
IsPredicate returns true if the AST represents a PostgreSQL-style "predicate check" path.
type AnyNode ¶
type AnyNode struct {
// contains filtered or unexported fields
}
AnyNode represents any node in a path accessor with the expression 'first TO last'.
func NewAny ¶
NewAny returns a new AnyNode with first as its first index and last as its last. If either number is negative it's considered unbounded. Numbers greater than math.MaxUint32 (or math.MaxInt on 32-bit systems) will max out at that number.
func (*AnyNode) First ¶
First returns the first index. If its value math.MaxUint32 it's considered unbounded.
type ArrayIndexNode ¶
type ArrayIndexNode struct {
// contains filtered or unexported fields
}
ArrayIndexNode represents the nodes in an array index expression.
func NewArrayIndex ¶
func NewArrayIndex(subscripts []Node) *ArrayIndexNode
NewArrayIndex creates a new ArrayIndexNode consisting of subscripts. which must be BinaryNodes using the BinarySubscript operator.
func (*ArrayIndexNode) Next ¶
func (n *ArrayIndexNode) Next() Node
Next returns the next node, if any.
func (*ArrayIndexNode) String ¶
func (n *ArrayIndexNode) String() string
String produces JSON Path array index string representation of the nodes in n.
func (*ArrayIndexNode) Subscripts ¶
func (n *ArrayIndexNode) Subscripts() []Node
Subscripts returns all of the subscript nodes in n.
type BinaryNode ¶
type BinaryNode struct {
// contains filtered or unexported fields
}
BinaryNode represents a binary operation.
func NewBinary ¶
func NewBinary(op BinaryOperator, left, right Node) *BinaryNode
NewBinary returns a new BinaryNode where op represents the binary operator and left and right the operands.
func (*BinaryNode) Left ¶
func (n *BinaryNode) Left() Node
Left returns the BinaryNode's left operand.
func (*BinaryNode) Operator ¶
func (n *BinaryNode) Operator() BinaryOperator
Operator returns the BinaryNode's BinaryOperator.
func (*BinaryNode) Right ¶
func (n *BinaryNode) Right() Node
Right returns the BinaryNode's right operand.
func (*BinaryNode) String ¶
func (n *BinaryNode) String() string
String returns the SQL/JSON path string representation of the binary expression.
type BinaryOperator ¶
type BinaryOperator int
BinaryOperator represents a binary operator.
const ( BinaryAnd BinaryOperator = iota // && BinaryOr // || BinaryEqual // == BinaryNotEqual // != BinaryLess // < BinaryGreater // > BinaryLessOrEqual // <= BinaryGreaterOrEqual // >= BinaryStartsWith // starts with BinaryAdd // + BinarySub // - BinaryMul // * BinaryDiv // / BinaryMod // % BinarySubscript // to BinaryDecimal // .decimal() )
func (BinaryOperator) String ¶
func (i BinaryOperator) String() string
type ConstNode ¶
type ConstNode struct {
// contains filtered or unexported fields
}
ConstNode represents a constant node in the path.
type IntegerNode ¶
type IntegerNode struct {
// contains filtered or unexported fields
}
IntegerNode represents an integral value.
func NewInteger ¶
func NewInteger(integer string) *IntegerNode
NewInteger returns a new IntegerNode representing num. Panics if integer cannot be parsed into int64.
func (*IntegerNode) Int ¶
func (n *IntegerNode) Int() int64
Int returns the integer corresponding to n.
type KeyNode ¶
type KeyNode struct {
// contains filtered or unexported fields
}
KeyNode represents a SQL/JSON path key expression, e.g., '.foo'.
type MethodName ¶
type MethodName int
MethodName represents the name of a path method.
const ( MethodAbs MethodName = iota // .abs() MethodSize // .size() MethodType // .type() MethodFloor // .floor() MethodCeiling // .ceiling() MethodDouble // .double() MethodKeyValue // .keyvalue() MethodBigInt // .bigint() MethodBoolean // .boolean() MethodInteger // .integer() MethodNumber // .number() MethodString // .string() )
func (MethodName) String ¶
func (i MethodName) String() string
type MethodNode ¶
type MethodNode struct {
// contains filtered or unexported fields
}
MethodNode represents a path method.
func NewMethod ¶
func NewMethod(name MethodName) *MethodNode
NewMethod returns a new MethodNode with name.
func (*MethodNode) Name ¶
func (n *MethodNode) Name() MethodName
Name returns the MethodName of the method.
func (*MethodNode) String ¶
func (n *MethodNode) String() string
String returns the SQL/JSON representation of the method: A dot, the name, then parentheses.
type Node ¶
type Node interface { fmt.Stringer // Next returns the next node when the node is part of a linked list of // nodes. Next() Node // contains filtered or unexported methods }
Node represents a single node in the AST.
func LinkNodes ¶
LinkNodes assembles nodes into a linked list, where a call to Next on each returns the next node in the list until the last node, where Next returns nil.
func NewUnaryOrNumber ¶
func NewUnaryOrNumber(op UnaryOperator, node Node) Node
NewUnaryOrNumber returns a new node for op ast.UnaryPlus or ast.UnaryMinus. If node is numeric and not the first item in an accessor list, it returns a ast.NumericNode or ast.IntegerNode, as appropriate.
type NumericNode ¶
type NumericNode struct {
// contains filtered or unexported fields
}
NumericNode represents a numeric (non-integer) value.
func NewNumeric ¶
func NewNumeric(num string) *NumericNode
NewNumeric returns a new NumericNode representing num. Panics if num cannot be parsed into JSON-compatible float64.
func (*NumericNode) Float ¶
func (n *NumericNode) Float() float64
Float returns the floating point number corresponding to n.
type RegexNode ¶
type RegexNode struct {
// contains filtered or unexported fields
}
RegexNode represents a regular expression.
func NewRegex ¶
NewRegex returns anew RegexNode that compares node to the regular expression pattern configured by flags.
type StringNode ¶
type StringNode struct {
// contains filtered or unexported fields
}
StringNode represents a string parsed from the path.
func NewString ¶
func NewString(str string) *StringNode
NewString returns a new StringNode representing str.
type UnaryNode ¶
type UnaryNode struct {
// contains filtered or unexported fields
}
UnaryNode represents a unary operation.
func NewUnary ¶
func NewUnary(op UnaryOperator, node Node) *UnaryNode
NewUnary returns a new UnaryNode where op represents the unary operator and node its operand.
func (*UnaryNode) Operator ¶
func (n *UnaryNode) Operator() UnaryOperator
Operator returns the UnaryNode's BinaryOperator.
type UnaryOperator ¶
type UnaryOperator int
UnaryOperator represents a unary operator.
const ( UnaryExists UnaryOperator = iota // exists UnaryNot // ! UnaryIsUnknown // is unknown UnaryPlus // + UnaryMinus // - UnaryFilter // ? UnaryDateTime // .datetime UnaryDate // .date UnaryTime // .time UnaryTimeTZ // .time_tz UnaryTimestamp // .timestamp UnaryTimestampTZ // .timestamp_tz )
func (UnaryOperator) String ¶
func (i UnaryOperator) String() string
type VariableNode ¶
type VariableNode struct {
// contains filtered or unexported fields
}
VariableNode represents a SQL/JSON path variable name.
func NewVariable ¶
func NewVariable(name string) *VariableNode
NewVariable returns a new VariableNode named name.
func (*VariableNode) String ¶
func (n *VariableNode) String() string
String returns the double-quoted representation of n, preceded by '$'.