Skip to content

Commit 13a8385

Browse files
committed
Finish doc copyedit
1 parent f622b8e commit 13a8385

File tree

7 files changed

+98
-50
lines changed

7 files changed

+98
-50
lines changed

path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (p *Path) Select(input any) NodeList {
5353
}
5454

5555
// SelectLocated returns the values that JSONPath query p selects from input
56-
// as [spec.LocatedNode] structs that pair the values with the [normalized
56+
// as [spec.LocatedNode] objects that pair the values with the [normalized
5757
// paths] that identify them. Unless you have a specific need for the unique
5858
// normalized path for each value, you probably want to use [Path.Select].
5959
//

spec/function.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ type JSONPathValue interface {
8282

8383
// NodesType defines a node list (a list of JSON values) for a function
8484
// expression parameters or results, as defined by [RFC 9535 Section 2.4.1].
85-
// It can also be used in filter expressions. The underlying values should be
86-
// strings, integers, floats, [json.Number]s, nil, true, false, []any, or
85+
// It can also be used in filter expressions. The underlying types should be
86+
// string, integer, float, [json.Number], nil, true, false, []any, or
8787
// map[string]any. Interfaces implemented:
8888
//
8989
// - [JSONPathValue]
@@ -93,8 +93,8 @@ type JSONPathValue interface {
9393
type NodesType []any
9494

9595
// Nodes creates a NodesType that contains val, all of which should be the Go
96-
// equivalent of the JSON data types: strings, integers, floats,
97-
// [json.Number]s, nil, true, false, []any, or map[string]any.
96+
// equivalent of the JSON data types: string, integer, float, [json.Number],
97+
// nil, true, false, []any, or map[string]any.
9898
func Nodes(val ...any) NodesType {
9999
return NodesType(val)
100100
}
@@ -134,8 +134,8 @@ func (nt NodesType) String() string {
134134
// parameters or results, as defined by [RFC 9535 Section 2.4.1]. Interfaces
135135
// implemented:
136136
//
137-
// - [JSONPathValue]
138-
// - [fmt.Stringer]
137+
// - [JSONPathValue]
138+
// - [fmt.Stringer]
139139
//
140140
// [RFC 9535 Section 2.4.1]: https://www.rfc-editor.org/rfc/rfc9535.html#section-2.4.1
141141
type LogicalType uint8
@@ -192,21 +192,21 @@ func (lt LogicalType) writeTo(buf *strings.Builder) {
192192

193193
// ValueType encapsulates a JSON value for a function expression parameter or
194194
// result, as defined by [RFC 9535 Section 2.4.1]. It can also be used as in
195-
// filter expression. The underlying value should be a string, integer, float,
196-
// nil, true, false, []any, or map[string]any. A nil ValueType pointer
197-
// indicates no value. Interfaces implemented:
195+
// filter expression. The underlying value should be a string, integer,
196+
// [json.Number], float, nil, true, false, []any, or map[string]any. A nil
197+
// ValueType pointer indicates no value. Interfaces implemented:
198198
//
199-
// - [JSONPathValue]
200-
// - [BasicExpr]
201-
// - [fmt.Stringer]
199+
// - [JSONPathValue]
200+
// - [BasicExpr]
201+
// - [fmt.Stringer]
202202
//
203203
// [RFC 9535 Section 2.4.1]: https://www.rfc-editor.org/rfc/rfc9535.html#section-2.4.1
204204
type ValueType struct {
205205
any
206206
}
207207

208-
// Value returns a new ValueType for val, which must be the Go equivalent of a
209-
// JSON data type: string, integer, float, [json.Number], nil, true, false,
208+
// Value returns a new [ValueType] for val, which must be the Go equivalent of
209+
// a JSON data type: string, integer, float, [json.Number], nil, true, false,
210210
// []any, or map[string]any.
211211
func Value(val any) *ValueType {
212212
return &ValueType{val}
@@ -357,7 +357,7 @@ func (la *LiteralArg) asValue(_, _ any) JSONPathValue {
357357
}
358358

359359
// SingularQueryExpr represents a query that produces a single [ValueType]
360-
// (JSON value), or nothing. Used in contexts that require a singular value,
360+
// (JSON value) or nothing. Used in contexts that require a singular value,
361361
// such as comparison operations and function arguments. Interfaces
362362
// implemented:
363363
//
@@ -373,7 +373,7 @@ type SingularQueryExpr struct {
373373
}
374374

375375
// SingularQuery creates and returns a [SingularQueryExpr] that selects a
376-
// single value.
376+
// single value at the path defined by selectors.
377377
func SingularQuery(root bool, selectors ...Selector) *SingularQueryExpr {
378378
return &SingularQueryExpr{relative: !root, selectors: selectors}
379379
}
@@ -469,7 +469,7 @@ func (fq *NodesQueryExpr) writeTo(buf *strings.Builder) {
469469
}
470470

471471
// PathFunction represents a JSONPath function. See
472-
// [github.com/theory/jsonpath/registry] for the implementation.
472+
// [github.com/theory/jsonpath/registry.Function] for the implementation.
473473
type PathFunction interface {
474474
Name() string
475475
ResultType() FuncType
@@ -570,7 +570,7 @@ type NotFuncExpr struct {
570570
}
571571

572572
// NotFunction creates and returns a new NotFuncExpr that will execute fn
573-
// against the return values of args and return the inverses of its return
573+
// against the return values of args and return the inverse of its return
574574
// value.
575575
func NotFunction(fn *FuncExpr) NotFuncExpr {
576576
return NotFuncExpr{fn}

spec/normalized.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ func (np NormalizedPath) MarshalText() ([]byte, error) {
102102
return []byte(np.String()), nil
103103
}
104104

105-
// LocatedNode pairs a value with its location within the JSON query argument
106-
// from which it was selected. Used only for nodes in a [NormalizedPath].
105+
// LocatedNode pairs a value with the [NormalizedPath] for its location within
106+
// the JSON query argument from which it was selected. Returned by
107+
// implementations of [Selector]'s SelectLocated method.
107108
type LocatedNode struct {
108109
// Node is the value selected from a JSON query argument.
109110
Node any `json:"node"`

spec/query.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ type PathQuery struct {
1010
root bool
1111
}
1212

13-
// Query returns a new query consisting of segments. When root is true it
14-
// indicates a query from the root of a value. Set to false for filter
13+
// Query returns a new [PathQuery] consisting of segments. When root is true
14+
// it indicates a query from the root of a value. Set to false for filter
1515
// subqueries.
1616
func Query(root bool, segments ...*Segment) *PathQuery {
1717
return &PathQuery{root: root, segments: segments}
1818
}
1919

20-
// Segments returns q's [Segment]s.
20+
// Segments returns q's [Segment] objects.
2121
func (q *PathQuery) Segments() []*Segment {
2222
return q.segments
2323
}
@@ -36,7 +36,7 @@ func (q *PathQuery) String() string {
3636
return buf.String()
3737
}
3838

39-
// Select selects the [Segment]s from current or root and returns the result.
39+
// Select selects the values from current or root and returns the results.
4040
// Returns just current if q has no segments. Defined by the [Selector]
4141
// interface.
4242
func (q *PathQuery) Select(current, root any) []any {
@@ -55,9 +55,9 @@ func (q *PathQuery) Select(current, root any) []any {
5555
return res
5656
}
5757

58-
// SelectLocated selects the [Segment]s from current or root and returns the
59-
// resulting values as [LocatedNode] structs. Returns just current if q has no
60-
// segments. Defined by the [Selector] interface.
58+
// SelectLocated values from current or root into [LocatedNode] objects and
59+
// returns the results. Returns just current if q has no segments. Defined by
60+
// the [Selector] interface.
6161
func (q *PathQuery) SelectLocated(current, root any, parent NormalizedPath) []*LocatedNode {
6262
res := []*LocatedNode{nil}
6363
if q.root {

spec/segment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (s *Segment) Select(current, root any) []any {
6767
return ret
6868
}
6969

70-
// SelectLocated selects and returns values as [LocatedNode] structs from
70+
// SelectLocated selects and returns values as [LocatedNode] objects from
7171
// current or root for each of seg's selectors. Defined by the [Selector]
7272
// interface.
7373
func (s *Segment) SelectLocated(current, root any, parent NormalizedPath) []*LocatedNode {

spec/selector.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Selector interface {
2323
Select(current, root any) []any
2424

2525
// SelectLocated selects values from current and/or root and returns them
26-
// in [LocatedNode] structs with their located normalized paths
26+
// in [LocatedNode] objects with their located normalized paths
2727
SelectLocated(current, root any, parent NormalizedPath) []*LocatedNode
2828

2929
// isSingular returns true for selectors that can only return a single
@@ -44,7 +44,7 @@ type Name string
4444
// Defined by the [Selector] interface.
4545
func (Name) isSingular() bool { return true }
4646

47-
// String returns a quoted string representation of n.
47+
// String returns the quoted string representation of n.
4848
func (n Name) String() string {
4949
return strconv.Quote(string(n))
5050
}
@@ -166,7 +166,7 @@ func (WildcardSelector) Select(input, _ any) []any {
166166
}
167167

168168
// SelectLocated selects the values from input and returns them with their
169-
// normalized paths in a slice of [LocatedNode] structs. Returns an empty
169+
// normalized paths in a slice of [LocatedNode] objects. Returns an empty
170170
// slice if input is not []any map[string]any. Defined by the [Selector]
171171
// interface.
172172
func (WildcardSelector) SelectLocated(input, _ any, parent NormalizedPath) []*LocatedNode {
@@ -268,7 +268,7 @@ func (i Index) writePointerTo(buf *strings.Builder) {
268268
// - [Selector]
269269
// - [fmt.Stringer]
270270
//
271-
// RFC 9535 Section 2.3.4]: https://www.rfc-editor.org/rfc/rfc9535.html#name-array-slice-selector
271+
// [RFC 9535 Section 2.3.4]: https://www.rfc-editor.org/rfc/rfc9535.html#name-array-slice-selector
272272
type SliceSelector struct {
273273
// Start of the slice; defaults to 0.
274274
start int
@@ -381,7 +381,7 @@ func (s SliceSelector) Select(input, _ any) []any {
381381
}
382382

383383
// SelectLocated selects values from input for the indexes specified by s and
384-
// returns thm with their normalized paths as [LocatedNode] structs. Returns
384+
// returns thm with their normalized paths as [LocatedNode] objects. Returns
385385
// an empty slice if input is not a slice. Indexes outside the bounds of input
386386
// will not be included in the return value. Defined by the [Selector]
387387
// interface.
@@ -495,7 +495,7 @@ func (f *FilterSelector) Select(current, root any) []any {
495495
return ret
496496
}
497497

498-
// SelectLocated selects and returns [LocatedNode] structs with values that f
498+
// SelectLocated selects and returns [LocatedNode] objects with values that f
499499
// filters from current. Filter expressions may evaluate the current value
500500
// (@), the root value ($), or any path expression. Defined by the [Selector]
501501
// interface.

spec/spec_example_test.go

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ func ExampleNonExistExpr() {
191191
// Construct a path expression as a function argument.
192192
func ExampleNodesQueryExpr() {
193193
reg := registry.New()
194-
filter := spec.Filter(spec.And(
195-
spec.Function(reg.Get("length"),
196-
spec.NodesQuery(
197-
spec.Query(false, spec.Child(spec.Index(0))),
198-
),
199-
)))
200-
fmt.Printf("%v\n", filter)
201-
// Output: ?length(@[0])
194+
fnExpr := spec.Function(
195+
reg.Get("length"),
196+
spec.NodesQuery(
197+
spec.Query(false, spec.Child(spec.Index(0))),
198+
),
199+
)
200+
fmt.Printf("%v\n", fnExpr)
201+
// Output: length(@[0])
202202
}
203203

204204
// Each [spec.FuncType] converted to one of the spec-standard function types,
@@ -332,32 +332,40 @@ func ExampleNodesType() {
332332
// Output: [hi 42 true]
333333
}
334334

335-
// Use a [spec.ParenExpr] to group together a [spec.LogicalAnd].
335+
// Use a [spec.ParenExpr] to group the result of a [LogicalOr] expression.
336336
func ExampleParenExpr() {
337337
paren := spec.Paren(
338338
spec.And(
339-
spec.Value(42),
340339
spec.Existence(
341340
spec.Query(false, spec.Child(spec.Name("answer"))),
342341
),
343342
),
343+
spec.And(
344+
spec.Existence(
345+
spec.Query(false, spec.Child(spec.Name("question"))),
346+
),
347+
),
344348
)
345349
fmt.Printf("%v\n", paren)
346-
// Output: (42 && @["answer"])
350+
// Output: (@["answer"] || @["question"])
347351
}
348352

349-
// Use a [spec.NotParenExpr] to negate a [spec.LogicalAnd] group.
353+
// Use a [spec.NotParenExpr] to negate the result of a [LogicalOr] expression.
350354
func ExampleNotParenExpr() {
351355
not := spec.NotParen(
352356
spec.And(
353-
spec.Value(42),
354357
spec.Existence(
355358
spec.Query(false, spec.Child(spec.Name("answer"))),
356359
),
357360
),
361+
spec.And(
362+
spec.Existence(
363+
spec.Query(false, spec.Child(spec.Name("question"))),
364+
),
365+
),
358366
)
359367
fmt.Printf("%v\n", not)
360-
// Output: !(42 && @["answer"])
368+
// Output: !(@["answer"] || @["question"])
361369
}
362370

363371
// Compare a normalized JSONPath and its JSON Pointer equivalent.
@@ -386,7 +394,7 @@ func ExampleSingularQueryExpr() {
386394
// Output: $["profile"]["contacts"][0]["email"]
387395
}
388396

389-
// Create [spec.ValueType]s of each supported JSON type.
397+
// Create A [spec.ValueType] for each supported JSON type.
390398
func ExampleValueType() {
391399
for _, val := range []any{
392400
"hello",
@@ -411,6 +419,45 @@ func ExampleValueType() {
411419
// [1 2 false]
412420
}
413421

422+
func ExampleLocatedNode() {
423+
// Query all "author" object properties.
424+
p := jsonpath.New(spec.Query(
425+
true,
426+
spec.Descendant(spec.Name("author")),
427+
))
428+
429+
// Select LocatedNodes from unmarshaled JSON input.
430+
store := bookstore()
431+
nodes := p.SelectLocated(store)
432+
433+
// Show the LocatedNodes.
434+
items, err := json.MarshalIndent(nodes, "", " ")
435+
if err != nil {
436+
log.Fatal(err)
437+
}
438+
fmt.Printf("%s\n", items)
439+
440+
// Output:
441+
// [
442+
// {
443+
// "node": "Nigel Rees",
444+
// "path": "$['store']['book'][0]['author']"
445+
// },
446+
// {
447+
// "node": "Evelyn Waugh",
448+
// "path": "$['store']['book'][1]['author']"
449+
// },
450+
// {
451+
// "node": "Herman Melville",
452+
// "path": "$['store']['book'][2]['author']"
453+
// },
454+
// {
455+
// "node": "J. R. R. Tolkien",
456+
// "path": "$['store']['book'][3]['author']"
457+
// }
458+
// ]
459+
}
460+
414461
// bookstore returns an unmarshaled JSON object.
415462
func bookstore() any {
416463
src := []byte(`{

0 commit comments

Comments
 (0)