Documentation
¶
Overview ¶
Package gosonata provides a high-performance Go implementation of JSONata 2.1.0+.
JSONata is a lightweight query and transformation language for JSON data. GoSonata is designed for intensive data streaming scenarios with focus on:
- Performance: Optimized parser and evaluator
- Concurrency: Native goroutine support
- Streaming: Handle large JSON documents efficiently
- Conformance: 100% compatibility with JSONata 2.1.0+ spec
Quick Start ¶
// Simple evaluation
result, err := gosonata.Eval("$.name", data)
// Compile once, evaluate many times
expr, err := gosonata.Compile("$.items[price > 100]")
ev := evaluator.New()
result1, _ := ev.Eval(ctx, expr, data1)
result2, _ := ev.Eval(ctx, expr, data2)
// With options
result, err := gosonata.Eval("$.items", data,
gosonata.WithCaching(true),
gosonata.WithTimeout(5*time.Second),
)
Performance ¶
GoSonata is optimized for:
- Fast parsing with hand-written recursive descent parser
- Efficient evaluation with minimal allocations
- Concurrent evaluation for independent expressions
- Optional caching for repeated queries
- Streaming support for large documents
Conformance ¶
GoSonata aims for 100% compatibility with JSONata 2.1.0+ specification, passing the complete official test suite (91+ test groups).
More Information ¶
For detailed documentation, see:
- Parser: github.com/sandrolain/gosonata/pkg/parser
- Evaluator: github.com/sandrolain/gosonata/pkg/evaluator
- Functions: github.com/sandrolain/gosonata/pkg/functions
- Types: github.com/sandrolain/gosonata/pkg/types
Index ¶
- func Compile(query string, opts ...parser.CompileOption) (*types.Expression, error)
- func Eval(query string, data interface{}, opts ...evaluator.EvalOption) (interface{}, error)
- func EvalBytes(query string, raw json.RawMessage, opts ...EvalOption) (any, error)
- func EvalBytesWithContext(ctx context.Context, query string, raw json.RawMessage, opts ...EvalOption) (any, error)
- func EvalStream(ctx context.Context, query string, r io.Reader, opts ...EvalOption) (<-chan StreamResult, error)
- func EvalWithContext(ctx context.Context, query string, data interface{}, ...) (interface{}, error)
- func MustCompile(query string) *types.Expression
- func Version() string
- type AdvancedCustomFunc
- type AdvancedCustomFunctionDef
- type CustomFunc
- type CustomFunctionDef
- type EvalOption
- func WithCacheSize(size int) EvalOption
- func WithCaching(enabled bool) EvalOption
- func WithConcurrency(enabled bool) EvalOption
- func WithCustomFunction(name, signature string, fn CustomFunc) EvalOption
- func WithDebug(enabled bool) EvalOption
- func WithFunctions(defs ...functions.FunctionEntry) EvalOption
- func WithTimeout(t time.Duration) EvalOption
- type FunctionEntry
- type StreamResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compile ¶
func Compile(query string, opts ...parser.CompileOption) (*types.Expression, error)
Compile compiles a JSONata expression for repeated evaluation.
The compiled expression can be evaluated multiple times against different data. It is safe for concurrent use.
Example:
expr, err := gosonata.Compile("$.items[price > 100]")
if err != nil {
log.Fatal(err)
}
result, _ := expr.Eval(ctx, data)
func Eval ¶
func Eval(query string, data interface{}, opts ...evaluator.EvalOption) (interface{}, error)
Eval is a convenience function that compiles and evaluates an expression in a single call.
For repeated evaluations of the same expression, use Compile instead. If WithCaching(true) is passed in opts, the compiled expression is cached and reused on subsequent calls with the same query string.
Example:
result, err := gosonata.Eval("$.name", data)
func EvalBytes ¶
func EvalBytes(query string, raw json.RawMessage, opts ...EvalOption) (any, error)
EvalBytes is a convenience function that compiles query and evaluates it against raw JSON bytes in a single call.
For expressions eligible for the zero-copy fast path (simple field access, equality comparisons, and supported stdlib functions), this avoids a full json.Unmarshal. Call expr.IsFastPath() on a pre-compiled expression to check whether the fast path applies.
Example:
result, err := gosonata.EvalBytes(`user.email`, rawJSON)
func EvalBytesWithContext ¶
func EvalBytesWithContext(ctx context.Context, query string, raw json.RawMessage, opts ...EvalOption) (any, error)
EvalBytesWithContext evaluates an expression against raw JSON bytes with a custom context.
func EvalStream ¶
func EvalStream(ctx context.Context, query string, r io.Reader, opts ...EvalOption) (<-chan StreamResult, error)
EvalStream compiles query and evaluates it against each JSON value read from r.
It is a convenience wrapper around Compile + Evaluator.EvalStream. See evaluator.EvalStream for full documentation.
func EvalWithContext ¶
func EvalWithContext(ctx context.Context, query string, data interface{}, opts ...evaluator.EvalOption) (interface{}, error)
EvalWithContext evaluates an expression with a custom context.
func MustCompile ¶
func MustCompile(query string) *types.Expression
MustCompile is like Compile but panics if the expression cannot be compiled. It simplifies safe initialization of global variables.
Types ¶
type AdvancedCustomFunc ¶
type AdvancedCustomFunc = functions.AdvancedCustomFunc
AdvancedCustomFunc is the signature for higher-order user-defined functions that receive a Caller to invoke function arguments from JSONata expressions.
type AdvancedCustomFunctionDef ¶
type AdvancedCustomFunctionDef = functions.AdvancedCustomFunctionDef
AdvancedCustomFunctionDef is a type alias for functions.AdvancedCustomFunctionDef, re-exported so callers only need to import the top-level gosonata package.
type CustomFunc ¶
type CustomFunc = functions.CustomFunc
CustomFunc is the signature for user-defined functions callable from JSONata expressions. See WithCustomFunction.
type CustomFunctionDef ¶
type CustomFunctionDef = functions.CustomFunctionDef
CustomFunctionDef is a type alias for functions.CustomFunctionDef, re-exported so callers only need to import the top-level gosonata package.
type EvalOption ¶
type EvalOption = evaluator.EvalOption
EvalOption is a type alias for evaluator.EvalOption so callers do not need to import the evaluator package directly.
func WithCacheSize ¶
func WithCacheSize(size int) EvalOption
WithCacheSize re-exports evaluator.WithCacheSize for convenience.
func WithCaching ¶
func WithCaching(enabled bool) EvalOption
WithCaching re-exports evaluator.WithCaching for convenience.
func WithConcurrency ¶
func WithConcurrency(enabled bool) EvalOption
WithConcurrency re-exports evaluator.WithConcurrency for convenience.
func WithCustomFunction ¶
func WithCustomFunction(name, signature string, fn CustomFunc) EvalOption
WithCustomFunction registers a user-defined function with name (without "$") and an optional JSONata type-signature string.
Example:
result, err := gosonata.Eval(`$greet("World")`, nil,
gosonata.WithCustomFunction("greet", "<s:s>", func(ctx context.Context, args ...interface{}) (interface{}, error) {
return "Hello, " + args[0].(string) + "!", nil
}),
)
func WithDebug ¶
func WithDebug(enabled bool) EvalOption
WithDebug re-exports evaluator.WithDebug for convenience.
func WithFunctions ¶
func WithFunctions(defs ...functions.FunctionEntry) EvalOption
WithFunctions registers any mix of CustomFunctionDef and AdvancedCustomFunctionDef in a single variadic call. Use it to spread the result of AllEntries() from any ext sub-package:
gosonata.WithFunctions(extstring.AllEntries()...) gosonata.WithFunctions(extarray.AllEntries()...) gosonata.WithFunctions(ext.AllEntries()...)
func WithTimeout ¶
func WithTimeout(t time.Duration) EvalOption
WithTimeout re-exports evaluator.WithTimeout for convenience.
type FunctionEntry ¶
type FunctionEntry = functions.FunctionEntry
FunctionEntry is a type alias for functions.FunctionEntry, the common interface implemented by both functions.CustomFunctionDef and functions.AdvancedCustomFunctionDef. It allows mixing both kinds in a single call to WithFunctions.
type StreamResult ¶
type StreamResult = evaluator.StreamResult
StreamResult re-exports evaluator.StreamResult for callers that only import gosonata.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
wasm/js
command
Command gosonata-wasm-js is the WebAssembly entrypoint for browser and Node.js.
|
Command gosonata-wasm-js is the WebAssembly entrypoint for browser and Node.js. |
|
pkg
|
|
|
cache
Package cache provides a thread-safe expression cache for compiled JSONata expressions.
|
Package cache provides a thread-safe expression cache for compiled JSONata expressions. |
|
ext
Package ext provides optional extension functions for GoSonata that go beyond the official JSONata 2.1.0+ specification.
|
Package ext provides optional extension functions for GoSonata that go beyond the official JSONata 2.1.0+ specification. |
|
ext/extarray
Package extarray provides extended array functions for GoSonata beyond the official JSONata spec.
|
Package extarray provides extended array functions for GoSonata beyond the official JSONata spec. |
|
ext/extcrypto
Package extcrypto provides cryptographic and hashing functions for GoSonata.
|
Package extcrypto provides cryptographic and hashing functions for GoSonata. |
|
ext/extdatetime
Package extdatetime provides extended date/time functions for GoSonata beyond the official JSONata spec.
|
Package extdatetime provides extended date/time functions for GoSonata beyond the official JSONata spec. |
|
ext/extformat
Package extformat provides data-format functions for GoSonata (CSV, templates).
|
Package extformat provides data-format functions for GoSonata (CSV, templates). |
|
ext/extfunc
Package extfunc provides functional programming utilities for GoSonata beyond the official JSONata spec.
|
Package extfunc provides functional programming utilities for GoSonata beyond the official JSONata spec. |
|
ext/extnumeric
Package extnumeric provides extended numeric functions for GoSonata beyond the official JSONata spec.
|
Package extnumeric provides extended numeric functions for GoSonata beyond the official JSONata spec. |
|
ext/extobject
Package extobject provides extended object functions for GoSonata beyond the official JSONata spec.
|
Package extobject provides extended object functions for GoSonata beyond the official JSONata spec. |
|
ext/extstring
Package extstring provides extended string functions for GoSonata beyond the official JSONata spec.
|
Package extstring provides extended string functions for GoSonata beyond the official JSONata spec. |
|
ext/exttypes
Package exttypes provides type predicate and control functions for GoSonata beyond the official JSONata spec.
|
Package exttypes provides type predicate and control functions for GoSonata beyond the official JSONata spec. |
|
ext/extutil
Package extutil provides shared helpers for the ext sub-packages.
|
Package extutil provides shared helpers for the ext sub-packages. |
|
functions
Package functions provides types for registering custom JSONata functions.
|
Package functions provides types for registering custom JSONata functions. |
|
stream
Package stream provides a high-throughput multi-expression evaluator.
|
Package stream provides a high-throughput multi-expression evaluator. |
|
types
Package types defines the core type system for GoSonata.
|
Package types defines the core type system for GoSonata. |
|
tests
|
|
|
conformance/importer/cmd/import
command
|
|