Skip to content

Commit ce0c397

Browse files
committed
Remove spec FuncType
It is unnecessary.
1 parent 28f0dd2 commit ce0c397

17 files changed

+243
-320
lines changed

parser/parse.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ func (p *parser) parseFunctionFilterExpr(ident token) (spec.BasicExpr, error) {
421421
return nil, err
422422
}
423423

424-
if f.ResultType() == spec.ArgLogical {
424+
if f.ResultType() == spec.FuncLogical {
425425
return f, nil
426426
}
427427

@@ -652,7 +652,7 @@ func (p *parser) parseComparableVal(tok token) (spec.CompVal, error) {
652652
if err != nil {
653653
return nil, err
654654
}
655-
if f.ResultType() == spec.ArgLogical {
655+
if f.ResultType() == spec.FuncLogical {
656656
return nil, makeError(tok, "cannot compare result of logical function")
657657
}
658658
return f, nil

parser/parse_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func TestParseFilter(t *testing.T) {
199199
reg := registry.New()
200200
_ = reg.Register(
201201
"__true",
202-
spec.ArgLogical,
202+
spec.FuncLogical,
203203
func([]spec.FuncExprArg) error { return nil },
204204
func([]spec.JSONPathValue) spec.JSONPathValue {
205205
return spec.LogicalTrue
@@ -439,7 +439,7 @@ func TestParseFilter(t *testing.T) {
439439
),
440440
)),
441441
},
442-
// FuncExpr
442+
// FunExpr
443443
{
444444
name: "function_current",
445445
query: "__true(@)",
@@ -724,7 +724,7 @@ func TestParseFilter(t *testing.T) {
724724
{
725725
name: "invalid_function_arg",
726726
query: `length(@[1, 2])`,
727-
err: `jsonpath: function length() cannot convert argument to ValueType at position 7`,
727+
err: `jsonpath: function length() cannot convert argument to Value at position 7`,
728728
},
729729
{
730730
name: "too_many_function_args",

path_example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func ExampleParser_functionExtension() {
239239
reg := registry.New()
240240
err := reg.Register(
241241
"first", // name
242-
spec.ArgValue, // returns a single value
242+
spec.FuncValue, // returns a single value
243243
validateFirstArgs, // parse-time validation defined below
244244
firstFunc, // function defined below
245245
)
@@ -275,8 +275,8 @@ func validateFirstArgs(fea []spec.FuncExprArg) error {
275275
return fmt.Errorf("expected 1 argument but found %v", len(fea))
276276
}
277277

278-
if !fea[0].ResultType().ConvertsTo(spec.FuncNodes) {
279-
return errors.New("cannot convert argument to PathNodes")
278+
if !fea[0].ResultType().ConvertsToNodes() {
279+
return errors.New("cannot convert argument to nodes")
280280
}
281281

282282
return nil

registry/funcs.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import (
1111
)
1212

1313
// checkLengthArgs checks the argument expressions to length() and returns an
14-
// error if there is not exactly one expression that results in a
15-
// [spec.FuncValue]-compatible value.
14+
// error if there is not exactly one expression that results in a compatible
15+
// [spec.FuncValue] value.
1616
func checkLengthArgs(fea []spec.FuncExprArg) error {
1717
if len(fea) != 1 {
1818
return fmt.Errorf("expected 1 argument but found %v", len(fea))
1919
}
2020

2121
kind := fea[0].ResultType()
22-
if !kind.ConvertsTo(spec.FuncValue) {
23-
return errors.New("cannot convert argument to ValueType")
22+
if !kind.ConvertsToValue() {
23+
return errors.New("cannot convert argument to Value")
2424
}
2525

2626
return nil
@@ -64,8 +64,8 @@ func checkCountArgs(fea []spec.FuncExprArg) error {
6464
}
6565

6666
kind := fea[0].ResultType()
67-
if !kind.ConvertsTo(spec.FuncNodes) {
68-
return errors.New("cannot convert argument to PathNodes")
67+
if !kind.ConvertsToNodes() {
68+
return errors.New("cannot convert argument to Nodes")
6969
}
7070

7171
return nil
@@ -88,8 +88,8 @@ func checkValueArgs(fea []spec.FuncExprArg) error {
8888
}
8989

9090
kind := fea[0].ResultType()
91-
if !kind.ConvertsTo(spec.FuncNodes) {
92-
return errors.New("cannot convert argument to PathNodes")
91+
if !kind.ConvertsToNodes() {
92+
return errors.New("cannot convert argument to Nodes")
9393
}
9494

9595
return nil
@@ -109,8 +109,8 @@ func valueFunc(jv []spec.JSONPathValue) spec.JSONPathValue {
109109
}
110110

111111
// checkMatchArgs checks the argument expressions to match() and returns an
112-
// error if there are not exactly two expressions that result in
113-
// [spec.FuncValue]-compatible values.
112+
// error if there are not exactly two expressions that result in compatible
113+
// [spec.FuncValue] values.
114114
func checkMatchArgs(fea []spec.FuncExprArg) error {
115115
const matchArgLen = 2
116116
if len(fea) != matchArgLen {
@@ -119,8 +119,8 @@ func checkMatchArgs(fea []spec.FuncExprArg) error {
119119

120120
for i, arg := range fea {
121121
kind := arg.ResultType()
122-
if !kind.ConvertsTo(spec.FuncValue) {
123-
return fmt.Errorf("cannot convert argument %v to PathNodes", i+1)
122+
if !kind.ConvertsToValue() {
123+
return fmt.Errorf("cannot convert argument %v to Value", i+1)
124124
}
125125
}
126126

@@ -144,8 +144,8 @@ func matchFunc(jv []spec.JSONPathValue) spec.JSONPathValue {
144144
}
145145

146146
// checkSearchArgs checks the argument expressions to search() and returns an
147-
// error if there are not exactly two expressions that result in
148-
// [spec.FuncValue]-compatible values.
147+
// error if there are not exactly two expressions that result in compatible
148+
// [spec.FuncValue] values.
149149
func checkSearchArgs(fea []spec.FuncExprArg) error {
150150
const searchArgLen = 2
151151
if len(fea) != searchArgLen {
@@ -154,8 +154,8 @@ func checkSearchArgs(fea []spec.FuncExprArg) error {
154154

155155
for i, arg := range fea {
156156
kind := arg.ResultType()
157-
if !kind.ConvertsTo(spec.FuncValue) {
158-
return fmt.Errorf("cannot convert argument %v to PathNodes", i+1)
157+
if !kind.ConvertsToValue() {
158+
return fmt.Errorf("cannot convert argument %v to Value", i+1)
159159
}
160160
}
161161

registry/funcs_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ func TestCheckSingularFuncArgs(t *testing.T) {
144144
{
145145
name: "literal_string",
146146
expr: []spec.FuncExprArg{spec.Literal(nil)},
147-
countErr: "cannot convert argument to PathNodes",
148-
valueErr: "cannot convert argument to PathNodes",
147+
countErr: "cannot convert argument to Nodes",
148+
valueErr: "cannot convert argument to Nodes",
149149
},
150150
{
151151
name: "singular_query",
@@ -165,16 +165,16 @@ func TestCheckSingularFuncArgs(t *testing.T) {
165165
),
166166
spec.Literal("hi"),
167167
)},
168-
lengthErr: "cannot convert argument to ValueType",
169-
countErr: "cannot convert argument to PathNodes",
170-
valueErr: "cannot convert argument to PathNodes",
168+
lengthErr: "cannot convert argument to Value",
169+
countErr: "cannot convert argument to Nodes",
170+
valueErr: "cannot convert argument to Nodes",
171171
},
172172
{
173173
name: "logical_or",
174174
expr: []spec.FuncExprArg{spec.LogicalOr{}},
175-
lengthErr: "cannot convert argument to ValueType",
176-
countErr: "cannot convert argument to PathNodes",
177-
valueErr: "cannot convert argument to PathNodes",
175+
lengthErr: "cannot convert argument to Value",
176+
countErr: "cannot convert argument to Nodes",
177+
valueErr: "cannot convert argument to Nodes",
178178
},
179179
} {
180180
t.Run(tc.name, func(t *testing.T) {
@@ -243,12 +243,12 @@ func TestCheckRegexFuncArgs(t *testing.T) {
243243
{
244244
name: "logical_or_1",
245245
expr: []spec.FuncExprArg{&spec.LogicalOr{}, spec.Literal("hi")},
246-
err: "cannot convert argument 1 to PathNodes",
246+
err: "cannot convert argument 1 to Value",
247247
},
248248
{
249249
name: "logical_or_2",
250250
expr: []spec.FuncExprArg{spec.Literal("hi"), spec.LogicalOr{}},
251-
err: "cannot convert argument 2 to PathNodes",
251+
err: "cannot convert argument 2 to Value",
252252
},
253253
{
254254
name: "singular_query_literal",
@@ -282,7 +282,7 @@ func TestCheckRegexFuncArgs(t *testing.T) {
282282
),
283283
spec.Literal("hi"),
284284
},
285-
err: "cannot convert argument 1 to PathNodes",
285+
err: "cannot convert argument 1 to Value",
286286
},
287287
{
288288
name: "func_expr_2",
@@ -294,7 +294,7 @@ func TestCheckRegexFuncArgs(t *testing.T) {
294294
spec.Literal("hi"),
295295
),
296296
},
297-
err: "cannot convert argument 2 to PathNodes",
297+
err: "cannot convert argument 2 to Value",
298298
},
299299
} {
300300
t.Run(tc.name, func(t *testing.T) {

registry/registry.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,31 @@ func New() *Registry {
3838
funcs: map[string]*Function{
3939
"length": {
4040
name: "length",
41-
resultType: spec.ArgValue,
41+
resultType: spec.FuncValue,
4242
validator: checkLengthArgs,
4343
evaluator: lengthFunc,
4444
},
4545
"count": {
4646
name: "count",
47-
resultType: spec.ArgValue,
47+
resultType: spec.FuncValue,
4848
validator: checkCountArgs,
4949
evaluator: countFunc,
5050
},
5151
"value": {
5252
name: "value",
53-
resultType: spec.ArgValue,
53+
resultType: spec.FuncValue,
5454
validator: checkValueArgs,
5555
evaluator: valueFunc,
5656
},
5757
"match": {
5858
name: "match",
59-
resultType: spec.ArgLogical,
59+
resultType: spec.FuncLogical,
6060
validator: checkMatchArgs,
6161
evaluator: matchFunc,
6262
},
6363
"search": {
6464
name: "search",
65-
resultType: spec.ArgLogical,
65+
resultType: spec.FuncLogical,
6666
validator: checkSearchArgs,
6767
evaluator: searchFunc,
6868
},
@@ -85,7 +85,7 @@ var ErrRegister = errors.New("register")
8585
// already contains name.
8686
func (r *Registry) Register(
8787
name string,
88-
resultType spec.ArgType,
88+
resultType spec.FuncType,
8989
validator Validator,
9090
evaluator Evaluator,
9191
) error {
@@ -126,7 +126,7 @@ type Function struct {
126126
name string
127127

128128
// resultType defines the type of the function return value.
129-
resultType spec.ArgType
129+
resultType spec.FuncType
130130

131131
// validator executes at parse time to validate that all the args to
132132
// the function are compatible with the function.
@@ -147,7 +147,7 @@ type Function struct {
147147
// against args and returns the result defined by resultType.
148148
func NewFunction(
149149
name string,
150-
resultType spec.ArgType,
150+
resultType spec.FuncType,
151151
validator func(args []spec.FuncExprArg) error,
152152
evaluator func(args []spec.JSONPathValue,
153153
) spec.JSONPathValue,
@@ -159,7 +159,7 @@ func NewFunction(
159159
func (f *Function) Name() string { return f.name }
160160

161161
// ResultType returns the data type of the function return value.
162-
func (f *Function) ResultType() spec.ArgType { return f.resultType }
162+
func (f *Function) ResultType() spec.FuncType { return f.resultType }
163163

164164
// Evaluate executes the function against args and returns the result of type
165165
// [ResultType].

registry/registry_example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Example() {
1616
reg := registry.New()
1717
err := reg.Register(
1818
"first", // function name
19-
spec.ArgValue, // returns a single value
19+
spec.FuncValue, // returns a single value
2020
validateFirstArgs, // parse-time validation defined below
2121
firstFunc, // function defined below
2222
)
@@ -35,8 +35,8 @@ func validateFirstArgs(fea []spec.FuncExprArg) error {
3535
return fmt.Errorf("expected 1 argument but found %v", len(fea))
3636
}
3737

38-
if !fea[0].ResultType().ConvertsTo(spec.FuncNodes) {
39-
return errors.New("cannot convert argument to PathNodes")
38+
if !fea[0].ResultType().ConvertsToNodes() {
39+
return errors.New("cannot convert argument to Nodes")
4040
}
4141

4242
return nil

registry/registry_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,43 @@ func TestRegistry(t *testing.T) {
1616

1717
for _, tc := range []struct {
1818
name string
19-
rType spec.ArgType
19+
rType spec.FuncType
2020
expr []spec.FuncExprArg
2121
args []spec.JSONPathValue
2222
exp any
2323
}{
2424
// RFC 9535-defined functions.
2525
{
2626
name: "length",
27-
rType: spec.ArgValue,
27+
rType: spec.FuncValue,
2828
expr: []spec.FuncExprArg{spec.Literal("foo")},
2929
args: []spec.JSONPathValue{spec.Value("foo")},
3030
exp: spec.Value(3),
3131
},
3232
{
3333
name: "count",
34-
rType: spec.ArgValue,
34+
rType: spec.FuncValue,
3535
expr: []spec.FuncExprArg{&spec.SingularQueryExpr{}},
3636
args: []spec.JSONPathValue{spec.Nodes(1, 2)},
3737
exp: spec.Value(2),
3838
},
3939
{
4040
name: "value",
41-
rType: spec.ArgValue,
41+
rType: spec.FuncValue,
4242
expr: []spec.FuncExprArg{&spec.SingularQueryExpr{}},
4343
args: []spec.JSONPathValue{spec.Nodes(42)},
4444
exp: spec.Value(42),
4545
},
4646
{
4747
name: "match",
48-
rType: spec.ArgLogical,
48+
rType: spec.FuncLogical,
4949
expr: []spec.FuncExprArg{spec.Literal("foo"), spec.Literal(".*")},
5050
args: []spec.JSONPathValue{spec.Value("foo"), spec.Value(".*")},
5151
exp: spec.LogicalTrue,
5252
},
5353
{
5454
name: "search",
55-
rType: spec.ArgLogical,
55+
rType: spec.FuncLogical,
5656
expr: []spec.FuncExprArg{spec.Literal("foo"), spec.Literal(".")},
5757
args: []spec.JSONPathValue{spec.Value("foo"), spec.Value(".")},
5858
exp: spec.LogicalTrue,
@@ -103,7 +103,7 @@ func TestRegisterErr(t *testing.T) {
103103
} {
104104
t.Run(tc.name, func(t *testing.T) {
105105
t.Parallel()
106-
err := reg.Register(tc.fnName, spec.ArgValue, tc.valid, tc.eval)
106+
err := reg.Register(tc.fnName, spec.FuncValue, tc.valid, tc.eval)
107107
r.ErrorIs(err, ErrRegister, tc.name)
108108
r.EqualError(err, tc.err, tc.name)
109109
})
@@ -124,7 +124,7 @@ func TestFunction(t *testing.T) {
124124
{
125125
name: "valid_err_value",
126126
fn: NewFunction(
127-
"xyz", spec.ArgValue,
127+
"xyz", spec.FuncValue,
128128
func([]spec.FuncExprArg) error { return errors.New("oops") },
129129
func([]spec.JSONPathValue) spec.JSONPathValue { return spec.Value(42) },
130130
),
@@ -135,7 +135,7 @@ func TestFunction(t *testing.T) {
135135
{
136136
name: "no_valid_err_nodes",
137137
fn: NewFunction(
138-
"abc", spec.ArgNodes,
138+
"abc", spec.FuncNodes,
139139
func([]spec.FuncExprArg) error { return nil },
140140
func([]spec.JSONPathValue) spec.JSONPathValue { return spec.Nodes("hi") },
141141
),

0 commit comments

Comments
 (0)