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
14 changes: 7 additions & 7 deletions Sources/MongoSwift/APM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public struct CommandStartedEvent: MongoSwiftEvent, CommandEventProtocol {
}

/// The command.
public let command: Document
public let command: BSONDocument

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

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

/// The command reply.
public let reply: Document
public let reply: BSONDocument

/// The command name.
public let commandName: String
Expand All @@ -189,7 +189,7 @@ public struct CommandSucceededEvent: MongoSwiftEvent, CommandEventProtocol {
// TODO: SWIFT-349 add logging to check and warn of unlikely int size issues
self.duration = Int(mongoc_apm_command_succeeded_get_duration(mongocEvent.ptr))
// we have to copy because libmongoc owns the pointer.
self.reply = Document(copying: mongoc_apm_command_succeeded_get_reply(mongocEvent.ptr))
self.reply = BSONDocument(copying: mongoc_apm_command_succeeded_get_reply(mongocEvent.ptr))
self.commandName = String(cString: mongoc_apm_command_succeeded_get_command_name(mongocEvent.ptr))
self.requestID = mongoc_apm_command_succeeded_get_request_id(mongocEvent.ptr)
self.operationID = mongoc_apm_command_succeeded_get_operation_id(mongocEvent.ptr)
Expand Down Expand Up @@ -240,7 +240,7 @@ public struct CommandFailedEvent: MongoSwiftEvent, CommandEventProtocol {
self.commandName = String(cString: mongoc_apm_command_failed_get_command_name(mongocEvent.ptr))
var error = bson_error_t()
mongoc_apm_command_failed_get_error(mongocEvent.ptr, &error)
let reply = Document(copying: mongoc_apm_command_failed_get_reply(mongocEvent.ptr))
let reply = BSONDocument(copying: mongoc_apm_command_failed_get_reply(mongocEvent.ptr))
self.failure = extractMongoError(error: error, reply: reply) // should always return a CommandError
self.requestID = mongoc_apm_command_failed_get_request_id(mongocEvent.ptr)
self.operationID = mongoc_apm_command_failed_get_operation_id(mongocEvent.ptr)
Expand Down Expand Up @@ -552,15 +552,15 @@ public struct ServerHeartbeatSucceededEvent: MongoSwiftEvent {
public let duration: Int

/// The command reply.
public let reply: Document
public let reply: BSONDocument

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

fileprivate init(mongocEvent: MongocServerHeartbeatSucceededEvent) {
self.duration = Int(mongoc_apm_server_heartbeat_succeeded_get_duration(mongocEvent.ptr))
// we have to copy because libmongoc owns the pointer.
self.reply = Document(copying: mongoc_apm_server_heartbeat_succeeded_get_reply(mongocEvent.ptr))
self.reply = BSONDocument(copying: mongoc_apm_server_heartbeat_succeeded_get_reply(mongocEvent.ptr))
self.serverAddress = ServerAddress(mongoc_apm_server_heartbeat_succeeded_get_host(mongocEvent.ptr))
}

Expand Down
42 changes: 21 additions & 21 deletions Sources/MongoSwift/BSON/BSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public enum BSON {
case string(String)

/// A BSON document.
case document(Document)
case document(BSONDocument)

/// A BSON array.
indirect case array([BSON])
Expand Down Expand Up @@ -40,7 +40,7 @@ public enum BSON {
case regex(BSONRegularExpression)

/// A BSON dbPointer.
case dbPointer(DBPointer)
case dbPointer(BSONDBPointer)

/// A BSON symbol.
case symbol(BSONSymbol)
Expand All @@ -63,7 +63,7 @@ public enum BSON {

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

/// A BSON minKey.
case minKey
Expand Down Expand Up @@ -158,8 +158,8 @@ public enum BSON {
return s
}

/// If this `BSON` is a `.document`, return it as a `Document`. Otherwise, return nil.
public var documentValue: Document? {
/// If this `BSON` is a `.document`, return it as a `BSONDocument`. Otherwise, return nil.
public var documentValue: BSONDocument? {
guard case let .document(d) = self else {
return nil
}
Expand Down Expand Up @@ -190,8 +190,8 @@ public enum BSON {
return d
}

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

/// If this `BSON` is a `.dbPointer`, return it as a `DBPointer`. Otherwise, return nil.
public var dbPointerValue: DBPointer? {
/// If this `BSON` is a `.dbPointer`, return it as a `BSONDBPointer`. Otherwise, return nil.
public var dbPointerValue: BSONDBPointer? {
guard case let .dbPointer(d) = self else {
return nil
}
Expand Down Expand Up @@ -281,18 +281,18 @@ public enum BSON {
}
}

/// Return this BSON as a `Decimal128` if possible.
/// This will coerce numeric cases (e.g. `.double`) into a `Decimal128` if such coercion would be lossless.
public func toDecimal128() -> Decimal128? {
/// Return this BSON as a `BSONDecimal128` if possible.
/// This will coerce numeric cases (e.g. `.double`) into a `BSONDecimal128` if such coercion would be lossless.
public func toDecimal128() -> BSONDecimal128? {
switch self {
case let .decimal128(d):
return d
case let .int64(i):
return Decimal128(String(i))
return BSONDecimal128(String(i))
case let .int32(i):
return Decimal128(String(i))
return BSONDecimal128(String(i))
case let .double(d):
return Decimal128(String(d))
return BSONDecimal128(String(d))
default:
return nil
}
Expand All @@ -305,25 +305,25 @@ extension BSON {
internal static var allBSONTypes: [BSONValue.Type] = [
BSONNull.self,
BSONUndefined.self,
MinKey.self,
BSONMinKey.self,
MaxKey.self,
BSONSymbol.self,
Double.self,
String.self,
Document.self,
BSONDocument.self,
BSONBinary.self,
BSONObjectID.self,
Bool.self,
Date.self,
BSONRegularExpression.self,
DBPointer.self,
BSONDBPointer.self,
BSONCode.self,
BSONCodeWithScope.self,
Int32.self,
BSONTimestamp.self,
Int64.self,
[BSON].self,
Decimal128.self
BSONDecimal128.self
]

/// Get the associated `BSONValue` to this `BSON` case.
Expand All @@ -334,7 +334,7 @@ extension BSON {
case .undefined:
return BSONUndefined()
case .minKey:
return MinKey()
return BSONMinKey()
case .maxKey:
return MaxKey()
case let .symbol(v):
Expand Down Expand Up @@ -403,7 +403,7 @@ extension BSON: ExpressibleByIntegerLiteral {

extension BSON: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String, BSON)...) {
self = .document(Document(keyValuePairs: elements))
self = .document(BSONDocument(keyValuePairs: elements))
}
}

Expand Down
20 changes: 10 additions & 10 deletions Sources/MongoSwift/BSON/BSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public class BSONDecoder {
* - Returns: A value of the requested type.
* - Throws: `DecodingError` if any value throws an error during decoding.
*/
public func decode<T: Decodable>(_ type: T.Type, from document: Document) throws -> T {
// if the requested type is `Document` we're done
public func decode<T: Decodable>(_ type: T.Type, from document: BSONDocument) throws -> T {
// if the requested type is `BSONDocument` we're done
if let doc = document as? T {
return doc
}
Expand All @@ -154,7 +154,7 @@ public class BSONDecoder {
* - Throws: `DecodingError` if the BSON data is corrupt or if any value throws an error during decoding.
*/
public func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
try self.decode(type, from: Document(fromBSON: data))
try self.decode(type, from: BSONDocument(fromBSON: data))
}

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

if let doc = try? Document(fromJSON: wrapped) {
if let doc = try? BSONDocument(fromJSON: wrapped) {
let s = try self.decode(DecodableWrapper<T>.self, from: doc)
return s.value
}
Expand Down Expand Up @@ -240,7 +240,7 @@ internal class _BSONDecoder: Decoder {
guard let topContainer = self.storage.topContainer.documentValue else {
throw DecodingError._typeMismatch(
at: self.codingPath,
expectation: Document.self,
expectation: BSONDocument.self,
reality: self.storage.topContainer.bsonValue
)
}
Expand Down Expand Up @@ -489,21 +489,21 @@ extension _BSONDecoder {
}
}

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

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

/// A reference to the container we're reading from.
fileprivate let container: Document
fileprivate let container: BSONDocument

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

/// Initializes `self`, referencing the given decoder and container.
fileprivate init(referencing decoder: _BSONDecoder, wrapping container: Document) {
fileprivate init(referencing decoder: _BSONDecoder, wrapping container: BSONDocument) {
self.decoder = decoder
self.container = container
self.codingPath = decoder.codingPath
Expand Down Expand Up @@ -617,7 +617,7 @@ private struct _BSONKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainer
guard let doc = value.documentValue else {
throw DecodingError._typeMismatch(
at: self.codingPath,
expectation: Document.self,
expectation: BSONDocument.self,
reality: value.bsonValue
)
}
Expand Down Expand Up @@ -789,7 +789,7 @@ private struct _BSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
throws -> KeyedDecodingContainer<NestedKey> {
try self.decoder.with(pushedKey: _BSONKey(index: self.currentIndex)) {
try self.checkAtEnd()
let doc = try self.decodeBSONType(Document.self)
let doc = try self.decodeBSONType(BSONDocument.self)
self.currentIndex += 1
let container = _BSONKeyedDecodingContainer<NestedKey>(referencing: self.decoder, wrapping: doc)
return KeyedDecodingContainer(container)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Foundation

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

// directly add the `Document` to the encoder's storage.
// directly add the `BSONDocument` to the encoder's storage.
bsonEncoder.storage.containers.append(self)
}

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

// we can just return the top container `Document`.
// we can just return the top container `BSONDocument`.
let topContainer = bsonDecoder.storage.topContainer
guard let doc = topContainer.documentValue else {
throw DecodingError._typeMismatch(at: [], expectation: Document.self, reality: topContainer.bsonValue)
throw DecodingError._typeMismatch(at: [], expectation: BSONDocument.self, reality: topContainer.bsonValue)
}
self = doc
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Foundation

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

Expand Down Expand Up @@ -31,14 +31,14 @@ extension Document: Collection {
return i + 1
}

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

Expand All @@ -51,10 +51,10 @@ extension Document: Collection {
return (iter.currentKey, iter.currentValue)
}

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