Skip to content

Commit 4240cf6

Browse files
authored
Merge pull request #87 from orlandos-nl/jo/expose-type-identifier
Expose the Type Identifer APIs and conversion from Dictionary to Document
2 parents 191caa7 + 4d53fb2 commit 4240cf6

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

Sources/BSON/Codable/Decoding/Fast/FastBSONDecoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ struct _FastKeyedContainer<Key: CodingKey>: KeyedDecodingContainerProtocol {
237237
}
238238

239239
func decodeNil(forKey key: Key) throws -> Bool {
240-
guard let type = document.typeIdentifier(of: key.stringValue) else {
240+
guard let type = document.typeIdentifier(forKey: key.stringValue) else {
241241
return true
242242
}
243243

Sources/BSON/Document/Document+Dictionary.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,48 @@ extension Document: ExpressibleByDictionaryLiteral {
243243
}
244244

245245
extension Dictionary: BSONPrimitiveRepresentable, Primitive where Key == String, Value: Primitive {
246+
public var document: Document {
247+
var document = Document(isArray: false)
248+
for (key, value) in self {
249+
document[key] = value
250+
}
251+
return document
252+
}
253+
246254
public var primitive: Primitive {
255+
return document
256+
}
257+
}
258+
259+
extension Dictionary where Key == String, Value == Primitive {
260+
public var document: Document {
247261
var document = Document(isArray: false)
248262
for (key, value) in self {
249263
document[key] = value
250264
}
251265
return document
252266
}
253267
}
268+
269+
extension Document {
270+
public init(dictionary: [String: Primitive]) {
271+
self.init(isArray: false)
272+
for (key, value) in dictionary {
273+
self.appendValue(value, forKey: key)
274+
}
275+
}
276+
277+
public init<Value>(dictionary: [String: Value]) throws where Value: Primitive {
278+
self.init(isArray: false)
279+
for (key, value) in dictionary {
280+
self.appendValue(try value.encodePrimitive(), forKey: key)
281+
}
282+
}
283+
284+
public init<Value>(dictionary: [String: Value]) throws where Value: PrimitiveEncodable {
285+
self.init(isArray: false)
286+
for (key, value) in dictionary {
287+
self.appendValue(try value.encodePrimitive(), forKey: key)
288+
}
289+
}
290+
}

Sources/BSON/Document/Document+Helpers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
extension Document {
2-
internal func typeIdentifier(of key: String) -> TypeIdentifier? {
2+
public func typeIdentifier(forKey key: String) -> TypeIdentifier? {
33
var index = 4
44

55
while index < storage.readableBytes {
@@ -27,7 +27,7 @@ extension Document {
2727
return nil
2828
}
2929

30-
internal func typeIdentifier(at index: Int) -> TypeIdentifier? {
30+
public func typeIdentifier(at index: Int) -> TypeIdentifier? {
3131
var offset = 4
3232

3333
for _ in 0..<index {

Sources/BSON/Types/TypeIdentifier.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
internal enum TypeIdentifier: UInt8 {
1+
/// - **Warning:** This enum is not exhaustive and may grow in the future. Always combine this with a `default` case in switch statements.
2+
public enum TypeIdentifier: UInt8 {
23
case double = 0x01
34
case string = 0x02
45
case document = 0x03
56
case array = 0x04
67
case binary = 0x05
8+
9+
// Deprecated case undefined = 0x06
10+
711
case objectId = 0x07
812
case boolean = 0x08
913
case datetime = 0x09
1014
case null = 0x0a
1115
case regex = 0x0b
12-
// case dbPointer = 0x0c
16+
17+
// Deprecated: case dbPointer = 0x0c
18+
1319
case javascript = 0x0d
14-
// case something = 0x0e
20+
21+
// Deprecated case symbol = 0x0e
22+
23+
/// Deprecated
1524
case javascriptWithScope = 0x0f
25+
1626
case int32 = 0x10
1727
case timestamp = 0x11
1828
case int64 = 0x12

Tests/LinuxMain.swift

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)