/
matches.go
394 lines (349 loc) · 14 KB
/
matches.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
// Copyright (C) MongoDB, Inc. 2017-present.
//
// 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
package unified
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"strings"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)
// keyPathCtxKey is used as a key for a Context object. The value conveys the BSON key path that is currently being
// compared.
type keyPathCtxKey struct{}
// extraKeysAllowedCtxKey is used as a key for a Context object. The value conveys whether or not the document under
// test can contain extra keys. For example, if the expected document is {x: 1}, the document {x: 1, y: 1} would match
// if the value for this key is true.
type extraKeysAllowedCtxKey struct{}
func makeMatchContext(ctx context.Context, keyPath string, extraKeysAllowed bool) context.Context {
ctx = context.WithValue(ctx, keyPathCtxKey{}, keyPath)
return context.WithValue(ctx, extraKeysAllowedCtxKey{}, extraKeysAllowed)
}
// verifyValuesMatch compares the provided BSON values and returns an error if they do not match. If the values are
// documents and extraKeysAllowed is true, the actual value will be allowed to have additional keys at the top-level.
// For example, an expected document {x: 1} would match the actual document {x: 1, y: 1}.
func verifyValuesMatch(ctx context.Context, expected, actual bson.RawValue, extraKeysAllowed bool) error {
return verifyValuesMatchInner(makeMatchContext(ctx, "", extraKeysAllowed), expected, actual)
}
func verifyValuesMatchInner(ctx context.Context, expected, actual bson.RawValue) error {
keyPath := ctx.Value(keyPathCtxKey{}).(string)
extraKeysAllowed := ctx.Value(extraKeysAllowedCtxKey{}).(bool)
if expectedDoc, ok := expected.DocumentOK(); ok {
// If the root document only has one element and the key is a special matching operator, the actual value might
// not actually be a document. In this case, evaluate the special operator with the actual value rather than
// doing an element-wise document comparison.
if requiresSpecialMatching(expectedDoc) {
if err := evaluateSpecialComparison(ctx, expectedDoc, actual); err != nil {
return newMatchingError(keyPath, "error doing special matching assertion: %v", err)
}
return nil
}
actualDoc, ok := actual.DocumentOK()
if !ok {
return newMatchingError(keyPath, "expected value to be a document but got a %s", actual.Type)
}
// Perform element-wise comparisons.
expectedElems, _ := expectedDoc.Elements()
for _, expectedElem := range expectedElems {
expectedKey := expectedElem.Key()
expectedValue := expectedElem.Value()
fullKeyPath := expectedKey
if keyPath != "" {
fullKeyPath = keyPath + "." + expectedKey
}
// Get the value from actualDoc here but don't check the error until later because some of the special
// matching operators can assert that the value isn't present in the document (e.g. $$exists).
actualValue, err := actualDoc.LookupErr(expectedKey)
if specialDoc, ok := expectedValue.DocumentOK(); ok && requiresSpecialMatching(specialDoc) {
// Reset the key path so any errors returned from the function will only have the key path for the
// target value. Also unconditionally set extraKeysAllowed to false because an assertion like
// $$unsetOrMatches could recurse back into this function. In that case, the target document is nested
// and should not have extra keys.
ctx = makeMatchContext(ctx, "", false)
if err := evaluateSpecialComparison(ctx, specialDoc, actualValue); err != nil {
return newMatchingError(fullKeyPath, "error doing special matching assertion: %v", err)
}
continue
}
// This isn't a special comparison. Assert that the value exists in the actual document.
if err != nil {
return newMatchingError(fullKeyPath, "key not found in actual document")
}
// Check to see if the keypath requires us to convert actual/expected to make a true comparison. If the
// comparison is not supported for the keypath, continue with the recursive strategy.
//
// TODO(GODRIVER-2386): this branch of logic will be removed once we add document support for comments
mixedTypeEvaluated, err := evaluateMixedTypeComparison(expectedKey, expectedValue, actualValue)
if err != nil {
return newMatchingError(fullKeyPath, "error doing mixed-type matching assertion: %v", err)
}
if mixedTypeEvaluated {
continue
}
// Nested documents cannot have extra keys, so we unconditionally pass false for extraKeysAllowed.
comparisonCtx := makeMatchContext(ctx, fullKeyPath, false)
if err := verifyValuesMatchInner(comparisonCtx, expectedValue, actualValue); err != nil {
return err
}
}
// If required, verify that the actual document does not have extra elements. We do this by iterating over the
// actual and checking for each key in the expected rather than comparing element counts because the presence of
// special operators can cause incorrect counts. For example, the document {y: {$$exists: false}} has one
// element, but should match the document {}, which has none.
if !extraKeysAllowed {
actualElems, _ := actualDoc.Elements()
for _, actualElem := range actualElems {
if _, err := expectedDoc.LookupErr(actualElem.Key()); err != nil {
return newMatchingError(keyPath, "extra key %q found in actual document %s", actualElem.Key(),
actualDoc)
}
}
}
return nil
}
if expectedArr, ok := expected.ArrayOK(); ok {
actualArr, ok := actual.ArrayOK()
if !ok {
return newMatchingError(keyPath, "expected value to be an array but got a %s", actual.Type)
}
expectedValues, _ := expectedArr.Values()
actualValues, _ := actualArr.Values()
// Arrays must always have the same number of elements.
if len(expectedValues) != len(actualValues) {
return newMatchingError(keyPath, "expected array length %d, got %d", len(expectedValues),
len(actualValues))
}
for idx, expectedValue := range expectedValues {
// Use the index as the key to augment the key path.
fullKeyPath := fmt.Sprintf("%d", idx)
if keyPath != "" {
fullKeyPath = keyPath + "." + fullKeyPath
}
comparisonCtx := makeMatchContext(ctx, fullKeyPath, extraKeysAllowed)
err := verifyValuesMatchInner(comparisonCtx, expectedValue, actualValues[idx])
if err != nil {
return err
}
}
return nil
}
// Numeric values must be considered equal even if their types are different (e.g. if expected is an int32 and
// actual is an int64).
if expected.IsNumber() {
if !actual.IsNumber() {
return newMatchingError(keyPath, "expected value to be a number but got a %s", actual.Type)
}
expectedInt64 := expected.AsInt64()
actualInt64 := actual.AsInt64()
if expectedInt64 != actualInt64 {
return newMatchingError(keyPath, "expected numeric value %d, got %d", expectedInt64, actualInt64)
}
return nil
}
// If expected is not a recursive or numeric type, we can directly call Equal to do the comparison.
if !expected.Equal(actual) {
return newMatchingError(keyPath, "expected value %s, got %s", expected, actual)
}
return nil
}
// compareDocumentToString will compare an expected document to an actual string by converting the document into a
// string.
func compareDocumentToString(expected, actual bson.RawValue) error {
expectedDocument, ok := expected.DocumentOK()
if !ok {
return fmt.Errorf("expected value to be a document but got a %s", expected.Type)
}
actualString, ok := actual.StringValueOK()
if !ok {
return fmt.Errorf("expected value to be a string but got a %s", actual.Type)
}
if actualString != expectedDocument.String() {
return fmt.Errorf("expected value %s, got %s", expectedDocument.String(), actualString)
}
return nil
}
// evaluateMixedTypeComparison compares an expected document with an actual string. If this comparison occurs, then
// the function will return `true` along with any resulting error.
func evaluateMixedTypeComparison(expectedKey string, expected, actual bson.RawValue) (bool, error) {
switch expectedKey {
case "comment":
if expected.Type == bsontype.EmbeddedDocument && actual.Type == bsontype.String {
return true, compareDocumentToString(expected, actual)
}
}
return false, nil
}
func evaluateSpecialComparison(ctx context.Context, assertionDoc bson.Raw, actual bson.RawValue) error {
assertionElem := assertionDoc.Index(0)
assertion := assertionElem.Key()
assertionVal := assertionElem.Value()
switch assertion {
case "$$exists":
shouldExist := assertionVal.Boolean()
exists := actual.Validate() == nil
if shouldExist != exists {
return fmt.Errorf("expected value to exist: %v; value actually exists: %v", shouldExist, exists)
}
case "$$type":
possibleTypes, err := getTypesArray(assertionVal)
if err != nil {
return fmt.Errorf("error getting possible types for a $$type assertion: %v", err)
}
for _, possibleType := range possibleTypes {
if actual.Type == possibleType {
return nil
}
}
return fmt.Errorf("expected type to be one of %v but was %s", possibleTypes, actual.Type)
case "$$matchesEntity":
expected, err := entities(ctx).BSONValue(assertionVal.StringValue())
if err != nil {
return err
}
// $$matchesEntity doesn't modify the nesting level of the key path so we can propagate ctx without changes.
return verifyValuesMatchInner(ctx, expected, actual)
case "$$matchesHexBytes":
expectedBytes, err := hex.DecodeString(assertionVal.StringValue())
if err != nil {
return fmt.Errorf("error converting $$matcesHexBytes value to bytes: %v", err)
}
_, actualBytes, ok := actual.BinaryOK()
if !ok {
return fmt.Errorf("expected binary value for a $$matchesHexBytes assertion, but got a %s", actual.Type)
}
if !bytes.Equal(expectedBytes, actualBytes) {
return fmt.Errorf("expected bytes %v, got %v", expectedBytes, actualBytes)
}
case "$$unsetOrMatches":
if actual.Validate() != nil {
return nil
}
// $$unsetOrMatches doesn't modify the nesting level or the key path so we can propagate the context to the
// comparison function without changing anything.
return verifyValuesMatchInner(ctx, assertionVal, actual)
case "$$sessionLsid":
sess, err := entities(ctx).session(assertionVal.StringValue())
if err != nil {
return err
}
expectedID := sess.ID()
actualID, ok := actual.DocumentOK()
if !ok {
return fmt.Errorf("expected document value for a $$sessionLsid assertion, but got a %s", actual.Type)
}
if !bytes.Equal(expectedID, actualID) {
return fmt.Errorf("expected lsid %v, got %v", expectedID, actualID)
}
case "$$lte":
if assertionVal.Type != bsontype.Int32 && assertionVal.Type != bsontype.Int64 {
return fmt.Errorf("expected assertionVal to be an Int32 or Int64 but got a %s", assertionVal.Type)
}
if actual.Type != bsontype.Int32 && actual.Type != bsontype.Int64 {
return fmt.Errorf("expected value to be an Int32 or Int64 but got a %s", actual.Type)
}
// Numeric values can be compared even if their types are different (e.g. if expected is an int32 and actual
// is an int64).
expectedInt64 := assertionVal.AsInt64()
actualInt64 := actual.AsInt64()
if actualInt64 > expectedInt64 {
return fmt.Errorf("expected numeric value %d to be less than or equal %d", actualInt64, expectedInt64)
}
return nil
default:
return fmt.Errorf("unrecognized special matching assertion %q", assertion)
}
return nil
}
func requiresSpecialMatching(doc bson.Raw) bool {
elems, _ := doc.Elements()
return len(elems) == 1 && strings.HasPrefix(elems[0].Key(), "$$")
}
func getTypesArray(val bson.RawValue) ([]bsontype.Type, error) {
switch val.Type {
case bsontype.String:
convertedType, err := convertStringToBSONType(val.StringValue())
if err != nil {
return nil, err
}
return []bsontype.Type{convertedType}, nil
case bsontype.Array:
var typeStrings []string
if err := val.Unmarshal(&typeStrings); err != nil {
return nil, fmt.Errorf("error unmarshalling to slice of strings: %v", err)
}
var types []bsontype.Type
for _, typeStr := range typeStrings {
convertedType, err := convertStringToBSONType(typeStr)
if err != nil {
return nil, err
}
types = append(types, convertedType)
}
return types, nil
default:
return nil, fmt.Errorf("invalid type to convert to bsontype.Type slice: %s", val.Type)
}
}
func convertStringToBSONType(typeStr string) (bsontype.Type, error) {
switch typeStr {
case "double":
return bsontype.Double, nil
case "string":
return bsontype.String, nil
case "object":
return bsontype.EmbeddedDocument, nil
case "array":
return bsontype.Array, nil
case "binData":
return bsontype.Binary, nil
case "undefined":
return bsontype.Undefined, nil
case "objectId":
return bsontype.ObjectID, nil
case "bool":
return bsontype.Boolean, nil
case "date":
return bsontype.DateTime, nil
case "null":
return bsontype.Null, nil
case "regex":
return bsontype.Regex, nil
case "dbPointer":
return bsontype.DBPointer, nil
case "javascript":
return bsontype.JavaScript, nil
case "symbol":
return bsontype.Symbol, nil
case "javascriptWithScope":
return bsontype.CodeWithScope, nil
case "int":
return bsontype.Int32, nil
case "timestamp":
return bsontype.Timestamp, nil
case "long":
return bsontype.Int64, nil
case "decimal":
return bsontype.Decimal128, nil
case "minKey":
return bsontype.MinKey, nil
case "maxKey":
return bsontype.MaxKey, nil
default:
return bsontype.Type(0), fmt.Errorf("unrecognized BSON type string %q", typeStr)
}
}
// newMatchingError creates an error to convey that BSON value comparison failed at the provided key path. If the
// key path is empty (e.g. because the values being compared were not documents), the error message will contain the
// phrase "top-level" instead of the path.
func newMatchingError(keyPath, msg string, args ...interface{}) error {
fullMsg := fmt.Sprintf(msg, args...)
if keyPath == "" {
return fmt.Errorf("comparison error at top-level: %s", fullMsg)
}
return fmt.Errorf("comparison error at key %q: %s", keyPath, fullMsg)
}