-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.go
100 lines (83 loc) · 2.32 KB
/
path.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Package jsonpath implements RFC 9535 JSONPath query expressions.
package jsonpath
import (
"github.com/theory/jsonpath/parser"
"github.com/theory/jsonpath/registry"
"github.com/theory/jsonpath/spec"
)
// ErrPathParse errors are returned for path parse errors.
var ErrPathParse = parser.ErrPathParse
// Path represents a [RFC 9535] JSONPath query.
//
// [RFC 9535]: https://www.rfc-editor.org/rfc/rfc9535.html
type Path struct {
q *spec.PathQuery
}
// New creates and returns a new Path consisting of q.
func New(q *spec.PathQuery) *Path {
return &Path{q: q}
}
// Parse parses path, a JSONPath query string, into a Path. Returns an
// ErrPathParse on parse failure.
func Parse(path string) (*Path, error) {
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.
func (p *Path) String() string {
return p.q.String()
}
// Query returns p's root Query.
func (p *Path) Query() *spec.PathQuery {
return p.q
}
// Select returns the values that JSONPath query p selects from input.
func (p *Path) Select(input any) []any {
return p.q.Select(nil, input)
}
// Parser parses JSONPath strings into [*Path]s.
type Parser struct {
reg *registry.Registry
}
// 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 an
// ErrPathParse on parse failure.
//
//nolint:wrapcheck
func (c *Parser) Parse(path string) (*Path, error) {
q, err := parser.Parse(c.reg, path)
if err != nil {
return nil, err
}
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)
}