Skip to content

Commit 8435316

Browse files
authored
SWIFT-822 Prefix remaining BSON types (#482)
1 parent 79dfa37 commit 8435316

File tree

97 files changed

+1074
-1053
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1074
-1053
lines changed

Sources/MongoSwift/APM.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public struct CommandStartedEvent: MongoSwiftEvent, CommandEventProtocol {
118118
}
119119

120120
/// The command.
121-
public let command: Document
121+
public let command: BSONDocument
122122

123123
/// The database name.
124124
public let databaseName: String
@@ -138,7 +138,7 @@ public struct CommandStartedEvent: MongoSwiftEvent, CommandEventProtocol {
138138

139139
fileprivate init(mongocEvent: MongocCommandStartedEvent) {
140140
// we have to copy because libmongoc owns the pointer.
141-
self.command = Document(copying: mongoc_apm_command_started_get_command(mongocEvent.ptr))
141+
self.command = BSONDocument(copying: mongoc_apm_command_started_get_command(mongocEvent.ptr))
142142
self.databaseName = String(cString: mongoc_apm_command_started_get_database_name(mongocEvent.ptr))
143143
self.commandName = String(cString: mongoc_apm_command_started_get_command_name(mongocEvent.ptr))
144144
self.requestID = mongoc_apm_command_started_get_request_id(mongocEvent.ptr)
@@ -170,7 +170,7 @@ public struct CommandSucceededEvent: MongoSwiftEvent, CommandEventProtocol {
170170
public let duration: Int
171171

172172
/// The command reply.
173-
public let reply: Document
173+
public let reply: BSONDocument
174174

175175
/// The command name.
176176
public let commandName: String
@@ -189,7 +189,7 @@ public struct CommandSucceededEvent: MongoSwiftEvent, CommandEventProtocol {
189189
// TODO: SWIFT-349 add logging to check and warn of unlikely int size issues
190190
self.duration = Int(mongoc_apm_command_succeeded_get_duration(mongocEvent.ptr))
191191
// we have to copy because libmongoc owns the pointer.
192-
self.reply = Document(copying: mongoc_apm_command_succeeded_get_reply(mongocEvent.ptr))
192+
self.reply = BSONDocument(copying: mongoc_apm_command_succeeded_get_reply(mongocEvent.ptr))
193193
self.commandName = String(cString: mongoc_apm_command_succeeded_get_command_name(mongocEvent.ptr))
194194
self.requestID = mongoc_apm_command_succeeded_get_request_id(mongocEvent.ptr)
195195
self.operationID = mongoc_apm_command_succeeded_get_operation_id(mongocEvent.ptr)
@@ -240,7 +240,7 @@ public struct CommandFailedEvent: MongoSwiftEvent, CommandEventProtocol {
240240
self.commandName = String(cString: mongoc_apm_command_failed_get_command_name(mongocEvent.ptr))
241241
var error = bson_error_t()
242242
mongoc_apm_command_failed_get_error(mongocEvent.ptr, &error)
243-
let reply = Document(copying: mongoc_apm_command_failed_get_reply(mongocEvent.ptr))
243+
let reply = BSONDocument(copying: mongoc_apm_command_failed_get_reply(mongocEvent.ptr))
244244
self.failure = extractMongoError(error: error, reply: reply) // should always return a CommandError
245245
self.requestID = mongoc_apm_command_failed_get_request_id(mongocEvent.ptr)
246246
self.operationID = mongoc_apm_command_failed_get_operation_id(mongocEvent.ptr)
@@ -552,15 +552,15 @@ public struct ServerHeartbeatSucceededEvent: MongoSwiftEvent {
552552
public let duration: Int
553553

554554
/// The command reply.
555-
public let reply: Document
555+
public let reply: BSONDocument
556556

557557
/// The address of the server.
558558
public let serverAddress: ServerAddress
559559

560560
fileprivate init(mongocEvent: MongocServerHeartbeatSucceededEvent) {
561561
self.duration = Int(mongoc_apm_server_heartbeat_succeeded_get_duration(mongocEvent.ptr))
562562
// we have to copy because libmongoc owns the pointer.
563-
self.reply = Document(copying: mongoc_apm_server_heartbeat_succeeded_get_reply(mongocEvent.ptr))
563+
self.reply = BSONDocument(copying: mongoc_apm_server_heartbeat_succeeded_get_reply(mongocEvent.ptr))
564564
self.serverAddress = ServerAddress(mongoc_apm_server_heartbeat_succeeded_get_host(mongocEvent.ptr))
565565
}
566566

Sources/MongoSwift/BSON/BSON.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public enum BSON {
1111
case string(String)
1212

1313
/// A BSON document.
14-
case document(Document)
14+
case document(BSONDocument)
1515

1616
/// A BSON array.
1717
indirect case array([BSON])
@@ -40,7 +40,7 @@ public enum BSON {
4040
case regex(BSONRegularExpression)
4141

4242
/// A BSON dbPointer.
43-
case dbPointer(DBPointer)
43+
case dbPointer(BSONDBPointer)
4444

4545
/// A BSON symbol.
4646
case symbol(BSONSymbol)
@@ -63,7 +63,7 @@ public enum BSON {
6363

6464
/// A BSON Decimal128.
6565
/// - SeeAlso: https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst
66-
case decimal128(Decimal128)
66+
case decimal128(BSONDecimal128)
6767

6868
/// A BSON minKey.
6969
case minKey
@@ -158,8 +158,8 @@ public enum BSON {
158158
return s
159159
}
160160

161-
/// If this `BSON` is a `.document`, return it as a `Document`. Otherwise, return nil.
162-
public var documentValue: Document? {
161+
/// If this `BSON` is a `.document`, return it as a `BSONDocument`. Otherwise, return nil.
162+
public var documentValue: BSONDocument? {
163163
guard case let .document(d) = self else {
164164
return nil
165165
}
@@ -190,8 +190,8 @@ public enum BSON {
190190
return d
191191
}
192192

193-
/// If this `BSON` is a `.decimal128`, return it as a `Decimal128`. Otherwise, return nil.
194-
public var decimal128Value: Decimal128? {
193+
/// If this `BSON` is a `.decimal128`, return it as a `BSONDecimal128`. Otherwise, return nil.
194+
public var decimal128Value: BSONDecimal128? {
195195
guard case let .decimal128(d) = self else {
196196
return nil
197197
}
@@ -206,8 +206,8 @@ public enum BSON {
206206
return s
207207
}
208208

209-
/// If this `BSON` is a `.dbPointer`, return it as a `DBPointer`. Otherwise, return nil.
210-
public var dbPointerValue: DBPointer? {
209+
/// If this `BSON` is a `.dbPointer`, return it as a `BSONDBPointer`. Otherwise, return nil.
210+
public var dbPointerValue: BSONDBPointer? {
211211
guard case let .dbPointer(d) = self else {
212212
return nil
213213
}
@@ -281,18 +281,18 @@ public enum BSON {
281281
}
282282
}
283283

284-
/// Return this BSON as a `Decimal128` if possible.
285-
/// This will coerce numeric cases (e.g. `.double`) into a `Decimal128` if such coercion would be lossless.
286-
public func toDecimal128() -> Decimal128? {
284+
/// Return this BSON as a `BSONDecimal128` if possible.
285+
/// This will coerce numeric cases (e.g. `.double`) into a `BSONDecimal128` if such coercion would be lossless.
286+
public func toDecimal128() -> BSONDecimal128? {
287287
switch self {
288288
case let .decimal128(d):
289289
return d
290290
case let .int64(i):
291-
return Decimal128(String(i))
291+
return BSONDecimal128(String(i))
292292
case let .int32(i):
293-
return Decimal128(String(i))
293+
return BSONDecimal128(String(i))
294294
case let .double(d):
295-
return Decimal128(String(d))
295+
return BSONDecimal128(String(d))
296296
default:
297297
return nil
298298
}
@@ -305,25 +305,25 @@ extension BSON {
305305
internal static var allBSONTypes: [BSONValue.Type] = [
306306
BSONNull.self,
307307
BSONUndefined.self,
308-
MinKey.self,
308+
BSONMinKey.self,
309309
MaxKey.self,
310310
BSONSymbol.self,
311311
Double.self,
312312
String.self,
313-
Document.self,
313+
BSONDocument.self,
314314
BSONBinary.self,
315315
BSONObjectID.self,
316316
Bool.self,
317317
Date.self,
318318
BSONRegularExpression.self,
319-
DBPointer.self,
319+
BSONDBPointer.self,
320320
BSONCode.self,
321321
BSONCodeWithScope.self,
322322
Int32.self,
323323
BSONTimestamp.self,
324324
Int64.self,
325325
[BSON].self,
326-
Decimal128.self
326+
BSONDecimal128.self
327327
]
328328

329329
/// Get the associated `BSONValue` to this `BSON` case.
@@ -334,7 +334,7 @@ extension BSON {
334334
case .undefined:
335335
return BSONUndefined()
336336
case .minKey:
337-
return MinKey()
337+
return BSONMinKey()
338338
case .maxKey:
339339
return MaxKey()
340340
case let .symbol(v):
@@ -403,7 +403,7 @@ extension BSON: ExpressibleByIntegerLiteral {
403403

404404
extension BSON: ExpressibleByDictionaryLiteral {
405405
public init(dictionaryLiteral elements: (String, BSON)...) {
406-
self = .document(Document(keyValuePairs: elements))
406+
self = .document(BSONDocument(keyValuePairs: elements))
407407
}
408408
}
409409

Sources/MongoSwift/BSON/BSONDecoder.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ public class BSONDecoder {
136136
* - Returns: A value of the requested type.
137137
* - Throws: `DecodingError` if any value throws an error during decoding.
138138
*/
139-
public func decode<T: Decodable>(_ type: T.Type, from document: Document) throws -> T {
140-
// if the requested type is `Document` we're done
139+
public func decode<T: Decodable>(_ type: T.Type, from document: BSONDocument) throws -> T {
140+
// if the requested type is `BSONDocument` we're done
141141
if let doc = document as? T {
142142
return doc
143143
}
@@ -154,7 +154,7 @@ public class BSONDecoder {
154154
* - Throws: `DecodingError` if the BSON data is corrupt or if any value throws an error during decoding.
155155
*/
156156
public func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
157-
try self.decode(type, from: Document(fromBSON: data))
157+
try self.decode(type, from: BSONDocument(fromBSON: data))
158158
}
159159

160160
/**
@@ -177,7 +177,7 @@ public class BSONDecoder {
177177
// and pay a small performance penalty of decoding a few extra bytes.
178178
let wrapped = "{\"value\": \(json)}"
179179

180-
if let doc = try? Document(fromJSON: wrapped) {
180+
if let doc = try? BSONDocument(fromJSON: wrapped) {
181181
let s = try self.decode(DecodableWrapper<T>.self, from: doc)
182182
return s.value
183183
}
@@ -240,7 +240,7 @@ internal class _BSONDecoder: Decoder {
240240
guard let topContainer = self.storage.topContainer.documentValue else {
241241
throw DecodingError._typeMismatch(
242242
at: self.codingPath,
243-
expectation: Document.self,
243+
expectation: BSONDocument.self,
244244
reality: self.storage.topContainer.bsonValue
245245
)
246246
}
@@ -489,21 +489,21 @@ extension _BSONDecoder {
489489
}
490490
}
491491

492-
/// A keyed decoding container, backed by a `Document`.
492+
/// A keyed decoding container, backed by a `BSONDocument`.
493493
private struct _BSONKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
494494
typealias Key = K
495495

496496
/// A reference to the decoder we're reading from.
497497
private let decoder: _BSONDecoder
498498

499499
/// A reference to the container we're reading from.
500-
fileprivate let container: Document
500+
fileprivate let container: BSONDocument
501501

502502
/// The path of coding keys taken to get to this point in decoding.
503503
public private(set) var codingPath: [CodingKey]
504504

505505
/// Initializes `self`, referencing the given decoder and container.
506-
fileprivate init(referencing decoder: _BSONDecoder, wrapping container: Document) {
506+
fileprivate init(referencing decoder: _BSONDecoder, wrapping container: BSONDocument) {
507507
self.decoder = decoder
508508
self.container = container
509509
self.codingPath = decoder.codingPath
@@ -617,7 +617,7 @@ private struct _BSONKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainer
617617
guard let doc = value.documentValue else {
618618
throw DecodingError._typeMismatch(
619619
at: self.codingPath,
620-
expectation: Document.self,
620+
expectation: BSONDocument.self,
621621
reality: value.bsonValue
622622
)
623623
}
@@ -789,7 +789,7 @@ private struct _BSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
789789
throws -> KeyedDecodingContainer<NestedKey> {
790790
try self.decoder.with(pushedKey: _BSONKey(index: self.currentIndex)) {
791791
try self.checkAtEnd()
792-
let doc = try self.decodeBSONType(Document.self)
792+
let doc = try self.decodeBSONType(BSONDocument.self)
793793
self.currentIndex += 1
794794
let container = _BSONKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: doc)
795795
return KeyedDecodingContainer(container)

Sources/MongoSwift/BSON/Document+Codable.swift renamed to Sources/MongoSwift/BSON/BSONDocument+Codable.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Foundation
22

3-
/// An extension of `Document` to implement the `Codable` protocol.
4-
extension Document: Codable {
3+
/// An extension of `BSONDocument` to implement the `Codable` protocol.
4+
extension BSONDocument: Codable {
55
public func encode(to encoder: Encoder) throws {
66
guard let bsonEncoder = encoder as? _BSONEncoder else {
77
throw bsonEncodingUnsupportedError(value: self, at: encoder.codingPath)
88
}
99

10-
// directly add the `Document` to the encoder's storage.
10+
// directly add the `BSONDocument` to the encoder's storage.
1111
bsonEncoder.storage.containers.append(self)
1212
}
1313

@@ -18,13 +18,13 @@ extension Document: Codable {
1818
public init(from decoder: Decoder) throws {
1919
// currently we only support decoding to a document using a BSONDecoder.
2020
guard let bsonDecoder = decoder as? _BSONDecoder else {
21-
throw getDecodingError(type: Document.self, decoder: decoder)
21+
throw getDecodingError(type: BSONDocument.self, decoder: decoder)
2222
}
2323

24-
// we can just return the top container `Document`.
24+
// we can just return the top container `BSONDocument`.
2525
let topContainer = bsonDecoder.storage.topContainer
2626
guard let doc = topContainer.documentValue else {
27-
throw DecodingError._typeMismatch(at: [], expectation: Document.self, reality: topContainer.bsonValue)
27+
throw DecodingError._typeMismatch(at: [], expectation: BSONDocument.self, reality: topContainer.bsonValue)
2828
}
2929
self = doc
3030
}

Sources/MongoSwift/BSON/Document+Collection.swift renamed to Sources/MongoSwift/BSON/BSONDocument+Collection.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import Foundation
22

3-
/// An extension of `Document` to make it conform to the `Collection` protocol.
3+
/// An extension of `BSONDocument` to make it conform to the `Collection` protocol.
44
/// This gives guarantees on non-destructive iteration, and offers an indexed
55
/// ordering to the key-value pairs in the document.
6-
extension Document: Collection {
6+
extension BSONDocument: Collection {
77
/// The index type of a document.
88
public typealias Index = Int
99

@@ -31,14 +31,14 @@ extension Document: Collection {
3131
return i + 1
3232
}
3333

34-
/// Allows access to a `KeyValuePair` from the `Document`, given the position of the desired `KeyValuePair` held
34+
/// Allows access to a `KeyValuePair` from the `BSONDocument`, given the position of the desired `KeyValuePair` held
3535
/// within. This method does not guarantee constant-time (O(1)) access.
3636
public subscript(position: Index) -> KeyValuePair {
3737
// TODO: This method _should_ guarantee constant-time O(1) access, and it is possible to make it do so. This
3838
// criticism also applies to key-based subscripting via `String`.
3939
// See SWIFT-250.
4040
self.failIndexCheck(position)
41-
guard let iter = DocumentIterator(over: self) else {
41+
guard let iter = BSONDocumentIterator(over: self) else {
4242
fatalError("Failed to initialize an iterator over document \(self)")
4343
}
4444

@@ -51,10 +51,10 @@ extension Document: Collection {
5151
return (iter.currentKey, iter.currentValue)
5252
}
5353

54-
/// Allows access to a `KeyValuePair` from the `Document`, given a range of indices of the desired `KeyValuePair`'s
55-
/// held within. This method does not guarantee constant-time (O(1)) access.
56-
public subscript(bounds: Range<Index>) -> Document {
54+
/// Allows access to a `KeyValuePair` from the `BSONDocument`, given a range of indices of the desired
55+
/// `KeyValuePair`'s held within. This method does not guarantee constant-time (O(1)) access.
56+
public subscript(bounds: Range<Index>) -> BSONDocument {
5757
// TODO: SWIFT-252 should provide a more efficient implementation for this.
58-
DocumentIterator.subsequence(of: self, startIndex: bounds.lowerBound, endIndex: bounds.upperBound)
58+
BSONDocumentIterator.subsequence(of: self, startIndex: bounds.lowerBound, endIndex: bounds.upperBound)
5959
}
6060
}

0 commit comments

Comments
 (0)