paths

package
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package jsonpath/paths provides a parser for JSONpaths, a syntax for expressing queries and locations in a JSON structure.

The JSONpath syntax is often defined by providing a set of sample paths. Following https://github.com/dchester/jsonpath/, this package instead is based on a grammar, Briefly, a JSONpath gives a dot-separated path through a JSON structure, with nested expressions providing dynamic values and filters. For the detailed syntax, run

go doc jsonpath/syntax

ParsePath returns a Path that represents the JSONpath provided as text. That result Path, and the Steps and Expr trees it contains, can then be evaluated against a subject JSON structure ("document").

ParseScriptExpression parses a string that contains only a script expression (not a path) and returns the Expr tree. It is not normally needed, because ParsePath will parse any script expressions in a path string, but might be useful for calculating using values in a JSON structure.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnclosedString = errors.New("unclosed string literal")
	ErrBadEscape      = errors.New("unknown character escape sequence")
	ErrShortEscape    = errors.New("unicode escape needs 4 hex digits")
	ErrIntOverflow    = errors.New("overflow of negative integer literal")
	ErrBadReal        = errors.New("invalid floating-point literal syntax")
	ErrCtrlChar       = errors.New("must use escape to encode ctrl character")
)
View Source
var (
	ErrUnclosedRE = errors.New("unclosed regular expression literal")
)

Functions

This section is empty.

Types

type BoolLeaf

type BoolLeaf struct {
	Op
	Val bool
}

BoolLeaf represents a Boolean in an Expr tree.

func (*BoolLeaf) String

func (l *BoolLeaf) String() string

type Expr

type Expr interface {
	// Opcode gives the node's operator, which determines the detailed structure.
	// Useful to avoid .(type) for simple routing.
	Opcode() Op

	// IsLeaf is Opcode().IsLeaf() for convenience. If !IsLeaf(), it's an Inner operator.
	IsLeaf() bool

	String() string
}

Expr represents an arbitrary expression tree; it can be converted to one of the ...Leaf types or Inner, depending on Opcode, or using a type switch on an Expr value. Note that Expr satisfies Val, and can appear directly as a value or argument in a Path. Since it is not a constant value, it is not a Valuer.

func ParseScriptExpression

func ParseScriptExpression(s string) (Expr, error)

ParseScriptExpression gives direct access to the secondary parser for expressions, returning an Expr tree representing the expression in s.

type FloatLeaf

type FloatLeaf struct {
	Op
	Val float64
}

FloatLeaf represents a floating-point number in an Expr tree.

func (*FloatLeaf) String

func (l *FloatLeaf) String() string

type Inner

type Inner struct {
	Op
	Kids []Expr
}

Inner represents an interior operation with one or more operands.

func (*Inner) Kid

func (i *Inner) Kid(c int) (Expr, bool)

Kid returns child c (index c) and true, or nil and false if the child doesn't exist.

func (*Inner) String

func (i *Inner) String() string

type IntLeaf

type IntLeaf struct {
	Op
	Val int64
}

IntLeaf represents an integer in an Expr tree.

func (*IntLeaf) String

func (l *IntLeaf) String() string

type IntVal

type IntVal int64

IntVal represents an integer value, satisfying Val.

func (IntVal) String

func (v IntVal) String() string

func (IntVal) V

func (v IntVal) V() int64

V returns the underlying integer.

func (IntVal) Value

func (v IntVal) Value() JSON

Value satisfies Valuer, boxing a plain integer.

type JSON

type JSON = interface{}

JSON is a synonym for the interface{} structures returned by encoding/json, used as values in the JSON machine, to make it clear that's what they are.

type Loc

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

Loc is a location (line and byte offset)

type NameLeaf

type NameLeaf struct {
	Op
	Name string
}

NameLeaf represents a user-defined name (OpID), "@" (OpCurrent) and "$" (OpRoot) in an Expr tree.

func (*NameLeaf) String

func (l *NameLeaf) String() string

type NameVal

type NameVal string

NameVal represents a key or JSON member name as a value, satisfying Val.

func (NameVal) S

func (v NameVal) S() string

S returns the name as a plain string.

func (NameVal) String

func (v NameVal) String() string

func (NameVal) Value

func (v NameVal) Value() JSON

Value satisfies Valuer, boxing the Nameval itself, to distinguish a string that's an identifier from ordinary strings.

type NullLeaf

type NullLeaf struct {
	Op
}

NullLeaf represents JS "null" in an Expr tree.

func (*NullLeaf) String

func (l *NullLeaf) String() string

type Op

type Op int
const (
	OpError  Op = iota // illegal token, deliberately the same as the zero value
	OpInt              // integer
	OpBool             // true, false
	OpID               // identifier
	OpReal             // real number (might be used in expressions)
	OpString           // single- or double-quoted string
	OpRE               // /re/
	OpNull             // JS "null"
	OpBounds           // [lb: ub: stride]

	// path operators
	OpMember // . used for path selection (single int, key or expr)
	OpSelect // [] for path selection (single int, string, expr or filter
	OpUnion  // [union-element, union-element ...]
	OpWild   // *
	OpFilter // ?(...)
	OpExp    // (...)

	// path iteration operators
	OpFor  // start of OpFilter sequence, selecting on output candidates
	OpNest // start of OpNest* sequence, selecting on dot
	OpRep  // repeat sequence if values left

	// path nest operators
	OpNestMember // .. member
	OpNestSelect // .. [subscript]
	OpNestUnion  // .. [key1, key2, ...]
	OpNestWild   // .. [*]
	OpNestFilter // .. [?(expr)]

	// expression operators, in both filters and "expression engines"
	OpRoot    // $ (use root as operand)
	OpCurrent // @ (use current candidate as operand)
	OpDot     // . field selection (in an expression)
	OpIndex   // [] indexing an array
	OpSlice   // [lb: ub: stride] slice operator on array value
	OpLT      // <
	OpLE      // <=
	OpEQ      // = or ==
	OpNE      // !=
	OpGE      // >=
	OpGT      // >
	OpAnd     // &&
	OpOr      // ||
	OpMul     // *
	OpDiv     // /
	OpMod     // %
	OpNeg     // unary -
	OpAdd     // +
	OpSub     // binary -
	OpCall    // function call id(args)
	OpArray   // [e-list]
	OpIn      // "in"
	OpNin     // "nin", not in
	OpMatch   // =~ (why not just ~)
	OpNot     // unary !
)

func (Op) GoString

func (o Op) GoString() string

GoString returns the internal name of Op o, for debugging.

func (Op) HasVal

func (o Op) HasVal() bool

HasVal returns true if o is a leaf operator that carries a value.

func (Op) IsLeaf

func (o Op) IsLeaf() bool

IsLeaf returns true if o is a leaf operator.

func (Op) Opcode

func (o Op) Opcode() Op

Opcode returns the op value itself, so Op embedded in an operation satisfies the Expr interface.

func (Op) String

func (o Op) String() string

String returns a source-level representation of Op o for diagnostics.

type Path

type Path []*Step

Path is a sequence of Steps, starting from "$" (the document root), following the grammar. The initial "$" has no explicit representation in the Path: it's the starting point.

func ParsePath

func ParsePath(s string) (Path, error)

ParsePath returns the parsed form of the path expression in s, or an error.

type RegexpLeaf

type RegexpLeaf struct {
	Op
	Pattern string         // Pattern is the text of the expression.
	Prog    *regexp.Regexp // Prog is the compiled version of the same.
}

RegexpLeaf represents the text of a regular expression in an Expr tree.

func (*RegexpLeaf) String

func (l *RegexpLeaf) String() string

type Slice

type Slice struct {
	Start  Val // optional starting offset
	End    Val // optional end offset (exclusive)
	Stride Val // optional value selecting every n array elements.
}

Slice represents a JavaScript slice with [start: end: stride], where any of them might be optional (nil). *Slice satisfies Val.

func (*Slice) String

func (slice *Slice) String() string

func (*Slice) Value

func (s *Slice) Value() JSON

Value satisfies Valuer, boxing the slice (pointer).

type Step

type Step struct {
	Op   Op    // Op is the action to take at this step. Not all Ops are valid Steps (eg, expression operators).
	Args []Val // Zero or more arguments to the operation (eg, integer and string values, an identifier, a Slice or a filter or other Expr).
}

Step represents a single step in the path: an operation with zero or more parameters, each represented by a Val, which is either a constant (signed integer, string, or member name) or an Expr to be evaluated.

func (*Step) String

func (step *Step) String() string

type StringLeaf

type StringLeaf struct {
	Op
	Val string
}

StringLeaf represents the value of a single- or double-quoted string in an Expr tree.

func (*StringLeaf) String

func (l *StringLeaf) String() string

type StringVal

type StringVal string

StringVal represents a string value, satisfying Val.

func (StringVal) S

func (v StringVal) S() string

S returns the string value unwrapped.

func (StringVal) String

func (v StringVal) String() string

func (StringVal) Value

func (v StringVal) Value() JSON

Value satisfies Valuer, boxing an ordinary string.

type Val

type Val interface {
	String() string // String returns a text representation, mainly for tracing and testing.
}

Val is an int64, float64, string literal, name, bool?, *Slice or Expr as a value (see IntVal etc below), or nil as a missing value. It represents a parameter to a Path Step, or a value compiled into a Program from a leaf of an expression tree. Types that satisfy Val correspond to elements in the JsonPath grammar (ie, a union type in its abstract syntax tree):

Val = Int | String | Name | Slice | Float | Regexp
Slice = Lb: Val? Ub: Val? Stride: Val?

Originally, it was also the memory type for the Program machine, but because that also includes JSON trees, it was clearer just to use the JSON type (ie, interface{}) directly, hence the addition of Valuer, satisfied by constant values.

Other files in this package add their own Val variants.

type Valuer

type Valuer interface {
	Value() JSON // Value returns a suitable internal representation for use by the machine.
}

Valuer instances return an underlying constant value boxed as JSON (which can then be inspected by type switch). Note that the JSON returned by Value either wraps an ordinary Go type (eg, int64, string), or is simply the original value, boxed, allowing it to continue to be distinguished by type switch. See NameVal for an example. Not all Vals are Valuers: for instance, Expr has no underlying constant value.

Jump to

Keyboard shortcuts

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