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
29 changes: 18 additions & 11 deletions Sources/MongoSwift/WriteConcern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@ public struct WriteConcern: Codable {
public static let serverDefault = WriteConcern()

/// An option to request acknowledgement that the write operation has propagated to specified mongod instances.
/// - SeeAlso: https://docs.mongodb.com/manual/reference/write-concern/#w-option
public enum W: Codable, Equatable {
/// Specifies the number of nodes that should acknowledge the write. MUST be greater than or equal to 0.
/// Requests acknowledgment that the write operation has propagated to the specified number of mongod
/// instances. MUST be greater than or equal to 0.
case number(Int)
/// Indicates a tag for nodes that should acknowledge the write.
case tag(String)
/// Specifies that a majority of nodes should acknowledge the write.
/// Requests acknowledgment that write operations have propagated to the majority of the data-bearing voting
/// members.
case majority
// swiftlint:disable line_length
/// Requests acknowledgement that the write operation has propagated to tagged members that satisfy the custom
/// write concern with the specified name.
/// - SeeAlso: https://docs.mongodb.com/manual/reference/write-concern/#writeconcern.%3Ccustom-write-concern-name%3E
case custom(String)
// swiftlint:enable line_length

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let string = try? container.decode(String.self) {
self = string == "majority" ? .majority : .tag(string)
self = string == "majority" ? .majority : .custom(string)
} else {
let wNumber = try container.decode(Int.self)
self = .number(wNumber)
Expand All @@ -57,8 +64,8 @@ public struct WriteConcern: Codable {
message: "Invalid WriteConcern.w \(wNumber): must be between 0 and \(Int32.max)"
)
}
case let .tag(wTag):
try container.encode(wTag)
case let .custom(name):
try container.encode(name)
case .majority:
try container.encode("majority")
}
Expand Down Expand Up @@ -152,8 +159,8 @@ public struct WriteConcern: Codable {
case MONGOC_WRITE_CONCERN_W_MAJORITY:
self.w = .majority
case MONGOC_WRITE_CONCERN_W_TAG:
if let wTag = mongoc_write_concern_get_wtag(writeConcern) {
self.w = .tag(String(cString: wTag))
if let name = mongoc_write_concern_get_wtag(writeConcern) {
self.w = .custom(String(cString: name))
} else {
self.w = nil
}
Expand Down Expand Up @@ -187,8 +194,8 @@ public struct WriteConcern: Codable {
switch w {
case let .number(wNumber):
mongoc_write_concern_set_w(writeConcern, Int32(wNumber))
case let .tag(wTag):
mongoc_write_concern_set_wtag(writeConcern, wTag)
case let .custom(name):
mongoc_write_concern_set_wtag(writeConcern, name)
case .majority:
mongoc_write_concern_set_w(writeConcern, MONGOC_WRITE_CONCERN_W_MAJORITY)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/MongoSwiftTests/ReadWriteConcernSpecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ extension WriteConcern {
let j = doc["journal"]?.boolValue

var w: W?
if let wtag = doc["w"]?.stringValue {
w = wtag == "majority" ? .majority : .tag(wtag)
if let str = doc["w"]?.stringValue {
w = str == "majority" ? .majority : .custom(str)
} else if let wInt = doc["w"]?.toInt() {
w = .number(wInt)
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/MongoSwiftTests/WriteConcernTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class WriteConcernTests: MongoSwiftTestCase {
expect(try WriteConcern(w: .number(3))).toNot(throwError())
expect(try WriteConcern(journal: true, w: .number(1))).toNot(throwError())
expect(try WriteConcern(w: .number(0), wtimeoutMS: 1000)).toNot(throwError())
expect(try WriteConcern(w: .tag("hi"))).toNot(throwError())
expect(try WriteConcern(w: .custom("hi"))).toNot(throwError())
expect(try WriteConcern(w: .majority)).toNot(throwError())

// verify that this combination is considered invalid
Expand Down Expand Up @@ -147,7 +147,7 @@ final class WriteConcernTests: MongoSwiftTestCase {
let wcs: [WriteConcern] = [
.serverDefault,
try WriteConcern(w: .number(2)),
try WriteConcern(w: .tag("hi")),
try WriteConcern(w: .custom("hi")),
.majority,
try WriteConcern(journal: true),
try WriteConcern(wtimeoutMS: 200)
Expand Down