|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Non-encoder wrapper for `_PostgreSQLMessageEncoder`. |
| 4 | +final class PostgreSQLMessageEncoder { |
| 5 | + /// Create a new `PostgreSQLMessageEncoder` |
| 6 | + init() {} |
| 7 | + |
| 8 | + /// Encodes a `PostgreSQLMessage` to `Data`. |
| 9 | + func encode<Message>(_ message: Message) throws -> Data where Message: PostgreSQLMessage { |
| 10 | + let encoder = _PostgreSQLMessageEncoder() |
| 11 | + try message.encode(to: encoder) |
| 12 | + encoder.updateSize() |
| 13 | + if let identifier = Message.identifier { |
| 14 | + return [identifier] + encoder.data |
| 15 | + } else { |
| 16 | + return encoder.data |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// MARK: Encoder / Single |
| 22 | + |
| 23 | +internal final class _PostgreSQLMessageEncoder: Encoder, SingleValueEncodingContainer { |
| 24 | + var codingPath: [CodingKey] |
| 25 | + var userInfo: [CodingUserInfoKey: Any] |
| 26 | + var data: Data |
| 27 | + |
| 28 | + init() { |
| 29 | + self.codingPath = [] |
| 30 | + self.userInfo = [:] |
| 31 | + self.data = Data([0, 0, 0, 0]) |
| 32 | + } |
| 33 | + |
| 34 | + func updateSize() { |
| 35 | + let size = numericCast(data.count - 4) as Int32 |
| 36 | + data.withUnsafeMutableBytes { (pointer: UnsafeMutablePointer<Int32>) in |
| 37 | + pointer.pointee = size.bigEndian |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey { |
| 42 | + let container = PostgreSQLMessageKeyedEncodingContainer<Key>(encoder: self) |
| 43 | + return KeyedEncodingContainer(container) |
| 44 | + } |
| 45 | + |
| 46 | + func unkeyedContainer() -> UnkeyedEncodingContainer { |
| 47 | + return PostgreSQLMessageUnkeyedEncodingContainer(encoder: self) |
| 48 | + } |
| 49 | + |
| 50 | + func singleValueContainer() -> SingleValueEncodingContainer { |
| 51 | + return self |
| 52 | + } |
| 53 | + |
| 54 | + func encode(_ value: String) throws { |
| 55 | + // kafka style string |
| 56 | +// let stringData = Data(value.utf8) |
| 57 | +// |
| 58 | +// guard stringData.count < numericCast(Int16.max) else { |
| 59 | +// throw UnsupportedStringLength() |
| 60 | +// } |
| 61 | +// |
| 62 | +// try encode(numericCast(stringData.count) as Int16) |
| 63 | +// self.data.append(stringData) |
| 64 | + // c style string |
| 65 | + let stringData = Data(value.utf8) |
| 66 | + self.data.append(stringData + [0]) |
| 67 | + } |
| 68 | + |
| 69 | + func encode(_ value: Int8) throws { |
| 70 | + self.data.append(numericCast(value)) |
| 71 | + } |
| 72 | + |
| 73 | + func encode(_ value: Int16) throws { |
| 74 | + var value = value.bigEndian |
| 75 | + withUnsafeBytes(of: &value) { buffer in |
| 76 | + let buffer = buffer.baseAddress!.assumingMemoryBound(to: UInt8.self) |
| 77 | + self.data.append(buffer, count: 2) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func encode(_ value: Int32) throws { |
| 82 | + var value = value.bigEndian |
| 83 | + withUnsafeBytes(of: &value) { buffer in |
| 84 | + let buffer = buffer.baseAddress!.assumingMemoryBound(to: UInt8.self) |
| 85 | + self.data.append(buffer, count: 4) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + func encode(_ value: Int64) throws { |
| 90 | + var value = value.bigEndian |
| 91 | + withUnsafeBytes(of: &value) { buffer in |
| 92 | + let buffer = buffer.baseAddress!.assumingMemoryBound(to: UInt8.self) |
| 93 | + self.data.append(buffer, count: 8) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + func encode<T>(_ value: T) throws where T : Encodable { |
| 98 | + try value.encode(to: self) |
| 99 | + } |
| 100 | + |
| 101 | + // Unsupported |
| 102 | + |
| 103 | + func encode(_ value: Int) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 104 | + func encode(_ value: UInt) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 105 | + func encode(_ value: UInt8) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 106 | + func encode(_ value: UInt16) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 107 | + func encode(_ value: UInt32) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 108 | + func encode(_ value: UInt64) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 109 | + func encode(_ value: Float) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 110 | + func encode(_ value: Double) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 111 | + func encode(_ value: Bool) throws { fatalError("Unsupported type: \(type(of: value))") } |
| 112 | + func encodeNil() throws { fatalError("Unsupported type: nil") } |
| 113 | +} |
| 114 | + |
| 115 | +// MARK: Keyed |
| 116 | + |
| 117 | +internal struct PostgreSQLMessageKeyedEncodingContainer<K>: KeyedEncodingContainerProtocol where K: CodingKey { |
| 118 | + var count = 0 |
| 119 | + typealias Key = K |
| 120 | + |
| 121 | + var codingPath: [CodingKey] |
| 122 | + let encoder: _PostgreSQLMessageEncoder |
| 123 | + |
| 124 | + init(encoder: _PostgreSQLMessageEncoder) { |
| 125 | + self.encoder = encoder |
| 126 | + self.codingPath = [] |
| 127 | + } |
| 128 | + |
| 129 | + mutating func encode(_ value: Int, forKey key: K) throws { try encoder.encode(value) } |
| 130 | + mutating func encode(_ value: Int8, forKey key: K) throws { try encoder.encode(value) } |
| 131 | + mutating func encode(_ value: Int16, forKey key: K) throws { try encoder.encode(value) } |
| 132 | + mutating func encode(_ value: Int32, forKey key: K) throws { try encoder.encode(value) } |
| 133 | + mutating func encode(_ value: Int64, forKey key: K) throws { try encoder.encode(value) } |
| 134 | + mutating func encode(_ value: UInt, forKey key: K) throws { try encoder.encode(value) } |
| 135 | + mutating func encode(_ value: UInt8, forKey key: K) throws { try encoder.encode(value) } |
| 136 | + mutating func encode(_ value: UInt16, forKey key: K) throws { try encoder.encode(value) } |
| 137 | + mutating func encode(_ value: UInt32, forKey key: K) throws { try encoder.encode(value) } |
| 138 | + mutating func encode(_ value: UInt64, forKey key: K) throws { try encoder.encode(value) } |
| 139 | + mutating func encode(_ value: Float, forKey key: K) throws { try encoder.encode(value) } |
| 140 | + mutating func encode(_ value: Double, forKey key: K) throws { try encoder.encode(value) } |
| 141 | + mutating func encode(_ value: String, forKey key: K) throws { try encoder.encode(value) } |
| 142 | + mutating func encode<T>(_ value: T, forKey key: K) throws where T : Encodable { try value.encode(to: encoder)} |
| 143 | + mutating func encode(_ value: Bool, forKey key: K) throws { try encoder.encode(value) } |
| 144 | + mutating func encodeNil(forKey key: K) throws { try encoder.encodeNil() } |
| 145 | + mutating func superEncoder() -> Encoder { return encoder } |
| 146 | + mutating func superEncoder(forKey key: K) -> Encoder { return encoder } |
| 147 | + |
| 148 | + mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: K) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey { |
| 149 | + let container = PostgreSQLMessageKeyedEncodingContainer<NestedKey>(encoder: encoder) |
| 150 | + return KeyedEncodingContainer(container) |
| 151 | + } |
| 152 | + |
| 153 | + mutating func nestedUnkeyedContainer(forKey key: K) -> UnkeyedEncodingContainer { |
| 154 | + return PostgreSQLMessageUnkeyedEncodingContainer(encoder: encoder) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// MARK: Unkeyed |
| 159 | + |
| 160 | +internal struct PostgreSQLMessageUnkeyedEncodingContainer: UnkeyedEncodingContainer { |
| 161 | + var codingPath: [CodingKey] |
| 162 | + var count: Int |
| 163 | + let encoder: _PostgreSQLMessageEncoder |
| 164 | + |
| 165 | + init(encoder: _PostgreSQLMessageEncoder) { |
| 166 | + self.encoder = encoder |
| 167 | + self.codingPath = [] |
| 168 | + self.count = 0 |
| 169 | + } |
| 170 | + |
| 171 | + mutating func encode(_ value: Int) throws { try encoder.encode(value) } |
| 172 | + mutating func encode(_ value: Int8) throws { try encoder.encode(value) } |
| 173 | + mutating func encode(_ value: Int16) throws { try encoder.encode(value) } |
| 174 | + mutating func encode(_ value: Int32) throws { try encoder.encode(value) } |
| 175 | + mutating func encode(_ value: Int64) throws { try encoder.encode(value) } |
| 176 | + mutating func encode(_ value: UInt) throws { try encoder.encode(value) } |
| 177 | + mutating func encode(_ value: UInt8) throws { try encoder.encode(value) } |
| 178 | + mutating func encode(_ value: UInt16) throws { try encoder.encode(value) } |
| 179 | + mutating func encode(_ value: UInt32) throws { try encoder.encode(value) } |
| 180 | + mutating func encode(_ value: UInt64) throws { try encoder.encode(value) } |
| 181 | + mutating func encode(_ value: Float) throws { try encoder.encode(value) } |
| 182 | + mutating func encode(_ value: Double) throws { try encoder.encode(value) } |
| 183 | + mutating func encode(_ value: String) throws { try encoder.encode(value) } |
| 184 | + mutating func encode<T>(_ value: T) throws where T : Encodable { try value.encode(to: encoder)} |
| 185 | + mutating func encode(_ value: Bool) throws { try encoder.encode(value) } |
| 186 | + mutating func encodeNil() throws { try encoder.encodeNil() } |
| 187 | + mutating func superEncoder() -> Encoder { return encoder } |
| 188 | + mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer { return self } |
| 189 | + |
| 190 | + mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey { |
| 191 | + let container = PostgreSQLMessageKeyedEncodingContainer<NestedKey>(encoder: encoder) |
| 192 | + return KeyedEncodingContainer(container) |
| 193 | + } |
| 194 | +} |
0 commit comments