Skip to content

Commit

Permalink
Add MustParse and WithRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Sep 16, 2024
1 parent 8a4d7f2 commit 3ded3fd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
50 changes: 42 additions & 8 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions path_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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]")
Expand Down
3 changes: 1 addition & 2 deletions path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3ded3fd

Please sign in to comment.