|
| 1 | +/// Internal `UnkeyedDecodingContainer` for `PostgreSQLDataDecoder` |
| 2 | +final class PostgreSQLDataUnkeyedDecodingContainer: UnkeyedDecodingContainer { |
| 3 | + /// See `UnkeyedDecodingContainer.count` |
| 4 | + var count: Int? |
| 5 | + |
| 6 | + /// See `UnkeyedDecodingContainer.isAtEnd` |
| 7 | + var isAtEnd: Bool { |
| 8 | + return currentIndex == count |
| 9 | + } |
| 10 | + |
| 11 | + /// See `UnkeyedDecodingContainer.currentIndex` |
| 12 | + var currentIndex: Int |
| 13 | + |
| 14 | + /// Creates a coding key for the current index, then increments the count. |
| 15 | + var index: CodingKey { |
| 16 | + defer { currentIndex += 1} |
| 17 | + return PostgreSQLDataArrayKey(currentIndex) |
| 18 | + } |
| 19 | + |
| 20 | + /// See `UnkeyedDecodingContainer.codingPath` |
| 21 | + var codingPath: [CodingKey] |
| 22 | + |
| 23 | + /// Data being encoded. |
| 24 | + let partialData: PartialPostgreSQLData |
| 25 | + |
| 26 | + /// Creates a new internal `PostgreSQLDataUnkeyedDecodingContainer`. |
| 27 | + init(partialData: PartialPostgreSQLData, at path: [CodingKey]) { |
| 28 | + self.codingPath = path |
| 29 | + self.partialData = partialData |
| 30 | + switch partialData.get(at: codingPath) { |
| 31 | + case .some(let w): |
| 32 | + switch w { |
| 33 | + case .array(let a): count = a.count |
| 34 | + default: count = nil |
| 35 | + } |
| 36 | + case .none: count = nil |
| 37 | + } |
| 38 | + currentIndex = 0 |
| 39 | + } |
| 40 | + |
| 41 | + /// See `UnkeyedDecodingContainer.decodeNil` |
| 42 | + func decodeNil() throws -> Bool { |
| 43 | + return partialData.get(at: codingPath + [index]) == nil |
| 44 | + } |
| 45 | + |
| 46 | + /// See `UnkeyedDecodingContainer.decode` |
| 47 | + func decode(_ type: Bool.Type) throws -> Bool { |
| 48 | + return try partialData.requireBool(at: codingPath + [index]) |
| 49 | + } |
| 50 | + |
| 51 | + /// See `UnkeyedDecodingContainer.decode` |
| 52 | + func decode(_ type: Int.Type) throws -> Int { |
| 53 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 54 | + } |
| 55 | + |
| 56 | + /// See `UnkeyedDecodingContainer.decode` |
| 57 | + func decode(_ type: Int8.Type) throws -> Int8 { |
| 58 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 59 | + } |
| 60 | + |
| 61 | + /// See `UnkeyedDecodingContainer.decode` |
| 62 | + func decode(_ type: Int16.Type) throws -> Int16 { |
| 63 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 64 | + } |
| 65 | + |
| 66 | + /// See `UnkeyedDecodingContainer.decode` |
| 67 | + func decode(_ type: Int32.Type) throws -> Int32 { |
| 68 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 69 | + } |
| 70 | + |
| 71 | + /// See `UnkeyedDecodingContainer.decode` |
| 72 | + func decode(_ type: Int64.Type) throws -> Int64 { |
| 73 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 74 | + } |
| 75 | + |
| 76 | + /// See `UnkeyedDecodingContainer.decode` |
| 77 | + func decode(_ type: UInt.Type) throws -> UInt { |
| 78 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 79 | + } |
| 80 | + |
| 81 | + /// See `UnkeyedDecodingContainer.decode` |
| 82 | + func decode(_ type: UInt8.Type) throws -> UInt8 { |
| 83 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 84 | + } |
| 85 | + |
| 86 | + /// See `UnkeyedDecodingContainer.decode` |
| 87 | + func decode(_ type: UInt16.Type) throws -> UInt16 { |
| 88 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 89 | + } |
| 90 | + |
| 91 | + /// See `UnkeyedDecodingContainer.decode` |
| 92 | + func decode(_ type: UInt32.Type) throws -> UInt32 { |
| 93 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 94 | + } |
| 95 | + |
| 96 | + /// See `UnkeyedDecodingContainer.decode` |
| 97 | + func decode(_ type: UInt64.Type) throws -> UInt64 { |
| 98 | + return try partialData.requireFixedWidthItenger(at: codingPath + [index]) |
| 99 | + } |
| 100 | + |
| 101 | + /// See `UnkeyedDecodingContainer.decode` |
| 102 | + func decode(_ type: Float.Type) throws -> Float { |
| 103 | + return try partialData.requireFloatingPoint(at: codingPath + [index]) |
| 104 | + } |
| 105 | + |
| 106 | + /// See `UnkeyedDecodingContainer.decode` |
| 107 | + func decode(_ type: Double.Type) throws -> Double { |
| 108 | + return try partialData.requireFloatingPoint(at: codingPath + [index]) |
| 109 | + } |
| 110 | + |
| 111 | + /// See `UnkeyedDecodingContainer.decode` |
| 112 | + func decode(_ type: String.Type) throws -> String { |
| 113 | + return try partialData.requireString(at: codingPath + [index]) |
| 114 | + } |
| 115 | + |
| 116 | + /// See `UnkeyedDecodingContainer.decode` |
| 117 | + func decode<T>(_ type: T.Type) throws -> T where T : Decodable { |
| 118 | + let decoder = _PostgreSQLDataDecoder(partialData: partialData, at: codingPath + [index]) |
| 119 | + return try T(from: decoder) |
| 120 | + } |
| 121 | + |
| 122 | + /// See `UnkeyedDecodingContainer.nestedContainer` |
| 123 | + func nestedContainer<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> |
| 124 | + where Key: CodingKey |
| 125 | + { |
| 126 | + let container = PostgreSQLDataKeyedDecodingContainer<Key>(partialData: partialData, at: codingPath + [index]) |
| 127 | + return .init(container) |
| 128 | + } |
| 129 | + |
| 130 | + /// See `UnkeyedDecodingContainer.nestedUnkeyedContainer` |
| 131 | + func nestedUnkeyedContainer() throws -> UnkeyedDecodingContainer { |
| 132 | + return PostgreSQLDataUnkeyedDecodingContainer(partialData: partialData, at: codingPath + [index]) |
| 133 | + } |
| 134 | + |
| 135 | + /// See `UnkeyedDecodingContainer.superDecoder` |
| 136 | + func superDecoder() throws -> Decoder { |
| 137 | + return _PostgreSQLDataDecoder(partialData: partialData, at: codingPath + [index]) |
| 138 | + } |
| 139 | +} |
0 commit comments