/
filter.go
411 lines (375 loc) · 11.2 KB
/
filter.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package mongo
import (
"fmt"
"github.com/imulab/go-scim/core/errors"
"github.com/imulab/go-scim/core/expr"
"github.com/imulab/go-scim/core/prop"
"github.com/imulab/go-scim/core/spec"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"strconv"
"strings"
"time"
)
// Contrary to the main theme in this package, the methods in this file transforms SCIM filter to an
// intermediate format, which will again be transformed by the MongoDB driver to BSON. This decision
// was made based on two facts: first, filter is relatively small, hence the performance impact is negligible;
// second, operations involving filters are performed less frequent, hence, even if there is a performance
// penalty for double transformation and reflection, the overall throughput will not be affected as much.
//
// By transforming the query to bson.M (the map based intermediate format), we gain additional readability which
// is less achievable when directly appending BSON bytes to a buffer.
// Compile and transform a SCIM filter string to a bsonx.Val that contains the original
// filter in MongoDB compatible format.
func TransformFilter(scimFilter string, resourceType *spec.ResourceType) (bson.D, error) {
root, err := expr.CompileFilter(scimFilter)
if err != nil {
return nil, err
}
return TransformCompiledFilter(root, resourceType)
}
// Compile and transform a compiled SCIM filter to bsonx.Val that contains the original
// filter in MongoDB compatible format. This slight optimization allow the caller to pre-compile
// frequently used queries and save the trip to the filter parser and compiler.
func TransformCompiledFilter(root *expr.Expression, resourceType *spec.ResourceType) (bson.D, error) {
return newTransformer(resourceType).transform(root)
}
func newTransformer(resourceType *spec.ResourceType) *transformer {
return &transformer{
superAttr: resourceType.SuperAttribute(true),
}
}
type transformer struct {
superAttr *spec.Attribute
}
// Transform the filter which is represented by the root to bsonx.Val.
func (t *transformer) transform(root *expr.Expression) (bson.D, error) {
switch root.Token() {
case expr.And:
return t.transformAnd(root)
case expr.Or:
return t.transformOr(root)
case expr.Not:
return t.transformNot(root)
default:
return t.transformRelational(t.superAttr, root.Left(), root, root.Right())
}
}
func (t *transformer) transformAnd(root *expr.Expression) (bson.D, error) {
left, err := t.transform(root.Left())
if err != nil {
return nil, err
}
right, err := t.transform(root.Right())
if err != nil {
return nil, err
}
return bson.D{
{Key: mongoAnd, Value: bson.A{left, right}},
}, nil
}
func (t *transformer) transformOr(root *expr.Expression) (bson.D, error) {
left, err := t.transform(root.Left())
if err != nil {
return nil, err
}
right, err := t.transform(root.Right())
if err != nil {
return nil, err
}
return bson.D{
{Key: mongoOr, Value: bson.A{left, right}},
}, nil
}
func (t *transformer) transformNot(root *expr.Expression) (bson.D, error) {
left, err := t.transform(root.Left())
if err != nil {
return nil, err
}
return bson.D{
{Key: mongoNot, Value: bson.A{left}},
}, nil
}
func (t *transformer) transformRelational(containerAttr *spec.Attribute, path *expr.Expression, op *expr.Expression, value *expr.Expression) (bson.D, error) {
var (
cursorAttr = containerAttr
pathNames = make([]string, 0)
)
{
for path != nil && cursorAttr.SingleValued() {
cursorAttr = cursorAttr.SubAttributeForName(path.Token())
if cursorAttr == nil {
return nil, errors.InvalidFilter("no such path in filter")
}
pathName := cursorAttr.Name()
if md, ok := metadataHub[cursorAttr.ID()]; ok {
pathName = md.MongoName
}
pathNames = append(pathNames, pathName)
path = path.Next()
}
}
var nextDoc interface{}
{
var err error
if path == nil {
nextDoc, err = t.transformValue(cursorAttr, op, value)
} else {
nextDoc, err = t.transformRelational(cursorAttr.NewElementAttribute(), path, op, value)
}
if err != nil {
return nil, err
}
}
if cursorAttr.MultiValued() && (path != nil || op.Token() != expr.Pr) {
// If we have stopped on a multiValued attribute, we do an $elementMatch when
// 1. there's more to the path
// i.e. emails.value
// 2. the operator is not a pr
// i.e. schemas eq "foobar"
return bson.D{
{Key: strings.Join(pathNames, "."), Value: bson.D{
{Key: mongoElementMatch, Value: nextDoc},
}},
}, nil
} else {
q := bson.D{{Key: strings.Join(pathNames, "."), Value: nextDoc}}
if op.Token() != expr.Pr {
return q, nil
}
return t.rearrangeForPr(q), nil
}
}
// rearrange query in the form of "{ <field> : { $and : [ <criteria1>, <criteria2> , ... , <criteriaN>] }}", into
// { $and : [ { <field> : <criteria1> }, { <field> : <criteria2> }, ..., { <field> : <criteriaN> } ] }
func (t *transformer) rearrangeForPr(doc bson.D) bson.D {
if len(doc) != 1 {
return doc
}
field := doc[0].Key
if len(field) == 0 {
return doc
}
if _, ok := doc[0].Value.(bson.D); !ok {
return doc
} else if len(doc[0].Value.(bson.D)) != 1 {
return doc
} else if doc[0].Value.(bson.D)[0].Key != mongoAnd {
return doc
} else if _, ok := doc[0].Value.(bson.D)[0].Value.(bson.A); !ok {
return doc
}
criterion := doc[0].Value.(bson.D)[0].Value.(bson.A)
newCriterion := bson.A{}
for _, c := range criterion {
newCriterion = append(newCriterion, bson.D{{Key: field, Value: c}})
}
return bson.D{{Key: mongoAnd, Value: newCriterion}}
}
func (t *transformer) eqValue(attr *spec.Attribute, value *expr.Expression) interface{} {
if attr.Type() != spec.TypeString && attr.CaseExact() {
return bson.D{
{Key: mongoEq, Value: unquote(value.Token())},
}
} else {
return primitive.Regex{
Pattern: fmt.Sprintf("^%s$", unquote(value.Token())),
Options: "i",
}
}
}
func (t *transformer) neValue(attr *spec.Attribute, value *expr.Expression) interface{} {
if attr.Type() != spec.TypeString || attr.CaseExact() {
return bson.D{
{Key: mongoNe, Value: unquote(value.Token())},
}
} else {
return primitive.Regex{
Pattern: fmt.Sprintf("^((?!%s$).)", unquote(value.Token())),
Options: "i",
}
}
}
func (t *transformer) swValue(attr *spec.Attribute, value *expr.Expression) primitive.Regex {
if attr.CaseExact() {
return primitive.Regex{
Pattern: fmt.Sprintf("^%s", unquote(value.Token())),
}
} else {
return primitive.Regex{
Pattern: fmt.Sprintf("^%s", unquote(value.Token())),
Options: "i",
}
}
}
func (t *transformer) ewValue(attr *spec.Attribute, value *expr.Expression) primitive.Regex {
if attr.CaseExact() {
return primitive.Regex{
Pattern: fmt.Sprintf("%s$", unquote(value.Token())),
}
} else {
return primitive.Regex{
Pattern: fmt.Sprintf("%s$", unquote(value.Token())),
Options: "i",
}
}
}
func (t *transformer) coValue(attr *spec.Attribute, value *expr.Expression) primitive.Regex {
if attr.CaseExact() {
return primitive.Regex{
Pattern: unquote(value.Token()),
}
} else {
return primitive.Regex{
Pattern: "unquote(value.Token())",
Options: "i",
}
}
}
func (t *transformer) gtValue(attr *spec.Attribute, value *expr.Expression) (bson.D, error) {
v, err := t.parseValue(value.Token(), attr)
if err != nil {
return nil, err
}
return bson.D{
{Key: mongoGt, Value: v},
}, nil
}
func (t *transformer) geValue(attr *spec.Attribute, value *expr.Expression) (bson.D, error) {
v, err := t.parseValue(value.Token(), attr)
if err != nil {
return nil, err
}
return bson.D{
{Key: mongoGe, Value: v},
}, nil
}
func (t *transformer) ltValue(attr *spec.Attribute, value *expr.Expression) (bson.D, error) {
v, err := t.parseValue(value.Token(), attr)
if err != nil {
return nil, err
}
return bson.D{
{Key: mongoLt, Value: v},
}, nil
}
func (t *transformer) leValue(attr *spec.Attribute, value *expr.Expression) (bson.D, error) {
v, err := t.parseValue(value.Token(), attr)
if err != nil {
return nil, err
}
return bson.D{
{Key: mongoLe, Value: v},
}, nil
}
func (t *transformer) prDoc(attr *spec.Attribute) bson.D {
criterion := bson.A{}
criterion = append(criterion, existsCriteria, nullCriteria)
if attr.MultiValued() {
criterion = append(criterion, emptyArrayCriteria)
} else {
switch attr.Type() {
case spec.TypeString, spec.TypeReference, spec.TypeBinary:
criterion = append(criterion, emptyStringCriteria)
case spec.TypeComplex:
criterion = append(criterion, emptyObjectCriteria)
}
}
return bson.D{{Key: mongoAnd, Value: criterion}}
}
func (t *transformer) transformValue(attr *spec.Attribute, op *expr.Expression, value *expr.Expression) (interface{}, error) {
switch op.Token() {
case expr.Eq:
return t.eqValue(attr, value), nil
case expr.Ne:
return t.neValue(attr, value), nil
case expr.Sw:
return t.swValue(attr, value), nil
case expr.Ew:
return t.ewValue(attr, value), nil
case expr.Co:
return t.coValue(attr, value), nil
case expr.Gt:
return t.gtValue(attr, value)
case expr.Ge:
return t.geValue(attr, value)
case expr.Lt:
return t.ltValue(attr, value)
case expr.Le:
return t.leValue(attr, value)
case expr.Pr:
return t.prDoc(attr), nil
default:
panic("invalid relational operator")
}
}
// Parse the given raw value to the appropriate data type according to the type information in attribute.
// The attribute will be treated as singleValued even if it is multiValued.
func (t transformer) parseValue(raw string, attr *spec.Attribute) (interface{}, error) {
if attr.Type() == spec.TypeComplex {
return nil, errors.InvalidFilter("incompatible complex property")
}
switch attr.Type() {
case spec.TypeString, spec.TypeReference, spec.TypeBinary:
return unquote(raw), nil
case spec.TypeDateTime:
t, err := time.Parse(prop.ISO8601, unquote(raw))
if err != nil {
return nil, errors.InvalidFilter("invalid value: expects '%s' to be a dateTime", raw)
}
return primitive.NewDateTimeFromTime(t), nil
case spec.TypeBoolean:
b, err := strconv.ParseBool(raw)
if err != nil {
return nil, errors.InvalidFilter("invalid value: expects '%s' to be a boolean", raw)
}
return b, nil
case spec.TypeInteger:
i, err := strconv.ParseInt(raw, 10, 64)
if err != nil {
return nil, errors.InvalidFilter("invalid value: expects '%s' to be an integer", raw)
}
return i, nil
case spec.TypeDecimal:
f, err := strconv.ParseFloat(raw, 64)
if err != nil {
return nil, errors.InvalidFilter("invalid value: expects '%s' to be a decimal", raw)
}
return f, nil
default:
panic("impossible type")
}
}
func unquote(raw string) string {
uq, err := strconv.Unquote(raw)
if err != nil {
return raw
}
return uq
}
// pre compiled criteria
var (
existsCriteria = bson.D{{Key: mongoExists, Value: true}}
nullCriteria = bson.D{{Key: mongoNe, Value: primitive.Null{}}}
emptyStringCriteria = bson.D{{Key: mongoNe, Value: ""}}
emptyObjectCriteria = bson.D{{Key: mongoNe, Value: bson.M{}}}
emptyArrayCriteria = bson.D{
{Key: mongoNot, Value: bson.A{
bson.D{{Key: mongoSize, Value: 0}},
}},
}
)
const (
mongoAnd = "$and"
mongoOr = "$or"
mongoNot = "$nor"
mongoElementMatch = "$elemMatch"
mongoEq = "$eq"
mongoNe = "$ne"
mongoGt = "$gt"
mongoGe = "$gte"
mongoLt = "$lt"
mongoLe = "$lte"
mongoExists = "$exists"
mongoSize = "$size"
)