Documentation
¶
Overview ¶
Package registry provides a RFC 9535 JSONPath function registry.
Example ¶
Create and registry a custom JSONPath expression, first(), that returns the first node in a list of nodes passed to it. See github.com/theory/jsonpath.Parser for a more complete example.
package main import ( "errors" "fmt" "log" "github.com/theory/jsonpath/registry" "github.com/theory/jsonpath/spec" ) // Create and registry a custom JSONPath expression, first(), that returns the // first node in a list of nodes passed to it. See // [github.com/theory/jsonpath.Parser] for a more complete example. func main() { reg := registry.New() err := reg.Register( "first", // function name spec.FuncValue, // returns a single value validateFirstArgs, // parse-time validation defined below firstFunc, // function defined below ) if err != nil { log.Fatalf("Error %v", err) } fmt.Printf("%v\n", reg.Get("first").ResultType()) } // validateFirstArgs validates that a single argument is passed to the first() // function, and that it can be converted to [spec.PathNodes], so that first() // can return the first node. It's called by the parser. func validateFirstArgs(fea []spec.FunctionExprArg) error { if len(fea) != 1 { return fmt.Errorf("expected 1 argument but found %v", len(fea)) } if !fea[0].ResultType().ConvertsTo(spec.PathNodes) { return errors.New("cannot convert argument to PathNodes") } return nil } // firstFunc defines the custom first() JSONPath function. It converts its // single argument to a [spec.NodesType] value and returns a [*spec.ValueType] // that contains the first node. If there are no nodes it returns nil. func firstFunc(jv []spec.JSONPathValue) spec.JSONPathValue { nodes := spec.NodesFrom(jv[0]) if len(nodes) == 0 { return nil } return spec.Value(nodes[0]) }
Output: FuncValue
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrRegister = errors.New("register")
ErrRegister errors are returned by [Register].
Functions ¶
This section is empty.
Types ¶
type Evaluator ¶
type Evaluator func(args []spec.JSONPathValue) spec.JSONPathValue
Evaluator functions execute a function against the values returned by args.
type Function ¶
type Function struct {
// contains filtered or unexported fields
}
Function defines a JSONPath function. Use [Register] to register a new function.
func NewFunction ¶
func NewFunction( name string, resultType spec.FuncType, validator func(args []spec.FunctionExprArg) error, evaluator func(args []spec.JSONPathValue, ) spec.JSONPathValue, ) *Function
NewFunction creates a new JSONPath function extension. The parameters are:
- name: the name of the function as used in JSONPath queries.
- resultType: The data type of the function return value.
- validator: A validation function that will be called by at parse time to validate that all the function args are compatible with the function.
- evaluator: The implementation of the function itself that executes the against args and returns the result defined by resultType.
func (*Function) Evaluate ¶
func (f *Function) Evaluate(args []spec.JSONPathValue) spec.JSONPathValue
Evaluate executes the function against args and returns the result of type [ResultType].
func (*Function) ResultType ¶
ResultType returns the data type of the function return value.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry maintains a registry of JSONPath functions, including both RFC 9535-required functions and function extensions.
func New ¶
func New() *Registry
New returns a new Registry loaded with the RFC 9535-mandated functions:
type Validator ¶
type Validator func(args []spec.FunctionExprArg) error
Validator functions validate that the args expressions to a function can be processed by the function.