Skip to content

Commit 3e12ae7

Browse files
committed
Rename ComparisonExpr, add Wildcard(), doc stable
Rename `ComparisonExpr` to `CompExpr` to match the naming of `CompVal` and `CompOp`. Replace the `Wildcard` global variable with the `Wildcard()` constructor, which returns a singleton, but an be be changed in the future if for some reason it needs not to be a singleton. Document the stable types and constructors in the `spec` package, and note that there may be some changes to other stuff in the future (though probably not). Link to the `Wildcard` global from the `WildcardSelector` docs and document that `Registry.Get` is used internally by the parser.
1 parent 301c82a commit 3e12ae7

File tree

13 files changed

+110
-91
lines changed

13 files changed

+110
-91
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ All notable changes to this project will be documented in this file. It uses the
3434
package have been labeled as such.
3535
* Fixed links and typos in the main package documentation, and moved the
3636
registry example under `WithRegistry`.
37+
* Replaced the `spec.Wildcard` variable with a the function
38+
`spec.Wildcard()`.
3739

3840
### 📔 Notes
3941

parser/parse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func parseNameOrWildcard(lex *lexer) (spec.Selector, error) {
117117
case identifier:
118118
return spec.Name(tok.val), nil
119119
case '*':
120-
return spec.Wildcard, nil
120+
return spec.Wildcard(), nil
121121
default:
122122
return nil, unexpected(tok)
123123
}
@@ -137,7 +137,7 @@ func (p *parser) parseDescendant() (*spec.Segment, error) {
137137
case identifier:
138138
return spec.Descendant(spec.Name(tok.val)), nil
139139
case '*':
140-
return spec.Descendant(spec.Wildcard), nil
140+
return spec.Descendant(spec.Wildcard()), nil
141141
default:
142142
return nil, unexpected(tok)
143143
}
@@ -169,7 +169,7 @@ func (p *parser) parseSelectors() ([]spec.Selector, error) {
169169
}
170170
selectors = append(selectors, filter)
171171
case '*':
172-
selectors = append(selectors, spec.Wildcard)
172+
selectors = append(selectors, spec.Wildcard())
173173
case goString:
174174
selectors = append(selectors, spec.Name(tok.val))
175175
case integer:
@@ -613,7 +613,7 @@ func parseLiteral(tok token) (*spec.LiteralArg, error) {
613613
}
614614

615615
// parseComparableExpr parses a [ComparisonExpr] (comparison-expr) from lex.
616-
func (p *parser) parseComparableExpr(left spec.CompVal) (*spec.ComparisonExpr, error) {
616+
func (p *parser) parseComparableExpr(left spec.CompVal) (*spec.CompExpr, error) {
617617
// Skip blank space.
618618
lex := p.lex
619619
lex.skipBlankSpace()

parser/parse_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ func TestParseSimple(t *testing.T) {
8585
{
8686
name: "wildcard",
8787
path: "$.*",
88-
exp: spec.Query(true, spec.Child(spec.Wildcard)),
88+
exp: spec.Query(true, spec.Child(spec.Wildcard())),
8989
},
9090
{
9191
name: "wildcard_wildcard",
9292
path: "$.*.*",
9393
exp: spec.Query(
9494
true,
95-
spec.Child(spec.Wildcard),
96-
spec.Child(spec.Wildcard),
95+
spec.Child(spec.Wildcard()),
96+
spec.Child(spec.Wildcard()),
9797
),
9898
},
9999
{
@@ -102,7 +102,7 @@ func TestParseSimple(t *testing.T) {
102102
exp: spec.Query(
103103
true,
104104
spec.Child(spec.Name("x")),
105-
spec.Child(spec.Wildcard),
105+
spec.Child(spec.Wildcard()),
106106
),
107107
},
108108
{
@@ -122,23 +122,23 @@ func TestParseSimple(t *testing.T) {
122122
{
123123
name: "desc_wildcard",
124124
path: "$..*",
125-
exp: spec.Query(true, spec.Descendant(spec.Wildcard)),
125+
exp: spec.Query(true, spec.Descendant(spec.Wildcard())),
126126
},
127127
{
128128
name: "desc_wildcard_2x",
129129
path: "$..*..*",
130130
exp: spec.Query(
131131
true,
132-
spec.Descendant(spec.Wildcard),
133-
spec.Descendant(spec.Wildcard),
132+
spec.Descendant(spec.Wildcard()),
133+
spec.Descendant(spec.Wildcard()),
134134
),
135135
},
136136
{
137137
name: "desc_wildcard_name",
138138
path: "$..*.xyz",
139139
exp: spec.Query(
140140
true,
141-
spec.Descendant(spec.Wildcard),
141+
spec.Descendant(spec.Wildcard()),
142142
spec.Child(spec.Name("xyz")),
143143
),
144144
},
@@ -147,7 +147,7 @@ func TestParseSimple(t *testing.T) {
147147
path: "$.*..xyz",
148148
exp: spec.Query(
149149
true,
150-
spec.Child(spec.Wildcard),
150+
spec.Child(spec.Wildcard()),
151151
spec.Descendant(spec.Name("xyz")),
152152
),
153153
},
@@ -967,12 +967,12 @@ func TestParseSelectors(t *testing.T) {
967967
{
968968
name: "slice_wildcard",
969969
path: `$[:, *]`,
970-
exp: spec.Query(true, spec.Child(spec.Slice(), spec.Wildcard)),
970+
exp: spec.Query(true, spec.Child(spec.Slice(), spec.Wildcard())),
971971
},
972972
{
973973
name: "wildcard_slice",
974974
path: `$[ *, : ]`,
975-
exp: spec.Query(true, spec.Child(spec.Wildcard, spec.Slice())),
975+
exp: spec.Query(true, spec.Child(spec.Wildcard(), spec.Slice())),
976976
},
977977
{
978978
name: "slice_neg_start",
@@ -990,13 +990,13 @@ func TestParseSelectors(t *testing.T) {
990990
exp: spec.Query(true, spec.Child(spec.Slice(nil, nil, -2))),
991991
},
992992
{
993-
name: "index_name_slice, wildcard",
993+
name: "index_name_slice_wildcard",
994994
path: `$[3, "🦀", :3,*]`,
995995
exp: spec.Query(true, spec.Child(
996996
spec.Index(3),
997997
spec.Name("🦀"),
998998
spec.Slice(nil, 3),
999-
spec.Wildcard,
999+
spec.Wildcard(),
10001000
)),
10011001
},
10021002
{
@@ -1118,7 +1118,7 @@ func TestParseSelectors(t *testing.T) {
11181118
{
11191119
name: "wildcard_tab",
11201120
path: "$[*\t]",
1121-
exp: spec.Query(true, spec.Child(spec.Wildcard)),
1121+
exp: spec.Query(true, spec.Child(spec.Wildcard())),
11221122
},
11231123
{
11241124
name: "slice_newline",
@@ -1146,7 +1146,7 @@ func TestParseSelectors(t *testing.T) {
11461146
exp: spec.Query(true, spec.Descendant(
11471147
spec.Name("hi"),
11481148
spec.Index(2),
1149-
spec.Wildcard,
1149+
spec.Wildcard(),
11501150
spec.Slice(4, 5),
11511151
)),
11521152
},
@@ -1177,7 +1177,7 @@ func TestParseSelectors(t *testing.T) {
11771177
true,
11781178
spec.Child(spec.Name("names")),
11791179
spec.Child(spec.Name("first_name")),
1180-
spec.Descendant(spec.Wildcard),
1180+
spec.Descendant(spec.Wildcard()),
11811181
),
11821182
},
11831183
{
@@ -1214,7 +1214,7 @@ func TestParseSelectors(t *testing.T) {
12141214
{
12151215
name: "default_slice_wildcard_segment",
12161216
path: `$[::,*]`,
1217-
exp: spec.Query(true, spec.Child(spec.Slice(), spec.Wildcard)),
1217+
exp: spec.Query(true, spec.Child(spec.Slice(), spec.Wildcard())),
12181218
},
12191219
{
12201220
name: "leading_zero_index",

registry/registry.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ func (r *Registry) Register(
8787
}
8888

8989
// Get returns a reference to the registered function extension named name.
90-
// Returns nil if no function with that name has been registered.
90+
// Returns nil if no function with that name has been registered. Used by the
91+
// parser to match a function name to its implementation.
9192
func (r *Registry) Get(name string) *spec.FuncExtension {
9293
r.mu.RLock()
9394
defer r.mu.RUnlock()

spec/filter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
// BasicExpr defines the basic interface for filter expressions.
88
// Implementations:
99
//
10-
// - [ComparisonExpr]
10+
// - [CompExpr]
1111
// - [ExistExpr]
1212
// - [FuncExpr]
1313
// - [LogicalAnd]

spec/function_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ func TestFilterQuery(t *testing.T) {
420420
},
421421
{
422422
name: "current_wildcard",
423-
query: Query(false, Child(Wildcard)),
423+
query: Query(false, Child(Wildcard())),
424424
current: []any{13, 2, []any{4}},
425425
exp: []any{13, 2, []any{4}},
426426
typeKind: FuncNodes,

spec/op.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,41 @@ type CompVal interface {
4141
asValue(current, root any) PathValue
4242
}
4343

44-
// ComparisonExpr is a filter expression that compares two values, which
45-
// themselves may themselves be the output of expressions. Interfaces
46-
// implemented:
44+
// CompExpr is a filter expression that compares two values, which themselves
45+
// may themselves be the output of expressions. Interfaces implemented:
4746
//
4847
// - [BasicExpr]
4948
// - [fmt.Stringer]
50-
type ComparisonExpr struct {
49+
type CompExpr struct {
5150
left CompVal
5251
op CompOp
5352
right CompVal
5453
}
5554

56-
// Comparison creates and returns a new [ComparisonExpr] that uses op to
57-
// compare left and right.
58-
func Comparison(left CompVal, op CompOp, right CompVal) *ComparisonExpr {
59-
return &ComparisonExpr{left, op, right}
55+
// Comparison creates and returns a new [CompExpr] that uses op to compare
56+
// left and right.
57+
func Comparison(left CompVal, op CompOp, right CompVal) *CompExpr {
58+
return &CompExpr{left, op, right}
6059
}
6160

6261
// writeTo writes a string representation of ce to buf. Defined by
6362
// [stringWriter].
64-
func (ce *ComparisonExpr) writeTo(buf *strings.Builder) {
63+
func (ce *CompExpr) writeTo(buf *strings.Builder) {
6564
ce.left.writeTo(buf)
6665
fmt.Fprintf(buf, " %v ", ce.op)
6766
ce.right.writeTo(buf)
6867
}
6968

7069
// String returns the string representation of ce.
71-
func (ce *ComparisonExpr) String() string {
70+
func (ce *CompExpr) String() string {
7271
var buf strings.Builder
7372
ce.writeTo(&buf)
7473
return buf.String()
7574
}
7675

7776
// testFilter uses ce.Op to compare the values returned by ce.Left and
7877
// ce.Right relative to current and root. Defined by [BasicExpr].
79-
func (ce *ComparisonExpr) testFilter(current, root any) bool {
78+
func (ce *CompExpr) testFilter(current, root any) bool {
8079
left := ce.left.asValue(current, root)
8180
right := ce.right.asValue(current, root)
8281
switch ce.op {

0 commit comments

Comments
 (0)