Skip to content

Commit 2ef59bc

Browse files
committed
Finish examples
1 parent 95454c0 commit 2ef59bc

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed

spec/filter.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ func (p *ParenExpr) writeTo(buf *strings.Builder) {
114114
buf.WriteRune(')')
115115
}
116116

117+
// String returns the string representation of p.
118+
func (p *ParenExpr) String() string {
119+
var buf strings.Builder
120+
p.writeTo(&buf)
121+
return buf.String()
122+
}
123+
117124
// NotParenExpr represents a parenthesized expression preceded with a !.
118125
type NotParenExpr struct {
119126
LogicalOr
@@ -131,6 +138,13 @@ func (np *NotParenExpr) writeTo(buf *strings.Builder) {
131138
buf.WriteRune(')')
132139
}
133140

141+
// String returns the string representation of np.
142+
func (np *NotParenExpr) String() string {
143+
var buf strings.Builder
144+
np.writeTo(&buf)
145+
return buf.String()
146+
}
147+
134148
// testFilter returns false if the np.LogicalOrExpression returns true and
135149
// true if it returns false.
136150
func (np *NotParenExpr) testFilter(current, root any) bool {

spec/function.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ func (nt NodesType) String() string {
115115
return fmt.Sprintf("%v", []any(nt))
116116
}
117117

118-
// LogicalType is a JSONPath type that represents true or false.
118+
// LogicalType is a JSONPath type that represents true or false. Generally
119+
// used as a function return value or argument.
119120
type LogicalType uint8
120121

121122
const (
@@ -296,7 +297,9 @@ func (la *LiteralArg) asValue(_, _ any) JSONPathValue {
296297
}
297298

298299
// SingularQueryExpr represents a query that produces a single [ValueType]
299-
// (JSON value), or nothing.
300+
// (JSON value), or nothing. Used in contexts that require a singular value,
301+
// such as comparison operations and function arguments. It therefor
302+
// implements [CompVal] and [FunctionExprArg].
300303
type SingularQueryExpr struct {
301304
// The kind of singular query, relative (from the current node) or
302305
// absolute (from the root node).

spec/normalized.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ type NormalSelector interface {
2222
}
2323

2424
// NormalizedPath represents a normalized path identifying a single value in a
25-
// JSON query argument, as [defined by RFC 9535].
25+
// JSON query argument, as [defined by RFC 9535]. Defines a simplified string
26+
// format that exclusively uses single quotation marks for names. Useful for
27+
// converting to JSON Pointer (via [NormalizedPath.Pointer]) or other JSON
28+
// pointer-type formats.
2629
//
2730
// [defined by RFC 9535]: https://www.rfc-editor.org/rfc/rfc9535#name-normalized-paths
2831
type NormalizedPath []NormalSelector

spec/spec_example_test.go

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,98 @@ func ExampleLogicalOr() {
317317
// Output: @["answer"] || 42
318318
}
319319

320-
// type LogicalType
321-
// type NodesType
322-
// type NormalizedPath
323-
// type NotParenExpr
324-
// type ParenExpr
325-
// type SingularQueryExpr
326-
// type ValueType
320+
func ExampleLogicalType() {
321+
fmt.Printf("%v\n", spec.Logical(true))
322+
fmt.Printf("%v\n", spec.Logical(false))
323+
// Output:
324+
// true
325+
// false
326+
}
327+
328+
// Use a [spec.NodesType] to create a list of nodes, a.k.a. a JSON array.
329+
func ExampleNodesType() {
330+
fmt.Printf("%v\n", spec.Nodes("hi", 42, true))
331+
// Output: [hi 42 true]
332+
}
333+
334+
// Use a [spec.ParenExpr] to group together a [spec.LogicalAnd].
335+
func ExampleParenExpr() {
336+
paren := spec.Paren(
337+
spec.And(
338+
spec.Value(42),
339+
spec.Existence(
340+
spec.Query(false, spec.Child(spec.Name("answer"))),
341+
),
342+
),
343+
)
344+
fmt.Printf("%v\n", paren)
345+
// Output: (42 && @["answer"])
346+
}
347+
348+
// Use a [spec.NotParenExpr] to negate a [spec.LogicalAnd] group.
349+
func ExampleNotParenExpr() {
350+
not := spec.NotParen(
351+
spec.And(
352+
spec.Value(42),
353+
spec.Existence(
354+
spec.Query(false, spec.Child(spec.Name("answer"))),
355+
),
356+
),
357+
)
358+
fmt.Printf("%v\n", not)
359+
// Output: !(42 && @["answer"])
360+
}
361+
362+
// Compare a normalized JSONPath and its JSON Pointer equivalent.
363+
func ExampleNormalizedPath() {
364+
norm := spec.Normalized(
365+
spec.Name("x"),
366+
spec.Index(1),
367+
spec.Name("y"),
368+
)
369+
fmt.Printf("%v\n", norm)
370+
fmt.Printf("%v\n", norm.Pointer())
371+
// Output:
372+
// $['x'][1]['y']
373+
// /x/1/y
374+
}
375+
376+
func ExampleSingularQueryExpr() {
377+
singular := spec.SingularQuery(
378+
true,
379+
spec.Name("profile"),
380+
spec.Name("contacts"),
381+
spec.Index(0),
382+
spec.Name("email"),
383+
)
384+
fmt.Printf("%v\n", singular)
385+
// Output: $["profile"]["contacts"][0]["email"]
386+
}
387+
388+
// Create [spec.ValueType]s of each supported JSON type.
389+
func ExampleValueType() {
390+
for _, val := range []any{
391+
"hello",
392+
42,
393+
98.6,
394+
json.Number("1024"),
395+
true,
396+
nil,
397+
map[string]any{"x": true},
398+
[]any{1, 2, false},
399+
} {
400+
fmt.Printf("%v\n", spec.Value(val))
401+
}
402+
// Output:
403+
// hello
404+
// 42
405+
// 98.6
406+
// 1024
407+
// true
408+
// <nil>
409+
// map[x:true]
410+
// [1 2 false]
411+
}
327412

328413
// bookstore returns an unmarshaled JSON object.
329414
func bookstore() any {

0 commit comments

Comments
 (0)