-
Notifications
You must be signed in to change notification settings - Fork 67
SWIFT-178 Conform various options/result types to Decodable #269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
patrickfreed
merged 2 commits into
master
from
SWIFT-178/retryable-writes-decodable-options
May 21, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,7 +41,7 @@ extension MongoCollection { | |
| } | ||
|
|
||
| /// A model for a `deleteOne` operation within a bulk write. | ||
| public struct DeleteOneModel: WriteModel { | ||
| public struct DeleteOneModel: WriteModel, Decodable { | ||
| /// A `Document` representing the match criteria. | ||
| public let filter: Document | ||
|
|
||
|
|
@@ -78,7 +78,7 @@ extension MongoCollection { | |
| } | ||
|
|
||
| /// A model for a `deleteMany` operation within a bulk write. | ||
| public struct DeleteManyModel: WriteModel { | ||
| public struct DeleteManyModel: WriteModel, Decodable { | ||
| /// A `Document` representing the match criteria. | ||
| public let filter: Document | ||
|
|
||
|
|
@@ -115,7 +115,7 @@ extension MongoCollection { | |
| } | ||
|
|
||
| /// A model for an `insertOne` operation within a bulk write. | ||
| public struct InsertOneModel: WriteModel { | ||
| public struct InsertOneModel: WriteModel, Decodable { | ||
| /// The `CollectionType` to insert. | ||
| public let document: CollectionType | ||
|
|
||
|
|
@@ -158,7 +158,7 @@ extension MongoCollection { | |
| } | ||
|
|
||
| /// A model for a `replaceOne` operation within a bulk write. | ||
| public struct ReplaceOneModel: WriteModel { | ||
| public struct ReplaceOneModel: WriteModel, Decodable { | ||
| /// A `Document` representing the match criteria. | ||
| public let filter: Document | ||
|
|
||
|
|
@@ -216,7 +216,7 @@ extension MongoCollection { | |
| } | ||
|
|
||
| /// A model for an `updateOne` operation within a bulk write. | ||
| public struct UpdateOneModel: WriteModel { | ||
| public struct UpdateOneModel: WriteModel, Decodable { | ||
| /// A `Document` representing the match criteria. | ||
| public let filter: Document | ||
|
|
||
|
|
@@ -278,7 +278,7 @@ extension MongoCollection { | |
| } | ||
|
|
||
| /// A model for an `updateMany` operation within a bulk write. | ||
| public struct UpdateManyModel: WriteModel { | ||
| public struct UpdateManyModel: WriteModel, Decodable { | ||
| /// A `Document` representing the match criteria. | ||
| public let filter: Document | ||
|
|
||
|
|
@@ -418,7 +418,7 @@ public class BulkWriteOperation: Operation { | |
| } | ||
|
|
||
| /// Options to use when performing a bulk write operation on a `MongoCollection`. | ||
| public struct BulkWriteOptions: Encodable { | ||
| public struct BulkWriteOptions: Codable { | ||
| /// If `true`, allows the write to opt-out of document level validation. | ||
| public let bypassDocumentValidation: Bool? | ||
|
|
||
|
|
@@ -454,7 +454,7 @@ public struct BulkWriteOptions: Encodable { | |
| } | ||
|
|
||
| /// The result of a bulk write operation on a `MongoCollection`. | ||
| public struct BulkWriteResult { | ||
| public struct BulkWriteResult: Decodable { | ||
| /// Number of documents deleted. | ||
| public let deletedCount: Int | ||
|
|
||
|
|
@@ -476,6 +476,37 @@ public struct BulkWriteResult { | |
| /// Map of the index of the operation to the id of the upserted document. | ||
| public let upsertedIds: [Int: BSONValue] | ||
|
|
||
| private enum CodingKeys: CodingKey { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it always annoys me that the synthesized code is all or none and you can't use the synthesized coding keys if you use a custom |
||
| case deletedCount, insertedCount, insertedIds, matchedCount, modifiedCount, upsertedCount, upsertedIds | ||
| } | ||
|
|
||
| public init(from decoder: Decoder) throws { | ||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
|
||
| // None of the results must be present themselves, but at least one must. | ||
| guard !container.allKeys.isEmpty else { | ||
| throw DecodingError.valueNotFound(BulkWriteResult.self, | ||
| DecodingError.Context(codingPath: decoder.codingPath, | ||
| debugDescription: "No results found")) | ||
| } | ||
|
|
||
| self.deletedCount = try container.decodeIfPresent(Int.self, forKey: .deletedCount) ?? 0 | ||
| self.matchedCount = try container.decodeIfPresent(Int.self, forKey: .matchedCount) ?? 0 | ||
| self.modifiedCount = try container.decodeIfPresent(Int.self, forKey: .modifiedCount) ?? 0 | ||
|
|
||
| let insertedIds = | ||
| (try container.decodeIfPresent([Int: AnyBSONValue].self, forKey: .insertedIds) ?? [:]) | ||
| .mapValues { $0.value } | ||
| self.insertedIds = insertedIds | ||
| self.insertedCount = try container.decodeIfPresent(Int.self, forKey: .insertedCount) ?? insertedIds.count | ||
|
|
||
| let upsertedIds = | ||
| (try container.decodeIfPresent([Int: AnyBSONValue].self, forKey: .upsertedIds) ?? [:]) | ||
| .mapValues { $0.value } | ||
| self.upsertedIds = upsertedIds | ||
| self.upsertedCount = try container.decodeIfPresent(Int.self, forKey: .upsertedCount) ?? upsertedIds.count | ||
| } | ||
|
|
||
| /** | ||
| * Create a `BulkWriteResult` from a reply and map of inserted IDs. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.