ast

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2025 License: PostgreSQL Imports: 8 Imported by: 0

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:

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

func New(lax, pred bool, n Node) (*AST, error)

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

func (a *AST) IsLax() bool

IsLax indicates whether the path query is lax.

func (*AST) IsPredicate

func (a *AST) IsPredicate() bool

IsPredicate returns true if the AST represents a PostgreSQL-style "predicate check" path.

func (*AST) IsStrict

func (a *AST) IsStrict() bool

IsStrict indicates whether the path query is strict.

func (*AST) Root

func (a *AST) Root() Node

Root returns the root node of the AST.

func (*AST) String

func (a *AST) String() string

String returns the SQL/JSON Path-encoded string representation of the 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

func NewAny(first, last int) *AnyNode

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

func (n *AnyNode) First() uint32

First returns the first index. If its value math.MaxUint32 it's considered unbounded.

func (*AnyNode) Last

func (n *AnyNode) Last() uint32

Last returns the last index. If its value math.MaxUint32 it's considered unbounded.

func (*AnyNode) Next

func (n *AnyNode) Next() Node

Next returns the next node, if any.

func (*AnyNode) String

func (n *AnyNode) String() string

String returns the SQL/JSON path any node expression.

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

func (n *BinaryNode) Next() Node

Next returns the next node, if any.

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.

func NewConst

func NewConst(kind Constant) *ConstNode

NewConst creates a new ConstNode defined by kind.

func (*ConstNode) Const

func (n *ConstNode) Const() Constant

Const returns the Constant defining n.

func (*ConstNode) Next

func (n *ConstNode) Next() Node

Next returns the next node, if any.

func (*ConstNode) String

func (n *ConstNode) String() string

String returns the string representation of n.

type Constant

type Constant int

Constant is a constant value parsed from the path.

const (
	ConstRoot     Constant = iota // $
	ConstCurrent                  // @
	ConstLast                     // last
	ConstAnyArray                 // [*]
	ConstAnyKey                   // *
	ConstTrue                     // true
	ConstFalse                    // false
	ConstNull                     // null
)

func (Constant) String

func (i Constant) String() string

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.

func (IntegerNode) Literal

func (n IntegerNode) Literal() string

Literal returns the literal text string of the number as passed to the constructor.

func (IntegerNode) Next

func (n IntegerNode) Next() Node

Next returns the next node, if any.

func (IntegerNode) String

func (n IntegerNode) String() string

String returns the normalized string representation of the number.

type KeyNode

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

KeyNode represents a SQL/JSON path key expression, e.g., '.foo'.

func NewKey

func NewKey(key string) *KeyNode

NewKey returns a new KeyNode with key.

func (KeyNode) Next

func (n KeyNode) Next() Node

Next returns the next node, if any.

func (KeyNode) String

func (n KeyNode) String() string

String returns the SQL/JSON path-encoded quoted string.

func (KeyNode) Text

func (n KeyNode) Text() string

Text returns the textual representation of the string.

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

func (n *MethodNode) Next() Node

Next returns the next node, if any.

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

func LinkNodes(nodes []Node) Node

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.

func (NumericNode) Literal

func (n NumericNode) Literal() string

Literal returns the literal text string of the number as passed to the constructor.

func (NumericNode) Next

func (n NumericNode) Next() Node

Next returns the next node, if any.

func (NumericNode) String

func (n NumericNode) String() string

String returns the normalized string representation of the number.

type RegexNode

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

RegexNode represents a regular expression.

func NewRegex

func NewRegex(expr Node, pattern, flags string) (*RegexNode, error)

NewRegex returns anew RegexNode that compares node to the regular expression pattern configured by flags.

func (*RegexNode) Next

func (n *RegexNode) Next() Node

Next returns the next node, if any.

func (*RegexNode) Operand

func (n *RegexNode) Operand() Node

Operand returns the RegexNode's operand.

func (*RegexNode) Regexp

func (n *RegexNode) Regexp() *regexp.Regexp

Regexp returns a regexp.Regexp compiled from n.

func (*RegexNode) String

func (n *RegexNode) String() string

String returns the RegexNode as a SQL/JSON path 'like_regex' expression.

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.

func (StringNode) Next

func (n StringNode) Next() Node

Next returns the next node, if any.

func (StringNode) String

func (n StringNode) String() string

String returns the SQL/JSON path-encoded quoted string.

func (StringNode) Text

func (n StringNode) Text() string

Text returns the textual representation of the string.

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

func (n *UnaryNode) Next() Node

Next returns the next node, if any.

func (*UnaryNode) Operand

func (n *UnaryNode) Operand() Node

Operand returns the UnaryNode's operand.

func (*UnaryNode) Operator

func (n *UnaryNode) Operator() UnaryOperator

Operator returns the UnaryNode's BinaryOperator.

func (*UnaryNode) String

func (n *UnaryNode) String() string

String returns the SQL/JSON path string representation of the unary expression.

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

func (n VariableNode) Next() Node

Next returns the next node, if any.

func (*VariableNode) String

func (n *VariableNode) String() string

String returns the double-quoted representation of n, preceded by '$'.

func (VariableNode) Text

func (n VariableNode) Text() string

Text returns the textual representation of the string.

Jump to

Keyboard shortcuts

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