Documentation
¶
Overview ¶
Package jqlite is the Go port of jqlite, the profile of the jq language specified in spec/jqlite.md: parse a program once into an Expression, evaluate it many times, from many goroutines. This package is the host boundary; the engine is in internal/.
Index ¶
- Constants
- Variables
- func EvaluateLines(expression string, r io.Reader, variables map[string]Value) iter.Seq2[Value, error]
- func ToAny(v Value) any
- func ToJSON(v Value) string
- type Array
- type Error
- type Expression
- func (e *Expression) Evaluate(input Value, variables map[string]Value) ([]Value, error)
- func (e *Expression) EvaluateFunc(input Value, variables map[string]Value, onResult func(Value) bool) error
- func (e *Expression) Lines(r io.Reader, variables map[string]Value) iter.Seq2[Value, error]
- func (e *Expression) Results(input Value, variables map[string]Value) iter.Seq2[Value, error]
- type FSResolver
- type FileResolver
- type HaltError
- type Kind
- type Member
- type ModuleKind
- type Object
- type Options
- type ParseError
- type Resolver
- type Value
- func Bool(b bool) Value
- func Evaluate(expression string, input Value, variables map[string]Value) ([]Value, error)
- func FromAny(v any) (Value, error)
- func NewArray(items ...Value) Value
- func NewObject(members ...Member) Value
- func Number(n float64) Value
- func ParseJSON(text string) (Value, error)
- func String(s string) Value
- type VariableError
Constants ¶
const ( KindNull = values.KindNull KindFalse = values.KindFalse KindTrue = values.KindTrue KindNumber = values.KindNumber KindString = values.KindString KindArray = values.KindArray KindObject = values.KindObject )
The kinds of values.
Variables ¶
Null, False and True are the three constant values.
Functions ¶
func EvaluateLines ¶
func EvaluateLines(expression string, r io.Reader, variables map[string]Value) iter.Seq2[Value, error]
EvaluateLines parses expression with the default options and evaluates it on each value of a JSON Lines stream, as Expression.Lines; a parse error is the first and only element.
Types ¶
type Array ¶
Array is the array of a Value (Count, At); Object is its object (Count, KeyAt, ValueAt, TryGetValue, in insertion order).
type Error ¶
Error is an error that reached the top of an expression: Value is the error value (what error(v) raised, or the message string of a builtin's error). The Message is the value itself when it is a string, else its JSON text followed by " (not a string)" (spec §10.1).
type Expression ¶
type Expression struct {
// contains filtered or unexported fields
}
Expression is a jq expression parsed and bound once by Parse, to evaluate on any number of inputs. It is immutable and safe to evaluate concurrently from many goroutines, each evaluation with its own input and variables.
func MustParse ¶
func MustParse(expression string) *Expression
MustParse is Parse for expressions known to be valid: it panics on an error.
func Parse ¶
func Parse(expression string) (*Expression, error)
Parse parses and binds expression with the default options (no modules). An empty expression is `.`. The error is a *ParseError.
func ParseWithOptions ¶
func ParseWithOptions(expression string, options Options) (*Expression, error)
ParseWithOptions parses and binds expression, loading its modules through the options' resolver, into an expression to evaluate any number of times, concurrently if need be. The error is a *ParseError.
func ParseWithResolver ¶
func ParseWithResolver(expression string, resolver Resolver) (*Expression, error)
ParseWithResolver parses and binds expression, loading its modules through resolver, with the default options otherwise.
func (*Expression) Evaluate ¶
Evaluate evaluates the expression on input and gives all its results. variables are the host variables, by name without '$' ("root" is $root): each name a jq identifier, ENV excluded (a *VariableError). A variable the expression uses but the host does not give is the runtime error "$name is not defined"; a binding in the expression shadows a host variable. When an error reaches the top of the expression (an *Error, or a *HaltError for halt_error), the results are nil.
func (*Expression) EvaluateFunc ¶
func (e *Expression) EvaluateFunc(input Value, variables map[string]Value, onResult func(Value) bool) error
EvaluateFunc evaluates the expression on input, giving each result to onResult as soon as it is produced; onResult gives false to stop the evaluation (stopping is not an error). An error that reaches the top of the expression comes after the results produced before it.
func (*Expression) Lines ¶
Lines evaluates the expression on each value of a JSON Lines stream in turn (one JSON value per line, blank lines skipped), yielding the results of each; the results of one input are computed before they are yielded. Each value is evaluated alone (spec §12): halt ends only the evaluation of its value. A line that is not JSON, an error that reaches the top of the expression or an error of the reader is yielded with Null and ends the sequence; breaking out of the loop stops reading.
type FSResolver ¶
FSResolver is a Resolver over an fs.FS (an embed.FS, for modules built into the program): a path is relative to the directory of the importing module, or to Root for the expression itself, and gets the extension .jq or .json when it has none. The canonical path is the path in the file system.
func (*FSResolver) CanonicalPath ¶
func (r *FSResolver) CanonicalPath(p string, kind ModuleKind, _ *Value, fromPath string) (string, error)
func (*FSResolver) Open ¶
func (r *FSResolver) Open(canonicalPath string) (io.ReadCloser, error)
type FileResolver ¶
type FileResolver struct {
BaseDirectory string
}
FileResolver is a Resolver over the file system: a path is relative to the directory of the importing module, or to BaseDirectory for the expression itself, and gets the extension .jq (code) or .json (data) when it has none. The canonical path is the absolute file path.
func NewFileResolver ¶
func NewFileResolver(baseDirectory string) *FileResolver
NewFileResolver gives a resolver whose expressions' own directives are relative to baseDirectory.
func (*FileResolver) CanonicalPath ¶
func (r *FileResolver) CanonicalPath(p string, kind ModuleKind, _ *Value, fromPath string) (string, error)
func (*FileResolver) Open ¶
func (r *FileResolver) Open(canonicalPath string) (io.ReadCloser, error)
type HaltError ¶
HaltError is halt_error ending an evaluation: Value is the value it was given and ExitCode its code. (halt ends an evaluation normally, without an error.)
type ModuleKind ¶
type ModuleKind int
ModuleKind tells whether an import asks for a code module (.jq) or a data module (import "p" as $name;, JSON values).
const ( ModuleCode ModuleKind = iota ModuleData )
The kinds of modules.
type Object ¶
Array is the array of a Value (Count, At); Object is its object (Count, KeyAt, ValueAt, TryGetValue, in insertion order).
type Options ¶
type Options struct {
// Resolver resolves import and include directives and modulemeta; without one, no module exists.
Resolver Resolver
// Environment is the variables of $ENV and env, as "NAME=value" entries in this order (the form
// of os.Environ). When nil (the default), the process environment, read when an evaluation first
// uses $ENV; an empty, non-nil slice is an empty $ENV.
Environment []string
// Now is the source of now; nil is time.Now.
Now func() time.Time
// Location is the local time zone of localtime, strflocaltime, %z and %Z; nil is time.Local. A
// zone's name (time.Time.Zone) is the abbreviation of %Z; an empty name prints the offset.
Location *time.Location
// MaxDepth is the maximum number of calls of jq-defined functions active at once (spec §11);
// beyond it, the catchable error "Maximum call depth exceeded". Deeper recursion never exhausts
// the host stack. 0 is the default, 1000.
MaxDepth int
// Debug receives the value of each debug (its input) and each message of debug(msgs), as is.
// Without one, the values are dropped.
Debug func(Value)
// Stderr receives the input of each stderr, as is. Without one, the values are dropped.
Stderr func(Value)
}
Options is what an expression takes from its host besides the input and the variables: modules, $ENV, the clock and zone, the depth limit and the handlers of debug and stderr. Options are read when the expression is parsed and apply to every evaluation of it; the zero Options are the defaults.
type ParseError ¶
type ParseError struct {
// Message is the error with where it is: "<message> at [<module>, ]line L, column C".
Message string
// Line and Column are 1-based, in the expression or in the module Module (when not empty);
// columns count code points.
Line int
Column int
Module string
}
ParseError is a syntax or binding error, given by Parse before any evaluation: an unknown function, label or module, a cycle of imports.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
type Resolver ¶
type Resolver interface {
// CanonicalPath gives the canonical path of the module path of the given kind, imported with
// metadata (the directive's metadata object, or nil) from the module at fromPath (a canonical
// path, or "" for the expression itself); "" when there is no such module. The same canonical
// path means the same module.
CanonicalPath(path string, kind ModuleKind, metadata *Value, fromPath string) (string, error)
// Open opens the module at a canonical path that CanonicalPath gave.
Open(canonicalPath string) (io.ReadCloser, error)
}
Resolver finds the modules of import and include directives and of modulemeta: maps a path, as written, to a canonical path (the module's identity), then opens it. A module is read once per canonical path while an expression is parsed. modulemeta calls the resolver while expressions are evaluated, possibly from several goroutines at once: implementations must allow that.
type Value ¶
Value is a jqlite value: null, false, true, a number (float64), a string, an array or an object whose keys keep their insertion order. Values are immutable and safe to share between goroutines. The zero Value is null.
Go has no standard ordered JSON value, so the host boundary exposes the engine's own value type, with conversions from and to JSON text (ParseJSON, ToJSON) and from and to Go's any (FromAny, ToAny).
func Evaluate ¶
Evaluate parses expression with the default options and evaluates it on input, giving all its results.
func FromAny ¶
FromAny converts a Go value: nil, bool, the integer and floating-point types, string, json.Number, a Value, []any (and any slice), map[string]any (and any map with string keys, keys sorted, since Go maps have no order). Anything else is an error.
func NewObject ¶
NewObject gives an object of the members, in their order; a repeated key keeps its first position and its last value.
type VariableError ¶
type VariableError struct {
Name string
}
VariableError is a host variable name that is not valid (spec §12): not a jq identifier, or ENV.
func (*VariableError) Error ¶
func (e *VariableError) Error() string
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
bench
command
Command bench is the runner of the cross-language benchmark (bench/README.md): the same command line, the same three measurements and the same line of output in every port.
|
Command bench is the runner of the cross-language benchmark (bench/README.md): the same command line, the same three measurements and the same line of output in every port. |
|
internal
|
|
|
binding
Package binding resolves the names of a parsed program (spec §7) into the bound program the interpreter evaluates, loads modules (§9.11) and holds the prelude and the native catalogue.
|
Package binding resolves the names of a parsed program (spec §7) into the bound program the interpreter evaluates, loads modules (§9.11) and holds the prelude and the native catalogue. |
|
builtins
Package builtins implements the natives of spec §9 and registers them into runtime.NativeTable when it is initialized.
|
Package builtins implements the natives of spec §9 and registers them into runtime.NativeTable when it is initialized. |
|
corpus
Package corpus reads the conformance corpus of spec/tests (format in spec/tests/README.md) for the tests of every layer.
|
Package corpus reads the conformance corpus of spec/tests (format in spec/tests/README.md) for the tests of every layer. |
|
corpus/modules
Package modules holds the module sources of the tests: a directory with the path rule of spec/tests/README.md, and a map of texts.
|
Package modules holds the module sources of the tests: a directory with the path rule of spec/tests/README.md, and a map of texts. |
|
runtime
Package runtime is the push-based interpreter of a bound program (spec §6 to §8), its environments, paths, signals, the host interface (evaluation.go) and the native table.
|
Package runtime is the push-based interpreter of a bound program (spec §6 to §8), its environments, paths, signals, the host interface (evaluation.go) and the native table. |
|
syntax
Package syntax holds the lexer, the AST and the parser of jqlite (spec §2, §3).
|
Package syntax holds the lexer, the AST and the parser of jqlite (spec §2, §3). |
|
values
Package values holds the jqlite value, its arrays and objects, and the operations on values: equality, ordering, arithmetic (value_ops.go), the JSON writer and reader (value_text.go) and every run-time message (errors.go).
|
Package values holds the jqlite value, its arrays and objects, and the operations on values: equality, ordering, arithmetic (value_ops.go), the JSON writer and reader (value_text.go) and every run-time message (errors.go). |