|
| 1 | +internal struct PostgreSQLDataDecoder: SingleValueDecodingContainer { |
| 2 | + var codingPath: [CodingKey] |
| 3 | + var userInfo: [CodingUserInfoKey: Any] |
| 4 | + let data: PostgreSQLData |
| 5 | + init(data: PostgreSQLData) { |
| 6 | + self.data = data |
| 7 | + self.userInfo = [:] |
| 8 | + self.codingPath = [] |
| 9 | + } |
| 10 | + |
| 11 | + func decodeNil() -> Bool { return data.data == nil } |
| 12 | + func decode(_ type: Int.Type) throws -> Int { return try data.decode(Int.self) } |
| 13 | + func decode(_ type: Int8.Type) throws -> Int8 { return try data.decode(Int8.self) } |
| 14 | + func decode(_ type: Int16.Type) throws -> Int16 { return try data.decode(Int16.self) } |
| 15 | + func decode(_ type: Int32.Type) throws -> Int32 { return try data.decode(Int32.self) } |
| 16 | + func decode(_ type: Int64.Type) throws -> Int64 { return try data.decode(Int64.self) } |
| 17 | + func decode(_ type: UInt.Type) throws -> UInt { return try data.decode(UInt.self) } |
| 18 | + func decode(_ type: UInt8.Type) throws -> UInt8 { return try data.decode(UInt8.self) } |
| 19 | + func decode(_ type: UInt16.Type) throws -> UInt16 { return try data.decode(UInt16.self) } |
| 20 | + func decode(_ type: UInt32.Type) throws -> UInt32 { return try data.decode(UInt32.self) } |
| 21 | + func decode(_ type: UInt64.Type) throws -> UInt64 { return try data.decode(UInt64.self) } |
| 22 | + func decode(_ type: Double.Type) throws -> Double { return try data.decode(Double.self) } |
| 23 | + func decode(_ type: Float.Type) throws -> Float { return try data.decode(Float.self) } |
| 24 | + func decode(_ type: Bool.Type) throws -> Bool { return try data.decode(Bool.self) } |
| 25 | + func decode(_ type: String.Type) throws -> String { return try data.decode(String.self) } |
| 26 | + func decode<T>(_ type: T.Type) throws -> T where T: Decodable { |
| 27 | + guard let convertible = type as? PostgreSQLDataCustomConvertible.Type else { |
| 28 | + throw PostgreSQLError( |
| 29 | + identifier: "convertible", |
| 30 | + reason: "Unsupported decodable type: \(type)", |
| 31 | + suggestedFixes: [ |
| 32 | + "Conform \(type) to PostgreSQLDataCustomConvertible" |
| 33 | + ] |
| 34 | + ) |
| 35 | + } |
| 36 | + return try convertible.convertFromPostgreSQLData(data) as! T |
| 37 | + } |
| 38 | +} |
0 commit comments