forked from vapor/postgres-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgreSQLDataEncoder.swift
174 lines (138 loc) · 5.15 KB
/
PostgreSQLDataEncoder.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/// Converts `Encodable` objects to `PostgreSQLData`.
///
/// let data = try PostgreSQLDataEncoder().encode("hello")
/// print(data) // PostgreSQLData
///
public struct PostgreSQLDataEncoder {
/// Creates a new `PostgreSQLDataEncoder`.
public init() { }
/// Encodes the supplied `Encodable` object to `PostgreSQLData`.
///
/// let data = try PostgreSQLDataEncoder().encode("hello")
/// print(data) // PostgreSQLData
///
/// - parameters:
/// - encodable: `Encodable` object to encode.
/// - returns: Encoded `PostgreSQLData`.
public func encode(_ encodable: Encodable) throws -> PostgreSQLData {
do {
let encoder = _Encoder()
try encodable.encode(to: encoder)
guard let data = encoder.data else {
fatalError()
}
return data
} catch is _KeyedError {
struct AnyEncodable: Encodable {
var encodable: Encodable
init(_ encodable: Encodable) {
self.encodable = encodable
}
func encode(to encoder: Encoder) throws {
try encodable.encode(to: encoder)
}
}
return try PostgreSQLData(.jsonb, binary: [0x01] + JSONEncoder().encode(AnyEncodable(encodable)))
}
}
// MARK: Private
private final class _Encoder: Encoder {
let codingPath: [CodingKey] = []
let userInfo: [CodingUserInfoKey: Any] = [:]
var data: PostgreSQLData?
init() {
self.data = nil
}
func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
return .init(_KeyedEncodingContainer())
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
fatalError()
}
func singleValueContainer() -> SingleValueEncodingContainer {
return _SingleValueEncodingContainer(encoder: self)
}
}
private struct _SingleValueEncodingContainer: SingleValueEncodingContainer {
let codingPath: [CodingKey] = []
let encoder: _Encoder
init(encoder: _Encoder) {
self.encoder = encoder
}
mutating func encodeNil() throws {
encoder.data = PostgreSQLData(null: .null)
}
mutating func encode(_ value: Bool) throws {
fatalError()
}
mutating func encode(_ value: String) throws {
encoder.data = PostgreSQLData(.text, binary: Data(value.utf8))
}
mutating func encode(_ value: Double) throws {
fatalError()
}
mutating func encode(_ value: Float) throws {
fatalError()
}
mutating func encode(_ value: Int) throws {
fatalError()
}
mutating func encode(_ value: Int8) throws {
fatalError()
}
mutating func encode(_ value: Int16) throws {
fatalError()
}
mutating func encode(_ value: Int32) throws {
fatalError()
}
mutating func encode(_ value: Int64) throws {
fatalError()
}
mutating func encode(_ value: UInt) throws {
fatalError()
}
mutating func encode(_ value: UInt8) throws {
fatalError()
}
mutating func encode(_ value: UInt16) throws {
fatalError()
}
mutating func encode(_ value: UInt32) throws {
fatalError()
}
mutating func encode(_ value: UInt64) throws {
fatalError()
}
mutating func encode<T>(_ value: T) throws where T : Encodable {
if let convertible = value as? PostgreSQLDataConvertible {
encoder.data = try convertible.convertToPostgreSQLData()
} else {
try value.encode(to: encoder)
}
}
}
private struct _KeyedError: Error { }
private struct _KeyedEncodingContainer<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
let codingPath: [CodingKey] = []
init() { }
mutating func encodeNil(forKey key: Key) throws {
throw _KeyedError()
}
mutating func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable {
throw _KeyedError()
}
mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey {
return .init(_KeyedEncodingContainer<NestedKey>())
}
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
fatalError()
}
mutating func superEncoder() -> Encoder {
fatalError()
}
mutating func superEncoder(forKey key: Key) -> Encoder {
fatalError()
}
}
}