-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath.go
205 lines (176 loc) · 5.25 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Package jsonpath implements RFC 9535 JSONPath query expressions.
package jsonpath
import (
"iter"
"slices"
"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) NodeList {
return p.q.Select(nil, input)
}
// SelectLocated returns the values that JSONPath query p selects from input
// as [spec.LocatedNode] structs pair the values with the [normalized paths]
// that identify them. Unless you have a specific need for the unique
// normalized path for each value, you probably want to use [Path.Select].
//
// [normalized paths]: https://www.rfc-editor.org/rfc/rfc9535#section-2.7
func (p *Path) SelectLocated(input any) LocatedNodeList {
return p.q.SelectLocated(nil, input, spec.NormalizedPath{})
}
// 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)
}
// NodeList is a list of nodes selected by a JSONPath query. Each node
// represents a single JSON value selected from the JSON query argument.
// Returned by [Path.Select].
type NodeList []any
// All returns an iterator over all the nodes in list.
//
// Range over list itself to get indexes and node values.
func (list NodeList) All() iter.Seq[any] {
return func(yield func(any) bool) {
for _, v := range list {
if !yield(v) {
return
}
}
}
}
// LocatedNodeList is a list of nodes selected by a JSONPath query, along with
// their locations. Returned by [Path.SelectLocated].
type LocatedNodeList []*spec.LocatedNode
// All returns an iterator over all the nodes in list.
//
// Range over list itself to get indexes and node values.
func (list LocatedNodeList) All() iter.Seq[*spec.LocatedNode] {
return func(yield func(*spec.LocatedNode) bool) {
for _, v := range list {
if !yield(v) {
return
}
}
}
}
// Nodes returns an iterator over all the nodes in list. This is effectively
// the same data a returned by [Path.Select].
func (list LocatedNodeList) Nodes() iter.Seq[any] {
return func(yield func(any) bool) {
for _, v := range list {
if !yield(v.Node) {
return
}
}
}
}
// Paths returns an iterator over all the normalized paths in list.
func (list LocatedNodeList) Paths() iter.Seq[spec.NormalizedPath] {
return func(yield func(spec.NormalizedPath) bool) {
for _, v := range list {
if !yield(v.Path) {
return
}
}
}
}
// Deduplicate deduplicates the nodes in list based on their normalized paths,
// modifying the contents of list. It returns the modified list, which may
// have a smaller length, and zeroes the elements between the new length and
// the original length.
func (list LocatedNodeList) Deduplicate() LocatedNodeList {
if len(list) <= 1 {
return list
}
seen := map[string]struct{}{}
uniq := list[:0]
for _, n := range list {
p := n.Path.String()
if _, x := seen[p]; !x {
seen[p] = struct{}{}
uniq = append(uniq, n)
}
}
clear(list[len(uniq):]) // zero/nil out the obsolete elements, for GC
return slices.Clip(uniq)
}
// Sort sorts list by the normalized path of each node.
func (list LocatedNodeList) Sort() {
slices.SortFunc(list, func(a, b *spec.LocatedNode) int {
return a.Path.Compare(b.Path)
})
}
// Clone returns a shallow copy of list.
func (list LocatedNodeList) Clone() LocatedNodeList {
return append(make(LocatedNodeList, 0, len(list)), list...)
}