Skip to content

Commit a122146

Browse files
committed
Fix doc links & typos, move WithRegistry example
1 parent 6444c27 commit a122146

File tree

5 files changed

+53
-43
lines changed

5 files changed

+53
-43
lines changed

CHANGELOG.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ All notable changes to this project will be documented in this file. It uses the
1313

1414
* Made significant changes to the `spec` package toward public stability,
1515
usability, and greater comprehension. The names of things are more
16-
consistent, the APIs more legible and user-friendly, and the documentation
17-
thorough, with examples for each significant type. See [PR #14] for
16+
consistent, the APIs more legible and user-friendly. See [PR #14] for
1817
details.
19-
* Removed the "Package Stability" statement from the README, as all packages
20-
are considered stable.
2118
* Added support for [json.Number] values to complement the existing support
2219
for Go core numeric types. This should allow for transparent handling of
2320
values marshaled with [json.Decoder.UseNumber] enabled.
2421

22+
### 📚 Documentation
23+
24+
* Vastly expanded the `spec` package documentation to make it much more
25+
thorough and accurate, with copious links to relevant parts of [RFC 9535]
26+
and complete lists of interface implementations and examples for each
27+
significant type. See [PR #14] for details.
28+
* Removed the "Package Stability" statement from the README, as all packages
29+
are considered stable.
30+
* Fixed links and typos in the main package documentation, and moved the
31+
registry example under `WithRegistry`.
32+
2533
### 📔 Notes
2634

2735
* Upgraded the [compliance test suite] and integrated its located path test
@@ -32,6 +40,8 @@ All notable changes to this project will be documented in this file. It uses the
3240
[json.Decoder.UseNumber]: https://pkg.go.dev/encoding/json#Decoder.UseNumber
3341
[PR #14]: https://github.com/theory/jsonpath/pull/14
3442
[compliance test suite]: https://github.com/jsonpath-standard/jsonpath-compliance-test-suite
43+
[RFC 9535]: https://www.rfc-editor.org/rfc/rfc9535.html
44+
"RFC 9535 JSONPath: Query Expressions for JSON"
3545

3646
## [v0.4.1] — 2025-05-02
3747

parser/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ type parser struct {
3535
reg *registry.Registry
3636
}
3737

38-
// Parse parses path, a JSON Path query string, into a PathQuery. Returns a
39-
// PathParseError on parse failure.
38+
// Parse parses path, a JSONPath query string, into a [spec.PathQuery].
39+
// Returns a [ErrPathParse] on parse failure.
4040
func Parse(reg *registry.Registry, path string) (*spec.PathQuery, error) {
4141
lex := newLexer(path)
4242
tok := lex.scan()

path.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ type Path struct {
2020
q *spec.PathQuery
2121
}
2222

23-
// New creates and returns a new Path consisting of q.
23+
// New creates and returns a new [Path] consisting of q.
2424
func New(q *spec.PathQuery) *Path {
2525
return &Path{q: q}
2626
}
2727

28-
// Parse parses path, a JSONPath query string, into a Path. Returns an
29-
// ErrPathParse on parse failure.
28+
// Parse parses path, a JSONPath query string, into a [Path]. Returns an
29+
// [ErrPathParse] on parse failure.
3030
func Parse(path string) (*Path, error) {
3131
return NewParser().Parse(path)
3232
}
3333

34-
// MustParse parses path into a Path. Panics with an ErrPathParse on parse
34+
// MustParse parses path into a [Path]. Panics with an [ErrPathParse] on parse
3535
// failure.
3636
func MustParse(path string) *Path {
3737
return NewParser().MustParse(path)
@@ -42,20 +42,21 @@ func (p *Path) String() string {
4242
return p.q.String()
4343
}
4444

45-
// Query returns p's root Query.
45+
// Query returns p's root [spec.PathQuery].
4646
func (p *Path) Query() *spec.PathQuery {
4747
return p.q
4848
}
4949

50-
// Select returns the values that JSONPath query p selects from input.
50+
// Select returns the nodes that JSONPath query p selects from input.
5151
func (p *Path) Select(input any) NodeList {
5252
return p.q.Select(nil, input)
5353
}
5454

55-
// SelectLocated returns the values that JSONPath query p selects from input
56-
// as [spec.LocatedNode] values that pair the values with the [normalized
57-
// paths] that identify them. Unless you have a specific need for the unique
58-
// normalized path for each value, you probably want to use [Path.Select].
55+
// SelectLocated returns the nodes that JSONPath query p selects from input as
56+
// [spec.LocatedNode] values that pair the nodes with the [normalized paths]
57+
// that identify them. Unless you have a specific need for the unique
58+
// [spec.NormalizedPath] for each value, you probably want to use
59+
// [Path.Select].
5960
//
6061
// [normalized paths]: https://www.rfc-editor.org/rfc/rfc9535#section-2.7
6162
func (p *Path) SelectLocated(input any) LocatedNodeList {
@@ -70,13 +71,13 @@ type Parser struct {
7071
// Option defines a parser option.
7172
type Option func(*Parser)
7273

73-
// WithRegistry configures a Parser with a function Registry, which may
74-
// contain function extensions. See [Parser] for an example.
74+
// WithRegistry configures a [Parser] with a [registry.Registry], which may
75+
// contain function extensions.
7576
func WithRegistry(reg *registry.Registry) Option {
7677
return func(p *Parser) { p.reg = reg }
7778
}
7879

79-
// NewParser creates a new Parser configured by opt.
80+
// NewParser creates a new [Parser] configured by opt.
8081
func NewParser(opt ...Option) *Parser {
8182
p := &Parser{}
8283
for _, o := range opt {
@@ -90,20 +91,19 @@ func NewParser(opt ...Option) *Parser {
9091
return p
9192
}
9293

93-
// Parse parses path, a JSON Path query string, into a Path. Returns an
94-
// ErrPathParse on parse failure.
95-
//
96-
//nolint:wrapcheck
94+
// Parse parses path, a JSONPath query string, into a [Path]. Returns an
95+
// [ErrPathParse] on parse failure.
9796
func (c *Parser) Parse(path string) (*Path, error) {
9897
q, err := parser.Parse(c.reg, path)
9998
if err != nil {
99+
//nolint:wrapcheck
100100
return nil, err
101101
}
102102
return New(q), nil
103103
}
104104

105-
// MustParse parses path, a JSON Path query string, into a Path. Panics with
106-
// an ErrPathParse on parse failure.
105+
// MustParse parses path, a JSONPath query string, into a [Path]. Panics with
106+
// an [ErrPathParse] on parse failure.
107107
func (c *Parser) MustParse(path string) *Path {
108108
q, err := parser.Parse(c.reg, path)
109109
if err != nil {
@@ -131,7 +131,7 @@ func (list NodeList) All() iter.Seq[any] {
131131
}
132132

133133
// LocatedNodeList is a list of nodes selected by a JSONPath query, along with
134-
// their locations. Returned by [Path.SelectLocated].
134+
// their [NormalizedPath] locations. Returned by [Path.SelectLocated].
135135
type LocatedNodeList []*spec.LocatedNode
136136

137137
// All returns an iterator over all the nodes in list.
@@ -147,8 +147,8 @@ func (list LocatedNodeList) All() iter.Seq[*spec.LocatedNode] {
147147
}
148148
}
149149

150-
// Nodes returns an iterator over all the nodes in list. This is effectively
151-
// the same data a returned by [Path.Select].
150+
// Nodes returns an iterator over all the nodes in list. This is the same data
151+
// as returned by [Path.Select].
152152
func (list LocatedNodeList) Nodes() iter.Seq[any] {
153153
return func(yield func(any) bool) {
154154
for _, v := range list {
@@ -159,7 +159,7 @@ func (list LocatedNodeList) Nodes() iter.Seq[any] {
159159
}
160160
}
161161

162-
// Paths returns an iterator over all the normalized paths in list.
162+
// Paths returns an iterator over all the [NormalizedPath] values in list.
163163
func (list LocatedNodeList) Paths() iter.Seq[spec.NormalizedPath] {
164164
return func(yield func(spec.NormalizedPath) bool) {
165165
for _, v := range list {
@@ -170,10 +170,10 @@ func (list LocatedNodeList) Paths() iter.Seq[spec.NormalizedPath] {
170170
}
171171
}
172172

173-
// Deduplicate deduplicates the nodes in list based on their normalized paths,
174-
// modifying the contents of list. It returns the modified list, which may
175-
// have a smaller length, and zeroes the elements between the new length and
176-
// the original length.
173+
// Deduplicate deduplicates the nodes in list based on their [NormalizedPath]
174+
// values, modifying the contents of list. It returns the modified list, which
175+
// may have a shorter length, and zeroes the elements between the new length
176+
// and the original length.
177177
func (list LocatedNodeList) Deduplicate() LocatedNodeList {
178178
if len(list) <= 1 {
179179
return list
@@ -192,7 +192,7 @@ func (list LocatedNodeList) Deduplicate() LocatedNodeList {
192192
return slices.Clip(uniq)
193193
}
194194

195-
// Sort sorts list by the normalized path of each node.
195+
// Sort sorts list by the [NormalizedPath] of each node.
196196
func (list LocatedNodeList) Sort() {
197197
slices.SortFunc(list, func(a, b *spec.LocatedNode) int {
198198
return a.Path.Compare(b.Path)

path_example_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ func ExampleParser() {
228228
// ["Sayings of the Century","Moby Dick"]
229229
}
230230

231-
// The second use case for the Parser is to provide a [registry.Registry]
231+
// Use WithRegistry to create a [Parser] that uses a [registry.Registry]
232232
// containing function extensions, as [defined by the standard]. This example
233233
// creates a function named "first" that returns the first item in a list of
234234
// nodes.
235235
//
236236
// [defined by the standard]: https://www.rfc-editor.org/rfc/rfc9535.html#name-function-extensions
237-
func ExampleParser_functionExtension() {
237+
func ExampleWithRegistry() {
238238
// Register the first function.
239239
reg := registry.New()
240240
err := reg.Register(
@@ -268,22 +268,22 @@ func ExampleParser_functionExtension() {
268268
}
269269

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

278-
if !fea[0].ResultType().ConvertsToNodes() {
278+
if !args[0].ResultType().ConvertsToNodes() {
279279
return errors.New("cannot convert argument to nodes")
280280
}
281281

282282
return nil
283283
}
284284

285285
// firstFunc defines the custom first() JSONPath function. It converts its
286-
// single argument to a [spec.NodesType] value and returns a [*spec.ValueType]
286+
// single argument to a [spec.NodesType] value and returns a [spec.ValueType]
287287
// that contains the first node. If there are no nodes it returns nil.
288288
func firstFunc(jv []spec.JSONPathValue) spec.JSONPathValue {
289289
nodes := spec.NodesFrom(jv[0])

spec/function.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (ft FuncType) ConvertsToNodes() bool {
6767
return ft == FuncNodes || ft == FuncSingularQuery
6868
}
6969

70-
// JSONPathValue defines the interface for JSON path values used as comparison
70+
// JSONPathValue defines the interface for JSONPath values used as comparison
7171
// operands, filter expression results, and function parameters & return
7272
// values.
7373
//

0 commit comments

Comments
 (0)