Skip to content

Commit 79dfa37

Browse files
authored
SWIFT-863 Rename WriteConcern tag case to custom (#481)
1 parent 1d1e6b3 commit 79dfa37

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

Sources/MongoSwift/WriteConcern.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,25 @@ public struct WriteConcern: Codable {
2727
public static let serverDefault = WriteConcern()
2828

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

3845
public init(from decoder: Decoder) throws {
3946
let container = try decoder.singleValueContainer()
4047
if let string = try? container.decode(String.self) {
41-
self = string == "majority" ? .majority : .tag(string)
48+
self = string == "majority" ? .majority : .custom(string)
4249
} else {
4350
let wNumber = try container.decode(Int.self)
4451
self = .number(wNumber)
@@ -57,8 +64,8 @@ public struct WriteConcern: Codable {
5764
message: "Invalid WriteConcern.w \(wNumber): must be between 0 and \(Int32.max)"
5865
)
5966
}
60-
case let .tag(wTag):
61-
try container.encode(wTag)
67+
case let .custom(name):
68+
try container.encode(name)
6269
case .majority:
6370
try container.encode("majority")
6471
}
@@ -152,8 +159,8 @@ public struct WriteConcern: Codable {
152159
case MONGOC_WRITE_CONCERN_W_MAJORITY:
153160
self.w = .majority
154161
case MONGOC_WRITE_CONCERN_W_TAG:
155-
if let wTag = mongoc_write_concern_get_wtag(writeConcern) {
156-
self.w = .tag(String(cString: wTag))
162+
if let name = mongoc_write_concern_get_wtag(writeConcern) {
163+
self.w = .custom(String(cString: name))
157164
} else {
158165
self.w = nil
159166
}
@@ -187,8 +194,8 @@ public struct WriteConcern: Codable {
187194
switch w {
188195
case let .number(wNumber):
189196
mongoc_write_concern_set_w(writeConcern, Int32(wNumber))
190-
case let .tag(wTag):
191-
mongoc_write_concern_set_wtag(writeConcern, wTag)
197+
case let .custom(name):
198+
mongoc_write_concern_set_wtag(writeConcern, name)
192199
case .majority:
193200
mongoc_write_concern_set_w(writeConcern, MONGOC_WRITE_CONCERN_W_MAJORITY)
194201
}

Tests/MongoSwiftTests/ReadWriteConcernSpecTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ extension WriteConcern {
1111
let j = doc["journal"]?.boolValue
1212

1313
var w: W?
14-
if let wtag = doc["w"]?.stringValue {
15-
w = wtag == "majority" ? .majority : .tag(wtag)
14+
if let str = doc["w"]?.stringValue {
15+
w = str == "majority" ? .majority : .custom(str)
1616
} else if let wInt = doc["w"]?.toInt() {
1717
w = .number(wInt)
1818
}

Tests/MongoSwiftTests/WriteConcernTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class WriteConcernTests: MongoSwiftTestCase {
3535
expect(try WriteConcern(w: .number(3))).toNot(throwError())
3636
expect(try WriteConcern(journal: true, w: .number(1))).toNot(throwError())
3737
expect(try WriteConcern(w: .number(0), wtimeoutMS: 1000)).toNot(throwError())
38-
expect(try WriteConcern(w: .tag("hi"))).toNot(throwError())
38+
expect(try WriteConcern(w: .custom("hi"))).toNot(throwError())
3939
expect(try WriteConcern(w: .majority)).toNot(throwError())
4040

4141
// verify that this combination is considered invalid
@@ -147,7 +147,7 @@ final class WriteConcernTests: MongoSwiftTestCase {
147147
let wcs: [WriteConcern] = [
148148
.serverDefault,
149149
try WriteConcern(w: .number(2)),
150-
try WriteConcern(w: .tag("hi")),
150+
try WriteConcern(w: .custom("hi")),
151151
.majority,
152152
try WriteConcern(journal: true),
153153
try WriteConcern(wtimeoutMS: 200)

0 commit comments

Comments
 (0)