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
194 changes: 0 additions & 194 deletions Sources/MongoSwift/BSON/AnyBSONValue.swift

This file was deleted.

36 changes: 31 additions & 5 deletions Sources/MongoSwift/BSON/BSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public enum BSON {
/// Initialize a `BSON` from an integer. On 64-bit systems, this will result in an `.int64`. On 32-bit systems,
/// this will result in an `.int32`.
public init(_ int: Int) {
if Int.bsonType == .int32 {
if MemoryLayout<Int>.size == 4 {
self = .int32(Int32(int))
} else {
self = .int64(Int64(int))
}
}

/// Gets the `BSONType` of this `BSON`.
/// Get the `BSONType` of this `BSON`.
public var type: BSONType {
return self.bsonValue.bsonType
}
Expand Down Expand Up @@ -403,8 +403,7 @@ extension BSON: ExpressibleByIntegerLiteral {

extension BSON: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String, BSON)...) {
// TODO SWIFT-630: Implement this.
self = .document(Document())
self = .document(Document(keyValuePairs: elements))
}
}

Expand All @@ -418,4 +417,31 @@ extension BSON: Equatable {}

extension BSON: Hashable {}

// TODO SWIFT-629: Implement Codable conformance
extension BSON: Codable {
public init(from decoder: Decoder) throws {
if let bsonDecoder = decoder as? _BSONDecoder {
// This path only taken if a BSON is directly decoded at the top-level. Otherwise execution will never reach
// this point.
self = try bsonDecoder.decodeBSON()
} else {
// This path is taken no matter what when a non-BSONDecoder is used.
for bsonType in BSON.allBSONTypes {
if let value = try? bsonType.init(from: decoder) {
self = value.bson
}
}

throw DecodingError.typeMismatch(
BSON.self,
DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Encountered a value that could not be decoded to any BSON type")
)
}
}

public func encode(to encoder: Encoder) throws {
// This is only reached when a non-BSON encoder is used.
try self.bsonValue.encode(to: encoder)
}
}
Loading