Skip to content

Commit 624b5a3

Browse files
committed
Expand conversion examples, docs
1 parent 13a8385 commit 624b5a3

File tree

4 files changed

+70
-20
lines changed

4 files changed

+70
-20
lines changed

path.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (p *Path) SelectLocated(input any) LocatedNodeList {
6262
return p.q.SelectLocated(nil, input, spec.Normalized())
6363
}
6464

65-
// Parser parses JSONPath strings into [Path]s.
65+
// Parser parses JSONPath strings into [Path] objects.
6666
type Parser struct {
6767
reg *registry.Registry
6868
}

spec/function.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,26 @@ const (
4343
FuncLogical
4444
)
4545

46-
// ConvertsToValue returns true if ft can be converted to a [ValueType]. Used
47-
// by [github.com/theory/jsonpath/registry.NewFunction] validator functions
48-
// to ensure the compatibility of parameter expressions.
46+
// ConvertsToValue returns true if ft can be converted to a [ValueType]. In
47+
// other words, ft values can safely be passed to [ValueFrom] without it
48+
// panicking. Used by [github.com/theory/jsonpath/registry.NewFunction]
49+
// validator functions to ensure the compatibility of parameter expressions.
4950
func (ft FuncType) ConvertsToValue() bool {
5051
return ft == FuncValue || ft == FuncLiteral || ft == FuncSingularQuery
5152
}
5253

5354
// ConvertsToLogical returns true if ft can be converted to a [LogicalType].
54-
// Used by [github.com/theory/jsonpath/registry.NewFunction] validator
55-
// functions to ensure the compatibility of parameter expressions.
55+
// In other words, ft values can safely be passed to [LogicalFrom] without it
56+
// panicking. Used by [github.com/theory/jsonpath/registry.NewFunction]
57+
// validator functions to ensure the compatibility of parameter expressions.
5658
func (ft FuncType) ConvertsToLogical() bool {
5759
return ft == FuncLogical || ft == FuncNodes || ft == FuncSingularQuery
5860
}
5961

60-
// ConvertsToNodes returns true if ft can be converted to a [NodesType]. Used
61-
// by [github.com/theory/jsonpath/registry.NewFunction] validator functions to
62-
// ensure the compatibility of parameter expressions.
62+
// ConvertsToNodes returns true if ft can be converted to a [NodesType]. In
63+
// other words, ft values can safely be passed to [NodesFrom] without it
64+
// panicking. Used by [github.com/theory/jsonpath/registry.NewFunction]
65+
// validator functions to ensure the compatibility of parameter expressions.
6366
func (ft FuncType) ConvertsToNodes() bool {
6467
return ft == FuncNodes || ft == FuncSingularQuery
6568
}
@@ -103,9 +106,11 @@ func Nodes(val ...any) NodesType {
103106
func (NodesType) FuncType() FuncType { return FuncNodes }
104107

105108
// NodesFrom attempts to convert value to a [NodesType] and panics if it
106-
// cannot. Used by [github.com/theory/jsonpath/registry.NewFunction] evaluator
107-
// functions to convert types, which should have already been validated by the
108-
// validator function.
109+
// cannot. Should only be used for [FuncType] objects from [FuncExprArg]
110+
// ResultType() and [JSONPathValue] FuncType() that return true from
111+
// [FuncType.ConvertsToNodes]. Use in [github.com/theory/jsonpath/registry.NewFunction]
112+
// evaluator functions to convert types, which should have already been
113+
// validated by the validator function.
109114
func NodesFrom(value JSONPathValue) NodesType {
110115
switch v := value.(type) {
111116
case NodesType:
@@ -163,9 +168,11 @@ func (lt LogicalType) Bool() bool { return lt == LogicalTrue }
163168
func (LogicalType) FuncType() FuncType { return FuncLogical }
164169

165170
// LogicalFrom attempts to convert value to a [LogicalType] and panics if it
166-
// cannot. Used by [github.com/theory/jsonpath/registry.NewFunction] evaluator
167-
// functions to convert types, which should have already been validated by the
168-
// validator function.
171+
// cannot. Should only be used for [FuncType] objects from [FuncExprArg]
172+
// ResultType() and [JSONPathValue] FuncType() that return true from
173+
// [FuncType.ConvertsToLogical]. Use in [github.com/theory/jsonpath/registry.NewFunction]
174+
// evaluator functions to convert types, which should have already been
175+
// validated by the validator function.
169176
func LogicalFrom(value any) LogicalType {
170177
switch v := value.(type) {
171178
case LogicalType:
@@ -222,9 +229,11 @@ func (vt *ValueType) String() string { return fmt.Sprintf("%v", vt.any) }
222229
func (*ValueType) FuncType() FuncType { return FuncValue }
223230

224231
// ValueFrom attempts to convert value to a ValueType and panics if it cannot.
225-
// Used by [github.com/theory/jsonpath/registry.NewFunction] evaluator
226-
// functions to convert types, which should have already been validated by the
227-
// validator function.
232+
// Should only be used for [FuncType] objects from [FuncExprArg] ResultType()
233+
// and [JSONPathValue] FuncType() that return true from
234+
// [FuncType.ConvertsToValue]. Use in [github.com/theory/jsonpath/registry.NewFunction]
235+
// evaluator functions to convert types, which should have already been
236+
// validated by the validator function.
228237
func ValueFrom(value JSONPathValue) *ValueType {
229238
switch v := value.(type) {
230239
case *ValueType:

spec/op.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func (ce *ComparisonExpr) testFilter(current, root any) bool {
9797
}
9898
}
9999

100-
// equalTo returns true if left and right are nils, or if both are
101-
// [ValueType]s and [valueEqualTo] returns true for their underlying values.
100+
// equalTo returns true if left and right are nils, or if both are [ValueType]
101+
// objects and [valueEqualTo] returns true for their underlying values.
102102
// Otherwise it returns false.
103103
func equalTo(left, right JSONPathValue) bool {
104104
switch left := left.(type) {

spec/spec_example_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,47 @@ func ExampleValueType() {
419419
// [1 2 false]
420420
}
421421

422+
func ExampleValueFrom() {
423+
for _, val := range []spec.JSONPathValue{
424+
spec.Value("hello"), // converts to value
425+
spec.Nodes(1, 2, 3), // does not convert to value
426+
spec.Logical(false), // does not convert to value
427+
} {
428+
if val.FuncType().ConvertsToValue() {
429+
fmt.Printf("val: %v\n", spec.ValueFrom(val))
430+
}
431+
}
432+
// Output: val: hello
433+
}
434+
435+
func ExampleNodesFrom() {
436+
for _, val := range []spec.JSONPathValue{
437+
spec.Value("hello"), // does not convert to nodes
438+
spec.Nodes(1, 2, 3), // converts to nodes
439+
spec.Logical(false), // does not convert to nodes
440+
} {
441+
if val.FuncType().ConvertsToNodes() {
442+
fmt.Printf("nodes: %v\n", spec.NodesFrom(val))
443+
}
444+
}
445+
// Output: nodes: [1 2 3]
446+
}
447+
448+
func ExampleLogicalFrom() {
449+
for _, val := range []spec.JSONPathValue{
450+
spec.Value("hello"), // does not convert to logical
451+
spec.Nodes(1, 2, 3), // converts to logical (existence)
452+
spec.Logical(false), // converts to logical
453+
} {
454+
if val.FuncType().ConvertsToLogical() {
455+
fmt.Printf("logical: %v\n", spec.LogicalFrom(val))
456+
}
457+
}
458+
// Output:
459+
// logical: true
460+
// logical: false
461+
}
462+
422463
func ExampleLocatedNode() {
423464
// Query all "author" object properties.
424465
p := jsonpath.New(spec.Query(

0 commit comments

Comments
 (0)