Skip to content

Conversation

@patrickfreed
Copy link
Contributor

@patrickfreed patrickfreed commented Jan 26, 2021

SWIFT-936

This PR updates the driver to use swift-bson, the new pure-Swift BSON implementation. It consists of mostly testing changes and should have no effect on the public API. I think the CRUD tests were synced due to a bug in them or something. I did it a while ago though so I'm not exactly sure.

* Throws an `MongoError.InternalError` if the bson_t isn't proper BSON.
*/
internal init(copying bsonPtr: BSONPointer) throws {
internal init(copying bsonPtr: BSONPointer) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This will be updated in a subsequent PR to reduce the amount of validation being done to match what we used to do via bson_copy, namely just verify that the encoded length matches the length of the data.

// Adds a custom "sortedEqual" predicate that compares two `Document`s and returns true if they
// have the same key/value pairs in them
public func sortedEqual(_ expectedValue: BSONDocument?) -> Predicate<BSONDocument> {
public func sortedEqual<T: SortedEquatable>(_ expectedValue: T?) -> Predicate<T> {
Copy link
Contributor Author

@patrickfreed patrickfreed Jan 26, 2021

Choose a reason for hiding this comment

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

In order to extend this matcher to BSON (which may contain arrays with documents or documents themselves), I introduced a protocol and made the matcher generic over it. This became more useful as more things became unordered due to the usage of ExtrasJSON instead of libbson.


public init(from decoder: Decoder) throws {
self.failPoint = try BSONDocument(from: decoder)
let container = try decoder.singleValueContainer()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required due to the ordering of the command getting messed up during decoding from JSON.


/// Return a new `RunCommand` with the command document ordered such that the provided command name
/// is the first key.
func withCommandName(_ name: String) -> RunCommand {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ditto required to reorder the command properly

@patrickfreed patrickfreed requested a review from kmahar January 26, 2021 22:31
@patrickfreed patrickfreed marked this pull request as ready for review January 26, 2021 22:31
}

extension BSONDocument: SortedEquatable {
public func sortedEquals(_ other: BSONDocument) -> Bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have an implementation of this in swift-bson now (called equalsIgnoreKeyOrder), but it currently has a bug where it doesn't compare documents contained in an array via equalsIgnoreKeyOrder. I filed SWIFT-1086 to cover fixing that. Once that's done we can remove this implementation of sortedEquals and just call through to equalsIgnoreKeyOrder.

Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

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

looks good and it's really exciting to finally be doing this 🎉 I just have some small questions/nits

Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

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

LGTM!

@patrickfreed patrickfreed merged commit ac623cb into mongodb:master Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants