From 3ded3fd98fe7bca97a65c6ee2f616dafe91cb2bd Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Mon, 16 Sep 2024 11:45:23 -0400 Subject: [PATCH] Add MustParse and WithRegistry --- path.go | 50 +++++++++++++++++++++++++++++++++++++------- path_example_test.go | 4 ++-- path_test.go | 3 +-- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/path.go b/path.go index ee51be2..3a5d525 100644 --- a/path.go +++ b/path.go @@ -22,10 +22,16 @@ func New(q *spec.PathQuery) *Path { return &Path{q: q} } -// Parse parses path, a JSON Path query string, into a Path. Returns a -// PathParseError on parse failure. +// Parse parses path, a JSONPath query string, into a Path. Returns an +// ErrPathParse on parse failure. func Parse(path string) (*Path, error) { - return NewParser(registry.New()).Parse(path) + return NewParser().Parse(path) +} + +// MustParse parses path into a Path. Panics with an ErrPathParse on parse +// failure. +func MustParse(path string) *Path { + return NewParser().MustParse(path) } // String returns a string representation of p. @@ -48,13 +54,31 @@ type Parser struct { reg *registry.Registry } -// NewParser creates a new Parser that uses the functions provided by reg. -func NewParser(reg *registry.Registry) *Parser { - return &Parser{reg: reg} +// Option defines a parser option. +type Option func(*Parser) + +// WithRegistry configures a Parser with a function Registry, which may +// contain function extensions. See [Parser] for an example. +func WithRegistry(reg *registry.Registry) Option { + return func(p *Parser) { p.reg = reg } +} + +// NewParser creates a new Parser configured by opt. +func NewParser(opt ...Option) *Parser { + p := &Parser{} + for _, o := range opt { + o(p) + } + + if p.reg == nil { + p.reg = registry.New() + } + + return p } -// Parse parses path, a JSON Path query string, into a Path. Returns a -// PathParseError on parse failure. +// Parse parses path, a JSON Path query string, into a Path. Returns an +// ErrPathParse on parse failure. // //nolint:wrapcheck func (c *Parser) Parse(path string) (*Path, error) { @@ -64,3 +88,13 @@ func (c *Parser) Parse(path string) (*Path, error) { } return New(q), nil } + +// MustParse parses path, a JSON Path query string, into a Path. Panics with +// an ErrPathParse on parse failure. +func (c *Parser) MustParse(path string) *Path { + q, err := parser.Parse(c.reg, path) + if err != nil { + panic(err) + } + return New(q) +} diff --git a/path_example_test.go b/path_example_test.go index 00636f8..da5ec74 100644 --- a/path_example_test.go +++ b/path_example_test.go @@ -36,7 +36,7 @@ func Example() { // Use the Parser to parse a collection of paths. func ExampleParser() { // Create a new parser using the default function registry. - parser := jsonpath.NewParser(registry.New()) + parser := jsonpath.NewParser() // Parse a list of paths. paths := []*jsonpath.Path{} @@ -97,7 +97,7 @@ func ExampleParser_functionExtension() { } // Create a parser with the registry that contains the extension. - parser := jsonpath.NewParser(reg) + parser := jsonpath.NewParser(jsonpath.WithRegistry(reg)) // Use the function to select lists that start with 6. path, err := parser.Parse("$[? first(@.*) == 6]") diff --git a/path_test.go b/path_test.go index de3f39c..944bcdc 100644 --- a/path_test.go +++ b/path_test.go @@ -10,7 +10,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/theory/jsonpath/registry" ) func TestParseSpecExamples(t *testing.T) { @@ -207,7 +206,7 @@ func TestParseCompliance(t *testing.T) { t.Parallel() a := assert.New(t) r := require.New(t) - p := NewParser(registry.New()) + p := NewParser() //nolint:tagliatelle type testCase struct {