Documentation
¶
Overview ¶
Package jsonata provides a pure-Go implementation of the JSONata 2.2 expression language, compatible with jsonata-js v2.2.2.
Compile creates an immutable expression that can be evaluated repeatedly. The compatibility API is designed around Compile, MustCompile, Expr.Eval, Expr.EvalBytes, RegisterExts, RegisterVars, and Extension. EvalWithOptions, EvalContext, and Engine provide context-aware and instance-scoped evaluation with per-call bindings and resource limits.
Evaluation accepts ordinary Go JSON values and returns ordinary Go JSON values. Inputs, bindings, and extension results are normalized and copied for evaluation; cyclic or unsupported values return an error. Compiled expressions and engines are safe for concurrent use, but extension code must provide its own synchronization for shared state.
The package follows semantic versioning. The JSONata language baseline is jsonata-js v2.2.2; the exact reference revision used for conformance is recorded by the conformance report.
Index ¶
- Variables
- func Eval(expression string, data interface{}) (interface{}, error)
- func EvalBytes(expression string, data []byte) ([]byte, error)
- func EvalBytesWithOptions(expression string, data []byte, options EvalOptions) ([]byte, error)
- func EvalNoInput(expression string) (any, error)
- func EvalNoInputWithOptions(expression string, options EvalOptions) (any, error)
- func EvalWithOptions(expression string, data any, options EvalOptions) (any, error)
- func RegisterExts(exts map[string]Extension) error
- func RegisterVars(vars map[string]interface{}) error
- type ArgCountError
- type ArgTypeError
- type Engine
- func (e *Engine) Compile(expression string) (*Expr, error)
- func (e *Engine) Eval(ctx context.Context, expression string, input any) (any, error)
- func (e *Engine) Evaluate(ctx context.Context, expr *Expr, input any) (any, error)
- func (e *Engine) RegisterExts(exts map[string]Extension) error
- func (e *Engine) RegisterVars(vars map[string]interface{}) error
- type ErrType
- type Error
- type EvalContext
- type EvalError
- type EvalOptions
- type Expr
- func (e *Expr) Eval(data interface{}) (interface{}, error)
- func (e *Expr) EvalBindings(data interface{}, bindings map[string]any) (interface{}, error)
- func (e *Expr) EvalBytes(data []byte) ([]byte, error)
- func (e *Expr) EvalBytesWithOptions(data []byte, options EvalOptions) ([]byte, error)
- func (e *Expr) EvalContext(ctx context.Context, input any) (any, error)
- func (e *Expr) EvalNoInput() (any, error)
- func (e *Expr) EvalNoInputBindings(bindings map[string]any) (any, error)
- func (e *Expr) EvalNoInputWithOptions(options EvalOptions) (any, error)
- func (e *Expr) EvalWithOptions(data any, options EvalOptions) (any, error)
- func (e *Expr) RegisterExts(exts map[string]Extension) error
- func (e *Expr) RegisterVars(vars map[string]interface{}) error
- func (e *Expr) String() string
- type Extension
- type JSONataError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUndefined = errors.New("no results found")
ErrUndefined is returned when an expression produces JSONata's empty sequence. It is identical in value and text to blues/jsonata-go's sentinel.
Functions ¶
func EvalBytes ¶
EvalBytes compiles and evaluates expression against one JSON value.
Example ¶
package main
import (
"fmt"
jsonata "github.com/tiaanduplessis/jsonata-go"
)
func main() {
result, err := jsonata.EvalBytes(
`$sum(items.price)`,
[]byte(`{"items":[{"price":12},{"price":30}]}`),
)
if err != nil {
panic(err)
}
fmt.Println(string(result))
}
Output: 42
func EvalBytesWithOptions ¶
func EvalBytesWithOptions(expression string, data []byte, options EvalOptions) ([]byte, error)
EvalBytesWithOptions compiles and evaluates one JSON document with options.
func EvalNoInput ¶
EvalNoInput compiles and evaluates an expression without an input value.
func EvalNoInputWithOptions ¶
func EvalNoInputWithOptions(expression string, options EvalOptions) (any, error)
EvalNoInputWithOptions compiles and evaluates an expression without an input value and with per-call controls.
func EvalWithOptions ¶
func EvalWithOptions(expression string, data any, options EvalOptions) (any, error)
EvalWithOptions compiles and evaluates an expression with per-call controls.
func RegisterExts ¶
RegisterExts registers extensions for expressions compiled afterward.
func RegisterVars ¶
RegisterVars registers variables for expressions compiled afterward.
Types ¶
type ArgCountError ¶
ArgCountError reports a legacy extension call with the wrong arity.
func (ArgCountError) Error ¶
func (e ArgCountError) Error() string
type ArgTypeError ¶
ArgTypeError reports a legacy extension argument that did not match the Go function signature. Which is one-based.
func (ArgTypeError) Error ¶
func (e ArgTypeError) Error() string
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is an instance-scoped entry point with a copy-on-write registry. Its zero value is ready for concurrent use.
Example ¶
package main
import (
"context"
"errors"
"fmt"
jsonata "github.com/tiaanduplessis/jsonata-go"
)
func main() {
first := jsonata.NewEngine()
second := jsonata.NewEngine()
if err := first.RegisterVars(map[string]interface{}{"tenant": "one"}); err != nil {
panic(err)
}
firstValue, err := first.Eval(context.Background(), `$tenant`, nil)
if err != nil {
panic(err)
}
secondValue, secondErr := second.Eval(context.Background(), `$tenant`, nil)
fmt.Println(firstValue)
fmt.Println(secondValue == nil, errors.Is(secondErr, jsonata.ErrUndefined))
}
Output: one true true
func (*Engine) RegisterExts ¶
RegisterExts registers extensions for expressions subsequently compiled by this engine.
func (*Engine) RegisterVars ¶
RegisterVars registers variables for expressions subsequently compiled by this engine.
type ErrType ¶
type ErrType uint
ErrType identifies the reason for a legacy-compatible evaluation error.
const ( ErrNonIntegerLHS ErrType = iota ErrNonIntegerRHS ErrNonNumberLHS ErrNonNumberRHS ErrNonComparableLHS ErrNonComparableRHS ErrTypeMismatch ErrNonCallable ErrNonCallableApply ErrNonCallablePartial ErrNumberInf ErrNumberNaN ErrMaxRangeItems ErrIllegalKey ErrDuplicateKey ErrClone ErrIllegalUpdate ErrIllegalDelete ErrNonSortable ErrSortMismatch )
Legacy evaluation error types retained for source compatibility with blues/jsonata-go v1.5.4.
type Error ¶
type Error struct {
Code string
Token string
Value any
Position int
Message string
// contains filtered or unexported fields
}
Error is a structured JSONata syntax or evaluation error. Position is a one-based UTF-16 code-unit offset into the expression. Value retains the JSONata value supplied by the reference diagnostic, including null, bool, and number values.
Example ¶
package main
import (
"context"
"errors"
"fmt"
jsonata "github.com/tiaanduplessis/jsonata-go"
)
func main() {
canceled, cancel := context.WithCancel(context.Background())
cancel()
_, err := jsonata.MustCompile(`1 + 1`).EvalContext(canceled, nil)
var structured *jsonata.Error
if errors.As(err, &structured) {
fmt.Println(structured.Code, errors.Is(err, context.Canceled))
}
}
Output: U1001 true
func (Error) As ¶
As preserves the legacy EvalError inspection surface for codes whose meaning is unambiguous in the modern JSONata diagnostics.
func (Error) JSONataCode ¶
JSONataCode lets conformance and embedding code inspect an error without depending on the concrete package type.
type EvalContext ¶
EvalContext is a compatibility convenience container for an input and its context. Use EvalOptions with EvalWithOptions for bindings and limits.
func NewEvalContext ¶
func NewEvalContext(ctx context.Context, input any) EvalContext
type EvalError ¶
EvalError preserves the exported legacy error shape. New code should prefer Error, which exposes JSONata 2.2 error codes and source positions.
type EvalOptions ¶
type EvalOptions struct {
Context context.Context
Bindings map[string]any
Timeout time.Duration
// MaxCallDepth defaults to 100 when zero or negative.
MaxCallDepth int
// MaxOperations defaults to 100,000 when zero or negative.
MaxOperations int64
// MaxSequenceLength limits JSONata sequences when positive. Zero or negative
// disables this optional v2.2 guardrail.
MaxSequenceLength int
}
EvalOptions controls one evaluation without changing the compatibility wrappers. Bindings are copied per call and are never retained by Expr.
type Expr ¶
type Expr struct {
// contains filtered or unexported fields
}
Expr contains immutable compiled syntax and a copy-on-write registration snapshot. It may be registered and evaluated concurrently.
func Compile ¶
Example ¶
package main
import (
"fmt"
jsonata "github.com/tiaanduplessis/jsonata-go"
)
func main() {
expression, err := jsonata.Compile(`Account.Name`)
if err != nil {
panic(err)
}
value, err := expression.Eval(map[string]any{
"Account": map[string]any{"Name": "Ada"},
})
if err != nil {
panic(err)
}
fmt.Println(value)
}
Output: Ada
func MustCompile ¶
func (*Expr) EvalBindings ¶
EvalBindings evaluates an immutable expression with per-call variable bindings. The bindings are copied by the evaluator and never stored on Expr.
func (*Expr) EvalBytesWithOptions ¶
func (e *Expr) EvalBytesWithOptions(data []byte, options EvalOptions) ([]byte, error)
EvalBytesWithOptions evaluates one JSON document with per-call controls.
func (*Expr) EvalNoInput ¶
EvalNoInput evaluates against JSONata's empty input sequence. Eval(nil) remains the explicit JSON null input for compatibility.
func (*Expr) EvalNoInputBindings ¶
EvalNoInputBindings evaluates against the empty input sequence with per-call variable bindings.
func (*Expr) EvalNoInputWithOptions ¶
func (e *Expr) EvalNoInputWithOptions(options EvalOptions) (any, error)
EvalNoInputWithOptions evaluates against the empty input sequence with per-call bindings and limits.
func (*Expr) EvalWithOptions ¶
func (e *Expr) EvalWithOptions(data any, options EvalOptions) (any, error)
EvalWithOptions evaluates an immutable expression with per-call controls.
func (*Expr) RegisterExts ¶
RegisterExts registers extensions only for this compiled expression.
Example ¶
package main
import (
"fmt"
"strings"
jsonata "github.com/tiaanduplessis/jsonata-go"
)
func main() {
expression := jsonata.MustCompile(`$uppercase("beneath the underdog")`)
err := expression.RegisterExts(map[string]jsonata.Extension{
"uppercase": {Func: strings.ToUpper},
})
if err != nil {
panic(err)
}
result, err := expression.Eval(nil)
if err != nil {
panic(err)
}
fmt.Println(result)
}
Output: BENEATH THE UNDERDOG
func (*Expr) RegisterVars ¶
RegisterVars registers variables only for this compiled expression.
type Extension ¶
type Extension struct {
Func interface{}
UndefinedHandler func([]reflect.Value) bool
EvalContextHandler func([]reflect.Value) bool
}
Extension describes a Go function exposed to JSONata.
type JSONataError ¶
type JSONataError = Error
JSONataError is the descriptive name for Error; both names are supported.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
benchmarkprofile
command
Command benchmarkprofile captures reproducible runtime profiles and their complete source and host metadata.
|
Command benchmarkprofile captures reproducible runtime profiles and their complete source and host metadata. |
|
benchmarkreport
command
Command benchmarkreport validates raw benchmark evidence and renders the scoped statistical claim report.
|
Command benchmarkreport validates raw benchmark evidence and renders the scoped statistical claim report. |
|
benchmarkrun
command
Command benchmarkrun collects repeated, correctness-gated benchmark output.
|
Command benchmarkrun collects repeated, correctness-gated benchmark output. |
|
benchverify
command
Command benchverify proves benchmark eligibility against the pinned oracle.
|
Command benchverify proves benchmark eligibility against the pinned oracle. |
|
conformance
command
Command conformance runs the pinned JSONata language-neutral suite and writes a deterministic machine-readable report.
|
Command conformance runs the pinned JSONata language-neutral suite and writes a deterministic machine-readable report. |
|
differential
command
|
|
|
internal
|
|
|
benchmark
Package benchmark contains the correctness-gated benchmark harness.
|
Package benchmark contains the correctness-gated benchmark harness. |
|
conformance
Package conformance provides deterministic loading and execution of the pinned, language-neutral JSONata test suite.
|
Package conformance provides deterministic loading and execution of the pinned, language-neutral JSONata test suite. |
|
differential
Package differential defines the deterministic compatibility corpus used to compare this implementation with the pinned jsonata-js reference.
|
Package differential defines the deterministic compatibility corpus used to compare this implementation with the pinned jsonata-js reference. |
|
evaluator
Package evaluator contains the pure, per-call JSONata evaluator.
|
Package evaluator contains the pure, per-call JSONata evaluator. |
|
regex
Package regex provides the evaluator's ECMAScript regular-expression boundary.
|
Package regex provides the evaluator's ECMAScript regular-expression boundary. |
|
syntax
Package syntax implements the immutable JSONata syntax tree and parser.
|
Package syntax implements the immutable JSONata syntax tree and parser. |
|
value
Package value contains the evaluator's private JSONata value model.
|
Package value contains the evaluator's private JSONata value model. |
|
version
Package version contains build and release metadata for jsonata-go.
|
Package version contains build and release metadata for jsonata-go. |
|
Package jlib implements the JSONata function library.
|
Package jlib implements the JSONata function library. |
|
jxpath
Package jxpath provides the XPath-compatible date and number formatting helpers used by the JSONata function library.
|
Package jxpath provides the XPath-compatible date and number formatting helpers used by the JSONata function library. |
|
Package jparse converts JSONata expressions to abstract syntax trees.
|
Package jparse converts JSONata expressions to abstract syntax trees. |
|
Package jtypes contains the small reflection-facing compatibility surface used by JSONata extension functions.
|
Package jtypes contains the small reflection-facing compatibility surface used by JSONata extension functions. |