Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Sources/SwiftBSON/BSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,21 @@ extension BSON: Codable {
try self.bsonValue.encode(to: encoder)
}
}

extension BSON {
internal func equalsIgnoreKeyOrder(_ other: BSON) -> Bool {
switch (self, other) {
case let (.document(selfDoc), .document(otherDoc)):
return selfDoc.equalsIgnoreKeyOrder(otherDoc)
case let (.array(selfArr), .array(otherArr)):
return selfArr.elementsEqual(otherArr) {
$0.equalsIgnoreKeyOrder($1)
}
case let (.codeWithScope(selfCodeWithScope), .codeWithScope(otherCodeWithScope)):
return selfCodeWithScope.code == otherCodeWithScope.code
&& selfCodeWithScope.scope.equalsIgnoreKeyOrder(otherCodeWithScope.scope)
default:
return self == other
}
}
}
9 changes: 1 addition & 8 deletions Sources/SwiftBSON/BSONDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -570,14 +570,7 @@ extension BSONDocument {
}

for (k, v) in self {
let otherValue = other[k]
if case let (.document(docA), .document(docB)?) = (v, otherValue) {
guard docA.equalsIgnoreKeyOrder(docB) else {
return false
}
continue
}
guard v == otherValue else {
guard let otherValue = other[k], v.equalsIgnoreKeyOrder(otherValue) else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that I think about it, shouldn't this method just not exist because a public BSONDocument.equalsIgnoreKeyOrder is already defined in the BSON library?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to BSON.equalsIgnoreKeyOrder? That's necessary (or rather helpful) to ease in recursion, since we have to go through BSON for both arrays and documents. If you mean BSONDocument.sortedEquals in the driver, then yeah that can be replaced with this. I originally tried doing that during the perf work but ran into this bug, so I had to implement it directly over there first. Once this is merged I'll open a follow-up PR updating the driver's implementation to just wrap this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol whoops, I don't know why but I was thinking this was a PR against the driver itself 🤦‍♀️ nvm me

return false
}
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/SwiftBSONTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ final class DocumentTests: BSONTestCase {
"array2": ["string1", "string2"],
"null": .null,
"code": .code(BSONCode(code: "console.log('hi');")),
"nestedarray": [[1, 2], [.int32(3), .int32(4)]],
"codewscope": .codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["x": 2]))
"nestedarray": [[1, 2], [.int32(3), .int32(4)], ["x": 1, "y": 2]],
"codewscope": .codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["y": 1, "x": 2]))
]

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