Skip to content

Commit 32b8ea4

Browse files
authored
SWIFT-148: WriteConcern.description should return empty string on error (#88)
This also refactors JSON conversion functions for Document and ReadConcern.
1 parent 9a0f5bf commit 32b8ea4

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

Sources/MongoSwift/BSON/Document.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,24 @@ public struct Document: ExpressibleByDictionaryLiteral, ExpressibleByArrayLitera
168168
})
169169
}
170170

171-
/// Returns a relaxed extended JSON representation of this `Document`
171+
/// Returns the relaxed extended JSON representation of this `Document`.
172+
/// On error, an empty string will be returned.
172173
public var extendedJSON: String {
173-
let json = bson_as_relaxed_extended_json(self.data, nil)
174-
guard let jsonData = json else {
175-
return String()
174+
guard let json = bson_as_relaxed_extended_json(self.data, nil) else {
175+
return ""
176176
}
177177

178-
return String(cString: jsonData)
178+
return String(cString: json)
179179
}
180180

181-
/// Returns a canonical extended JSON representation of this `Document`
181+
/// Returns the canonical extended JSON representation of this `Document`.
182+
/// On error, an empty string will be returned.
182183
public var canonicalExtendedJSON: String {
183-
let json = bson_as_canonical_extended_json(self.data, nil)
184-
guard let jsonData = json else {
185-
return String()
184+
guard let json = bson_as_canonical_extended_json(self.data, nil) else {
185+
return ""
186186
}
187187

188-
return String(cString: jsonData)
188+
return String(cString: json)
189189
}
190190

191191
/// Returns a copy of the raw BSON data for this `Document`, represented as `Data`
@@ -320,7 +320,8 @@ extension Document: Equatable {
320320

321321
/// An extension of `Document` to make it convertible to a string.
322322
extension Document: CustomStringConvertible {
323-
/// An extended JSON description of this `Document`.
323+
/// Returns the relaxed extended JSON representation of this `Document`.
324+
/// On error, an empty string will be returned.
324325
public var description: String {
325326
return self.extendedJSON
326327
}

Sources/MongoSwift/ReadConcern.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ public class ReadConcern: Codable {
9797

9898
/// An extension of `ReadConcern` to make it `CustomStringConvertible`.
9999
extension ReadConcern: CustomStringConvertible {
100-
/// An extended JSON description of this `ReadConcern`, or the
101-
/// empty string if encoding fails.
100+
/// Returns the relaxed extended JSON representation of this `ReadConcern`.
101+
/// On error, an empty string will be returned.
102102
public var description: String {
103-
if let encoded = try? BsonEncoder().encode(self).description {
104-
return encoded
103+
guard let description = try? BsonEncoder().encode(self).description else {
104+
return ""
105105
}
106-
return ""
106+
return description
107107
}
108108
}
109109

Sources/MongoSwift/WriteConcern.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,13 @@ public class WriteConcern: Codable {
167167

168168
/// An extension of `WriteConcern` to make it `CustomStringConvertible`.
169169
extension WriteConcern: CustomStringConvertible {
170+
/// Returns the relaxed extended JSON representation of this `WriteConcern`.
171+
/// On error, an empty string will be returned.
170172
public var description: String {
171-
do {
172-
return try BsonEncoder().encode(self).description
173-
} catch {
174-
preconditionFailure("Error encoding WriteConcern to BSON: \(error)")
173+
guard let description = try? BsonEncoder().encode(self).description else {
174+
return ""
175175
}
176+
return description
176177
}
177178
}
178179

0 commit comments

Comments
 (0)