Skip to content

Commit 4c9f380

Browse files
authored
SWIFT-1086 Ignore key order in array-nested documents in equalsIgnoreKeyOrder (#56)
1 parent 5389a76 commit 4c9f380

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

Sources/SwiftBSON/BSON.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,21 @@ extension BSON: Codable {
494494
try self.bsonValue.encode(to: encoder)
495495
}
496496
}
497+
498+
extension BSON {
499+
internal func equalsIgnoreKeyOrder(_ other: BSON) -> Bool {
500+
switch (self, other) {
501+
case let (.document(selfDoc), .document(otherDoc)):
502+
return selfDoc.equalsIgnoreKeyOrder(otherDoc)
503+
case let (.array(selfArr), .array(otherArr)):
504+
return selfArr.elementsEqual(otherArr) {
505+
$0.equalsIgnoreKeyOrder($1)
506+
}
507+
case let (.codeWithScope(selfCodeWithScope), .codeWithScope(otherCodeWithScope)):
508+
return selfCodeWithScope.code == otherCodeWithScope.code
509+
&& selfCodeWithScope.scope.equalsIgnoreKeyOrder(otherCodeWithScope.scope)
510+
default:
511+
return self == other
512+
}
513+
}
514+
}

Sources/SwiftBSON/BSONDocument.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -570,14 +570,7 @@ extension BSONDocument {
570570
}
571571

572572
for (k, v) in self {
573-
let otherValue = other[k]
574-
if case let (.document(docA), .document(docB)?) = (v, otherValue) {
575-
guard docA.equalsIgnoreKeyOrder(docB) else {
576-
return false
577-
}
578-
continue
579-
}
580-
guard v == otherValue else {
573+
guard let otherValue = other[k], v.equalsIgnoreKeyOrder(otherValue) else {
581574
return false
582575
}
583576
}

Tests/SwiftBSONTests/DocumentTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ final class DocumentTests: BSONTestCase {
268268
"array2": ["string1", "string2"],
269269
"null": .null,
270270
"code": .code(BSONCode(code: "console.log('hi');")),
271-
"nestedarray": [[1, 2], [.int32(3), .int32(4)]],
272-
"codewscope": .codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["x": 2]))
271+
"nestedarray": [[1, 2], [.int32(3), .int32(4)], ["x": 1, "y": 2]],
272+
"codewscope": .codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["y": 1, "x": 2]))
273273
]
274274

275275
let b: BSONDocument = [
@@ -283,8 +283,8 @@ final class DocumentTests: BSONTestCase {
283283
"minkey": .minKey,
284284
"date": .datetime(Date(timeIntervalSince1970: 500.004)),
285285
"timestamp": .timestamp(BSONTimestamp(timestamp: 5, inc: 10)),
286-
"nestedarray": [[1, 2], [.int32(3), .int32(4)]],
287-
"codewscope": .codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["x": 2])),
286+
"nestedarray": [[1, 2], [.int32(3), .int32(4)], ["y": 2, "x": 1]],
287+
"codewscope": .codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["x": 2, "y": 1])),
288288
"nesteddoc": ["b": 2, "a": 1, "d": [3, 4], "c": false],
289289
"oid": .objectID(try! BSONObjectID("507f1f77bcf86cd799439011")),
290290
"false": false,

0 commit comments

Comments
 (0)