-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathBSONSymbol.swift
70 lines (59 loc) · 2.5 KB
/
BSONSymbol.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import NIO
/// A struct to represent the deprecated Symbol type.
/// Symbols cannot be instantiated, but they can be read from existing documents that contain them.
public struct BSONSymbol: BSONValue, CustomStringConvertible, Equatable, Hashable {
internal static let extJSONTypeWrapperKeys: [String] = ["$symbol"]
/*
* Initializes a `Symbol` from ExtendedJSON.
*
* Parameters:
* - `json`: a `JSON` representing the canonical or relaxed form of ExtendedJSON for a `Symbol`.
* - `keyPath`: an array of `Strings`s containing the enclosing JSON keys of the current json being passed in.
* This is used for error messages.
*
* Throws:
* - `DecodingError` if `json` is a partial match or is malformed.
*
* Returns:
* - `nil` if the provided value is not an `Symbol`.
*/
internal init?(fromExtJSON json: JSON, keyPath: [String]) throws {
guard let value = try json.value.unwrapObject(withKey: "$symbol", keyPath: keyPath) else {
return nil
}
guard let str = value.stringValue else {
throw DecodingError._extendedJSONError(
keyPath: keyPath,
debugDescription:
"Could not parse `Symbol` from \"\(value)\", input must be a string."
)
}
self = BSONSymbol(str)
}
/// Converts this `Symbol` to a corresponding `JSON` in relaxed extendedJSON format.
internal func toRelaxedExtendedJSON() -> JSON {
self.toCanonicalExtendedJSON()
}
/// Converts this `Symbol` to a corresponding `JSON` in canonical extendedJSON format.
internal func toCanonicalExtendedJSON() -> JSON {
["$symbol": JSON(.string(self.stringValue))]
}
internal static var bsonType: BSONType { .symbol }
internal var bson: BSON { .symbol(self) }
public var description: String { self.stringValue }
/// String representation of this `BSONSymbol`.
public let stringValue: String
internal init(_ stringValue: String) {
self.stringValue = stringValue
}
internal static func read(from buffer: inout ByteBuffer) throws -> BSON {
let string = try String.read(from: &buffer)
guard let stringValue = string.stringValue else {
throw BSONError.InternalError(message: "Cannot get string value of BSON symbol")
}
return .symbol(BSONSymbol(stringValue))
}
internal func write(to buffer: inout ByteBuffer) {
self.stringValue.write(to: &buffer)
}
}