forked from Peripli/service-manager
/
selection.go
332 lines (298 loc) · 11 KB
/
selection.go
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright 2018 The Service Manager Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package query
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"github.com/Peripli/service-manager/pkg/util"
"github.com/Peripli/service-manager/pkg/web"
)
// Operator is a query operator
type Operator string
const (
// EqualsOperator takes two operands and tests if they are equal
EqualsOperator Operator = "="
// NotEqualsOperator takes two operands and tests if they are not equal
NotEqualsOperator Operator = "!="
// GreaterThanOperator takes two operands and tests if the left is greater than the right
GreaterThanOperator Operator = "gt"
// LessThanOperator takes two operands and tests if the left is lesser than the right
LessThanOperator Operator = "lt"
// InOperator takes two operands and tests if the left is contained in the right
InOperator Operator = "in"
// NotInOperator takes two operands and tests if the left is not contained in the right
NotInOperator Operator = "notin"
// EqualsOrNilOperator takes two operands and tests if the left is equal to the right, or if the left is nil
EqualsOrNilOperator Operator = "eqornil"
)
// IsMultiVariate returns true if the operator requires right operand with multiple values
func (op Operator) IsMultiVariate() bool {
return op == InOperator || op == NotInOperator
}
// IsNullable returns true if the operator can check if the left operand is nil
func (op Operator) IsNullable() bool {
return op == EqualsOrNilOperator
}
// IsNumeric returns true if the operator works only with numeric operands
func (op Operator) IsNumeric() bool {
return op == LessThanOperator || op == GreaterThanOperator
}
var operators = []Operator{EqualsOperator, NotEqualsOperator, InOperator,
NotInOperator, GreaterThanOperator, LessThanOperator, EqualsOrNilOperator}
const (
// OpenBracket is the token that denotes the beginning of a multivariate operand
OpenBracket rune = '['
// OpenBracket is the token that denotes the end of a multivariate operand
CloseBracket rune = ']'
// Separator is the separator between field and label queries
Separator rune = '|'
// OperandSeparator is the separator between the operator and the operands
OperandSeparator rune = ' '
)
// CriterionType is a type of criteria to be applied when querying
type CriterionType string
const (
// FieldQuery denotes that the query should be executed on the entity's fields
FieldQuery CriterionType = "fieldQuery"
// LabelQuery denotes that the query should be executed on the entity's labels
LabelQuery CriterionType = "labelQuery"
)
var supportedQueryTypes = []CriterionType{FieldQuery, LabelQuery}
// Criterion is a single part of a query criteria
type Criterion struct {
// LeftOp is the left operand in the query
LeftOp string
// Operator is the query operator
Operator Operator
// RightOp is the right operand in the query which can be multivariate
RightOp []string
// Type is the type of the query
Type CriterionType
}
// ByField constructs a new criterion for field querying
func ByField(operator Operator, leftOp string, rightOp ...string) Criterion {
return newCriterion(leftOp, operator, rightOp, FieldQuery)
}
// ByLabel constructs a new criterion for label querying
func ByLabel(operator Operator, leftOp string, rightOp ...string) Criterion {
return newCriterion(leftOp, operator, rightOp, LabelQuery)
}
func newCriterion(leftOp string, operator Operator, rightOp []string, criteriaType CriterionType) Criterion {
return Criterion{LeftOp: leftOp, Operator: operator, RightOp: rightOp, Type: criteriaType}
}
func (c Criterion) Validate() error {
if len(c.RightOp) > 1 && !c.Operator.IsMultiVariate() {
return fmt.Errorf("multiple values %s received for single value operation %s", c.RightOp, c.Operator)
}
if c.Operator.IsNullable() && c.Type != FieldQuery {
return &util.UnsupportedQueryError{Message: "nullable operations are supported only for field queries"}
}
if c.Operator.IsNumeric() && !isNumeric(c.RightOp[0]) {
return &util.UnsupportedQueryError{Message: fmt.Sprintf("%s is numeric operator, but the right operand %s is not numeric", c.Operator, c.RightOp[0])}
}
if strings.ContainsRune(c.LeftOp, Separator) {
parts := strings.FieldsFunc(c.LeftOp, func(r rune) bool {
return r == Separator
})
possibleKey := parts[len(parts)-1]
return &util.UnsupportedQueryError{Message: fmt.Sprintf("separator %c is not allowed in %s with left operand \"%s\". Maybe you meant \"%s\"? Make sure if the separator is present in any right operand, that it is escaped with a backslash (\\)", Separator, c.Type, c.LeftOp, possibleKey)}
}
for _, op := range c.RightOp {
if strings.ContainsRune(op, '\n') {
return fmt.Errorf("%s with key \"%s\" has value \"%s\" contaning forbidden new line character", c.Type, c.LeftOp, op)
}
}
return nil
}
func mergeCriteria(c1 []Criterion, c2 []Criterion) ([]Criterion, error) {
result := c1
fieldQueryLeftOperands := make(map[string]int)
labelQueryLeftOperands := make(map[string]int)
for _, criterion := range append(c1, c2...) {
if criterion.Type == FieldQuery {
fieldQueryLeftOperands[criterion.LeftOp]++
}
if criterion.Type == LabelQuery {
labelQueryLeftOperands[criterion.LeftOp]++
}
}
for _, newCriterion := range c2 {
leftOp := newCriterion.LeftOp
// disallow duplicate label queries
if count, ok := labelQueryLeftOperands[leftOp]; ok && count > 1 && newCriterion.Type == LabelQuery {
return nil, &util.UnsupportedQueryError{Message: fmt.Sprintf("duplicate label query key: %s", newCriterion.LeftOp)}
}
// disallow duplicate field query keys
if count, ok := fieldQueryLeftOperands[leftOp]; ok && count > 1 && newCriterion.Type == FieldQuery {
return nil, &util.UnsupportedQueryError{Message: fmt.Sprintf("duplicate field query key: %s", newCriterion.LeftOp)}
}
if err := newCriterion.Validate(); err != nil {
return nil, err
}
}
result = append(result, c2...)
return result, nil
}
type criteriaCtxKey struct{}
// AddCriteria adds the given criteria to the context and returns an error if any of the criteria is not valid
func AddCriteria(ctx context.Context, newCriteria ...Criterion) (context.Context, error) {
currentCriteria := CriteriaForContext(ctx)
criteria, err := mergeCriteria(currentCriteria, newCriteria)
if err != nil {
return nil, err
}
return context.WithValue(ctx, criteriaCtxKey{}, criteria), nil
}
// CriteriaForContext returns the criteria for the given context
func CriteriaForContext(ctx context.Context) []Criterion {
currentCriteria := ctx.Value(criteriaCtxKey{})
if currentCriteria == nil {
return []Criterion{}
}
return currentCriteria.([]Criterion)
}
// ContextWithCriteria returns a new context with given criteria
func ContextWithCriteria(ctx context.Context, criteria []Criterion) context.Context {
return context.WithValue(ctx, criteriaCtxKey{}, criteria)
}
// BuildCriteriaFromRequest builds criteria for the given request's query params and returns an error if the query is not valid
func BuildCriteriaFromRequest(request *web.Request) ([]Criterion, error) {
var criteria []Criterion
for _, queryType := range supportedQueryTypes {
queryValues := request.URL.Query().Get(string(queryType))
querySegments, err := process(queryValues, queryType)
if err != nil {
return nil, err
}
if criteria, err = mergeCriteria(criteria, querySegments); err != nil {
return nil, err
}
}
sort.Sort(ByLeftOp(criteria))
return criteria, nil
}
type ByLeftOp []Criterion
func (c ByLeftOp) Len() int {
return len(c)
}
func (c ByLeftOp) Less(i, j int) bool {
return c[i].LeftOp < c[j].LeftOp
}
func (c ByLeftOp) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func process(input string, criteriaType CriterionType) ([]Criterion, error) {
var c []Criterion
if input == "" {
return c, nil
}
var leftOp string
var operator Operator
j := 0
for i := 0; i < len(input); i++ {
if leftOp != "" && operator != "" {
remaining := input[i+len(operator)+1:]
rightOp, offset, err := findRightOp(remaining, leftOp, operator, criteriaType)
if err != nil {
return nil, err
}
criterion := newCriterion(leftOp, operator, rightOp, criteriaType)
if err := criterion.Validate(); err != nil {
return nil, err
}
c = append(c, criterion)
i += offset + len(operator) + len(string(Separator))
j = i + 1
leftOp = ""
operator = ""
} else {
remaining := input[i:]
for _, op := range operators {
if strings.HasPrefix(remaining, fmt.Sprintf("%c%s%c", OperandSeparator, op, OperandSeparator)) {
leftOp = input[j:i]
operator = op
break
}
}
}
}
if len(c) == 0 {
return nil, fmt.Errorf("%s is not a valid %s", input, criteriaType)
}
return c, nil
}
func findRightOp(remaining string, leftOp string, operator Operator, criteriaType CriterionType) (rightOp []string, offset int, err error) {
rightOpBuffer := strings.Builder{}
for _, ch := range remaining {
if ch == Separator {
if offset+1 < len(remaining) && rune(remaining[offset+1]) == Separator && remaining[offset-1] != '\\' {
arg := rightOpBuffer.String()
rightOp = append(rightOp, arg)
rightOpBuffer.Reset()
} else if rune(remaining[offset-1]) == Separator {
offset++
continue
} else {
if remaining[offset-1] != '\\' { // delimiter is not escaped - treat as separator
arg := rightOpBuffer.String()
rightOp = append(rightOp, arg)
rightOpBuffer.Reset()
break
} else { // remove escaping symbol
tmp := rightOpBuffer.String()[:offset-1]
rightOpBuffer.Reset()
rightOpBuffer.WriteString(tmp)
rightOpBuffer.WriteRune(ch)
}
}
} else {
rightOpBuffer.WriteRune(ch)
}
offset++
}
if rightOpBuffer.Len() > 0 {
rightOp = append(rightOp, rightOpBuffer.String())
}
if len(rightOp) > 0 && operator.IsMultiVariate() {
firstElement := rightOp[0]
if strings.IndexRune(firstElement, OpenBracket) == 0 {
rightOp[0] = firstElement[1:]
} else {
return nil, -1, fmt.Errorf("operator %s for %s %s requires right operand to be surrounded in %c%c", operator, criteriaType, leftOp, OpenBracket, CloseBracket)
}
lastElement := rightOp[len(rightOp)-1]
if rune(lastElement[len(lastElement)-1]) == CloseBracket {
rightOp[len(rightOp)-1] = lastElement[:len(lastElement)-1]
} else {
return nil, -1, fmt.Errorf("operator %s for %s %s requires right operand to be surrounded in %c%c", operator, criteriaType, leftOp, OpenBracket, CloseBracket)
}
}
if len(rightOp) == 0 {
rightOp = append(rightOp, "")
}
return
}
func isNumeric(str string) bool {
_, err := strconv.Atoi(str)
if err == nil {
return true
}
_, err = strconv.ParseFloat(str, 64)
return err == nil
}