registry

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2024 License: MIT Imports: 7 Imported by: 4 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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) Name

func (f *Function) Name() string

Name returns the name of the function.

func (*Function) ResultType

func (f *Function) ResultType() spec.FuncType

ResultType returns the data type of the function return value.

func (*Function) Validate

func (f *Function) Validate(args []spec.FunctionExprArg) error

Validate executes at parse time to validate that all the args to the function are compatible with the function.

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:

func (*Registry) Get

func (r *Registry) Get(name string) *Function

Get returns a reference to the registered function named name. Returns nil if no function with that name has been registered.

func (*Registry) Register

func (r *Registry) Register(
	name string,
	resultType spec.FuncType,
	validator Validator,
	evaluator Evaluator,
) error

Register registers a function extension by its name. Returns an ErrRegister error if validator or nil or evaluator is nil or if r already contains name.

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.

Jump to

Keyboard shortcuts

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