-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
255 lines (218 loc) · 5.87 KB
/
Copy pathvalidate.go
File metadata and controls
255 lines (218 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package jpath
import (
"errors"
"fmt"
)
type (
exprContext uint8
unaryArgValidator func(name string, arg FilterExpr) error
)
const (
contextLogical exprContext = iota
contextComparisonOperand
contextFunctionArg
)
var (
// ErrUnknownSelector indicates an unsupported AST selector kind
ErrUnknownSelector = errors.New("unknown selector kind")
// ErrUnknownOperator indicates an unsupported AST operator
ErrUnknownOperator = errors.New("unknown operator")
// ErrUnknownExpr indicates an unsupported filter AST expression
ErrUnknownExpr = errors.New("unknown filter expression")
// ErrLiteralMustBeCompared is raised for bare literals in logical context
ErrLiteralMustBeCompared = errors.New("literal must be compared")
// ErrCompRequiresSingularQuery is raised for non-singular comparisons
ErrCompRequiresSingularQuery = errors.New(
"comparison requires singular query",
)
// ErrInvalidFuncArity is raised when function arity is invalid
ErrInvalidFuncArity = errors.New("invalid function arity")
// ErrFuncResultMustBeCompared is raised for logical use without compare
ErrFuncResultMustBeCompared = errors.New("function result must be compared")
// ErrFuncResultMustNotBeCompared is raised for prohibited comparisons
ErrFuncResultMustNotBeCompared = errors.New(
"function result must not be compared",
)
// ErrFuncRequiresSingularQuery is raised for singular-path requirements
ErrFuncRequiresSingularQuery = errors.New(
"function requires singular query",
)
// ErrFuncRequiresQueryArgument is raised for query-arg requirements
ErrFuncRequiresQueryArgument = errors.New(
"function requires query argument",
)
)
func validatePath(path *PathExpr, reg *Registry) error {
for _, seg := range path.Segments {
for _, sel := range seg.Selectors {
if sel.Kind != SelectorFilter {
continue
}
if err := validateExpr(
sel.Filter, contextLogical, false, reg,
); err != nil {
return err
}
}
}
return nil
}
func validateExpr(
ex FilterExpr, ctx exprContext, inComparison bool, reg *Registry,
) error {
switch v := ex.(type) {
case *LiteralExpr:
if ctx == contextLogical {
return ErrLiteralMustBeCompared
}
return nil
case *PathValueExpr:
if inComparison && !isSingularPath(v.Path) {
return ErrCompRequiresSingularQuery
}
return nil
case *UnaryExpr:
return validateExpr(v.Expr, contextLogical, false, reg)
case *BinaryExpr:
switch v.Op {
case OpAnd, OpOr:
if err := validateExpr(
v.Left, contextLogical, false, reg,
); err != nil {
return err
}
return validateExpr(v.Right, contextLogical, false, reg)
case OpEq, OpNe, OpLt, OpLte, OpGt, OpGte:
if err := validateExpr(
v.Left, contextComparisonOperand, true, reg,
); err != nil {
return err
}
return validateExpr(
v.Right, contextComparisonOperand, true, reg,
)
default:
return fmt.Errorf("%w: %s", ErrUnknownOperator, v.Op)
}
case *FuncExpr:
if err := validateFunction(v, ctx, inComparison, reg); err != nil {
return err
}
for _, a := range v.Args {
if err := validateExpr(
a, contextFunctionArg, false, reg,
); err != nil {
return err
}
}
return nil
default:
return ErrUnknownExpr
}
}
func validateFunction(
f *FuncExpr, ctx exprContext, inComparison bool, reg *Registry,
) error {
def, ok := reg.function(f.Name)
if !ok {
return fmt.Errorf("%w: %s", ErrUnknownFunc, f.Name)
}
if def.Validate == nil {
return nil
}
return def.Validate(f.Args, functionUse(ctx), inComparison)
}
func functionUse(ctx exprContext) FunctionUse {
switch ctx {
case contextLogical:
return FunctionUseLogical
case contextComparisonOperand:
return FunctionUseComparisonOperand
default:
return FunctionUseArgument
}
}
func isSingularPath(path *PathExpr) bool {
for _, seg := range path.Segments {
if !isSingularSegment(seg) {
return false
}
}
return true
}
func isSingularSegment(seg *SegmentExpr) bool {
if seg.Descendant || len(seg.Selectors) != 1 {
return false
}
kind := seg.Selectors[0].Kind
return kind == SelectorName || kind == SelectorIndex
}
func validateMatchSearchFunction(
args []FilterExpr, _ FunctionUse, inComparison bool,
) error {
if err := validateFunctionArity("match/search", args, 2); err != nil {
return err
}
if inComparison {
return fmt.Errorf("%w: match/search", ErrFuncResultMustNotBeCompared)
}
return nil
}
func validateUnaryCompared(
name string, args []FilterExpr, use FunctionUse, inComparison bool,
argValidator unaryArgValidator,
) error {
if err := validateFunctionArity(name, args, 1); err != nil {
return err
}
if err := validateComparedUse(name, use, inComparison); err != nil {
return err
}
return argValidator(name, args[0])
}
func validateUnaryComparedReq(
name string, args []FilterExpr, use FunctionUse, inComparison bool,
) error {
return validateUnaryCompared(
name, args, use, inComparison, validateQueryArg,
)
}
func validateUnaryComparedSingular(
name string, args []FilterExpr, use FunctionUse, inComparison bool,
) error {
return validateUnaryCompared(
name, args, use, inComparison, validateSingularQueryArg,
)
}
func validateFunctionArity(name string, args []FilterExpr, want int) error {
if len(args) == want {
return nil
}
return fmt.Errorf("%w: %s", ErrInvalidFuncArity, name)
}
func validateComparedUse(
name string, use FunctionUse, inComparison bool,
) error {
if inComparison || use != FunctionUseLogical {
return nil
}
return fmt.Errorf("%w: %s", ErrFuncResultMustBeCompared, name)
}
func validateQueryArg(name string, arg FilterExpr) error {
if _, ok := arg.(*PathValueExpr); ok {
return nil
}
return fmt.Errorf(
"%w: %s requires query argument", ErrFuncRequiresQueryArgument, name,
)
}
func validateSingularQueryArg(name string, arg FilterExpr) error {
pv, ok := arg.(*PathValueExpr)
if !ok {
return nil
}
if isSingularPath(pv.Path) {
return nil
}
return fmt.Errorf("%w: %s", ErrFuncRequiresSingularQuery, name)
}