registry

package
v0.12.2 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: MIT Imports: 8 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 extension registry.

Example

Create and register a JSONPath extension function, first(), that returns the first node in a list of nodes passed to it. See github.com/theory/jsonpath.WithRegistry for a more complete example.

package main

import (
	"errors"
	"fmt"
	"log"

	"github.com/theory/jsonpath/registry"
	"github.com/theory/jsonpath/spec"
)

// Create and register a JSONPath extension function, first(), that returns
// the first node in a list of nodes passed to it. See
// [github.com/theory/jsonpath.WithRegistry] 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").ReturnType())
}

// validateFirstArgs validates that a single argument is passed to the first()
// extension function, and that it can be converted to [spec.NodesType], so
// that first() can return the first node. It's called by the parser.
func validateFirstArgs(args []spec.FuncExprArg) error {
	if len(args) != 1 {
		return fmt.Errorf("expected 1 argument but found %v", len(args))
	}

	if !args[0].ConvertsTo(spec.FuncNodes) {
		return errors.New("cannot convert argument to Nodes")
	}

	return nil
}

// firstFunc defines the first() JSONPath extension 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.PathValue) spec.PathValue {
	nodes := spec.NodesFrom(jv[0])
	if len(nodes) == 0 {
		return nil
	}
	return spec.Value(nodes[0])
}
Output:
Value

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrRegister = errors.New("register")

ErrRegister errors are returned by Registry.Register.

Functions

This section is empty.

Types

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry maintains a registry of JSONPath function extensions, including both RFC 9535-required functions and custom functions.

func New

func New() *Registry

New returns a new Registry loaded with the RFC 9535-mandated function extensions:

func (*Registry) Get

func (r *Registry) Get(name string) *spec.FuncExtension

Get returns a reference to the registered function extension named name. Returns nil if no function with that name has been registered. Used by the parser to match a function name to its implementation.

func (*Registry) Register

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

Register registers a function extension. The parameters are:

  • name: the name of the function extension as used in JSONPath queries.
  • returnType: The data type of the function return value.
  • validator: A validation function that will be called at parse time to validate that all the function args are compatible with the function.
  • evaluator: The implementation of the function itself that executes against args and returns the result of the type defined by resultType.

Returns ErrRegister if validator or evaluator is nil or if r already contains name.

Jump to

Keyboard shortcuts

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