|
| 1 | +internal final class PostgreSQLRowDecoder: Decoder { |
| 2 | + var codingPath: [CodingKey] |
| 3 | + var userInfo: [CodingUserInfoKey: Any] |
| 4 | + let data: [String: PostgreSQLData] |
| 5 | + init(row: [String: PostgreSQLData]) { |
| 6 | + self.data = row |
| 7 | + self.codingPath = [] |
| 8 | + self.userInfo = [:] |
| 9 | + } |
| 10 | + |
| 11 | + func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey { |
| 12 | + let container = PostgreSQLRowKeyedDecodingContainer<Key>(decoder: self) |
| 13 | + return KeyedDecodingContainer(container) |
| 14 | + } |
| 15 | + |
| 16 | + func unkeyedContainer() throws -> UnkeyedDecodingContainer { |
| 17 | + throw unsupported |
| 18 | + } |
| 19 | + |
| 20 | + func singleValueContainer() throws -> SingleValueDecodingContainer { |
| 21 | + throw unsupported |
| 22 | + } |
| 23 | + |
| 24 | + func require(key: CodingKey) throws -> PostgreSQLData { |
| 25 | + guard let data = self.data[key.stringValue] else { |
| 26 | + throw PostgreSQLError(identifier: "decode", reason: "No value found at key: \(key)") |
| 27 | + } |
| 28 | + return data |
| 29 | + } |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +private let unsupported = PostgreSQLError( |
| 34 | + identifier: "rowDecode", |
| 35 | + reason: "PostgreSQL rows only support a flat, keyed structure `[String: T]`", |
| 36 | + suggestedFixes: [ |
| 37 | + "You can conform nested types to `PostgreSQLJSONType` or `PostgreSQLArrayType`. (Nested types must be `PostgreSQLDataCustomConvertible`.)" |
| 38 | + ] |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +fileprivate struct PostgreSQLRowKeyedDecodingContainer<K>: KeyedDecodingContainerProtocol |
| 43 | + where K: CodingKey |
| 44 | +{ |
| 45 | + var allKeys: [K] |
| 46 | + typealias Key = K |
| 47 | + var codingPath: [CodingKey] |
| 48 | + let decoder: PostgreSQLRowDecoder |
| 49 | + init(decoder: PostgreSQLRowDecoder) { |
| 50 | + self.decoder = decoder |
| 51 | + codingPath = [] |
| 52 | + allKeys = self.decoder.data.keys.flatMap { K(stringValue: $0) } |
| 53 | + } |
| 54 | + func contains(_ key: K) -> Bool { return decoder.data.keys.contains(key.stringValue) } |
| 55 | + func decodeNil(forKey key: K) -> Bool { return decoder.data[key.stringValue]?.data == nil } |
| 56 | + func decode(_ type: Int.Type, forKey key: K) throws -> Int { return try decoder.require(key: key).decode(Int.self) } |
| 57 | + func decode(_ type: Int8.Type, forKey key: K) throws -> Int8 { return try decoder.require(key: key).decode(Int8.self) } |
| 58 | + func decode(_ type: Int16.Type, forKey key: K) throws -> Int16 { return try decoder.require(key: key).decode(Int16.self) } |
| 59 | + func decode(_ type: Int32.Type, forKey key: K) throws -> Int32 { return try decoder.require(key: key).decode(Int32.self) } |
| 60 | + func decode(_ type: Int64.Type, forKey key: K) throws -> Int64 { return try decoder.require(key: key).decode(Int64.self) } |
| 61 | + func decode(_ type: UInt.Type, forKey key: K) throws -> UInt { return try decoder.require(key: key).decode(UInt.self) } |
| 62 | + func decode(_ type: UInt8.Type, forKey key: K) throws -> UInt8 { return try decoder.require(key: key).decode(UInt8.self) } |
| 63 | + func decode(_ type: UInt16.Type, forKey key: K) throws -> UInt16 { return try decoder.require(key: key).decode(UInt16.self) } |
| 64 | + func decode(_ type: UInt32.Type, forKey key: K) throws -> UInt32 { return try decoder.require(key: key).decode(UInt32.self) } |
| 65 | + func decode(_ type: UInt64.Type, forKey key: K) throws -> UInt64 { return try decoder.require(key: key).decode(UInt64.self) } |
| 66 | + func decode(_ type: Double.Type, forKey key: K) throws -> Double { return try decoder.require(key: key).decode(Double.self) } |
| 67 | + func decode(_ type: Float.Type, forKey key: K) throws -> Float { return try decoder.require(key: key).decode(Float.self) } |
| 68 | + func decode(_ type: Bool.Type, forKey key: K) throws -> Bool { return try decoder.require(key: key).decode(Bool.self) } |
| 69 | + func decode(_ type: String.Type, forKey key: K) throws -> String { return try decoder.require(key: key).decode(String.self) } |
| 70 | + func decode<T>(_ type: T.Type, forKey key: K) throws -> T where T: Decodable { |
| 71 | + guard let convertible = type as? PostgreSQLDataCustomConvertible.Type else { |
| 72 | + throw PostgreSQLError( |
| 73 | + identifier: "convertible", |
| 74 | + reason: "Unsupported decodable type: \(type)", |
| 75 | + suggestedFixes: [ |
| 76 | + "Conform \(type) to PostgreSQLDataCustomConvertible" |
| 77 | + ] |
| 78 | + ) |
| 79 | + } |
| 80 | + return try convertible.convertFromPostgreSQLData(decoder.require(key: key)) as! T |
| 81 | + } |
| 82 | + |
| 83 | + func nestedContainer<NestedKey>(keyedBy type: NestedKey.Type, forKey key: K) throws -> KeyedDecodingContainer<NestedKey> where NestedKey : CodingKey { |
| 84 | + return try decoder.container(keyedBy: NestedKey.self) |
| 85 | + } |
| 86 | + |
| 87 | + func nestedUnkeyedContainer(forKey key: K) throws -> UnkeyedDecodingContainer { |
| 88 | + return try decoder.unkeyedContainer() |
| 89 | + } |
| 90 | + |
| 91 | + func superDecoder() throws -> Decoder { return decoder } |
| 92 | + func superDecoder(forKey key: K) throws -> Decoder { return decoder } |
| 93 | + |
| 94 | +} |
0 commit comments