|
| 1 | +#if compiler(>=5.9) |
| 2 | +#if hasFeature(VariadicGenerics) |
| 3 | +extension PostgresRow { |
| 4 | + |
| 5 | + // --- snip TODO: Remove once bug is fixed, that disallows tuples of one |
| 6 | + @inlinable |
| 7 | + public func decode<Column: PostgresDecodable>( |
| 8 | + _: Column.Type, |
| 9 | + file: String = #fileID, |
| 10 | + line: Int = #line |
| 11 | + ) throws -> (Column) { |
| 12 | + try self.decode(Column.self, context: .default, file: file, line: line) |
| 13 | + } |
| 14 | + |
| 15 | + @inlinable |
| 16 | + public func decode<Column: PostgresDecodable>( |
| 17 | + _: Column.Type, |
| 18 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 19 | + file: String = #fileID, |
| 20 | + line: Int = #line |
| 21 | + ) throws -> (Column) { |
| 22 | + precondition(self.columns.count >= 1) |
| 23 | + let columnIndex = 0 |
| 24 | + var cellIterator = self.data.makeIterator() |
| 25 | + var cellData = cellIterator.next().unsafelyUnwrapped |
| 26 | + var columnIterator = self.columns.makeIterator() |
| 27 | + let column = columnIterator.next().unsafelyUnwrapped |
| 28 | + let swiftTargetType: Any.Type = Column.self |
| 29 | + |
| 30 | + do { |
| 31 | + let r0 = try Column._decodeRaw(from: &cellData, type: column.dataType, format: column.format, context: context) |
| 32 | + |
| 33 | + return (r0) |
| 34 | + } catch let code as PostgresDecodingError.Code { |
| 35 | + throw PostgresDecodingError( |
| 36 | + code: code, |
| 37 | + columnName: column.name, |
| 38 | + columnIndex: columnIndex, |
| 39 | + targetType: swiftTargetType, |
| 40 | + postgresType: column.dataType, |
| 41 | + postgresFormat: column.format, |
| 42 | + postgresData: cellData, |
| 43 | + file: file, |
| 44 | + line: line |
| 45 | + ) |
| 46 | + } |
| 47 | + } |
| 48 | + // --- snap TODO: Remove once bug is fixed, that disallows tuples of one |
| 49 | + |
| 50 | +// @inlinable // <--- commenting this in, crashes the compiler |
| 51 | + public func decode<each Column: PostgresDecodable>( |
| 52 | + _ columnType: (repeat each Column).Type, |
| 53 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 54 | + file: String = #fileID, |
| 55 | + line: Int = #line |
| 56 | + ) throws -> (repeat each Column) { |
| 57 | + var columnIndex = 0 |
| 58 | + var cellIterator = self.data.makeIterator() |
| 59 | + var columnIterator = self.columns.makeIterator() |
| 60 | + |
| 61 | + return ( |
| 62 | + repeat try Self.decodeNextColumn( |
| 63 | + (each Column).self, |
| 64 | + cellIterator: &cellIterator, |
| 65 | + columnIterator: &columnIterator, |
| 66 | + columnIndex: &columnIndex, |
| 67 | + context: context, |
| 68 | + file: file, |
| 69 | + line: line |
| 70 | + ) |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + @inlinable |
| 75 | + static func decodeNextColumn<Column: PostgresDecodable>( |
| 76 | + _ columnType: Column.Type, |
| 77 | + cellIterator: inout IndexingIterator<DataRow>, |
| 78 | + columnIterator: inout IndexingIterator<[RowDescription.Column]>, |
| 79 | + columnIndex: inout Int, |
| 80 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 81 | + file: String, |
| 82 | + line: Int |
| 83 | + ) throws -> Column { |
| 84 | + defer { columnIndex += 1 } |
| 85 | + |
| 86 | + let column = columnIterator.next().unsafelyUnwrapped |
| 87 | + var cellData = cellIterator.next().unsafelyUnwrapped |
| 88 | + do { |
| 89 | + return try Column._decodeRaw(from: &cellData, type: column.dataType, format: column.format, context: context) |
| 90 | + } catch let code as PostgresDecodingError.Code { |
| 91 | + throw PostgresDecodingError( |
| 92 | + code: code, |
| 93 | + columnName: column.name, |
| 94 | + columnIndex: columnIndex, |
| 95 | + targetType: Column.self, |
| 96 | + postgresType: column.dataType, |
| 97 | + postgresFormat: column.format, |
| 98 | + postgresData: cellData, |
| 99 | + file: file, |
| 100 | + line: line |
| 101 | + ) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +// @inlinable // <--- commenting this in, crashes the compiler |
| 106 | + public func decode<each Column: PostgresDecodable>( |
| 107 | + _ columnType: (repeat each Column).Type, |
| 108 | + file: String = #fileID, |
| 109 | + line: Int = #line |
| 110 | + ) throws -> (repeat each Column) { |
| 111 | + try self.decode(columnType, context: .default, file: file, line: line) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +extension AsyncSequence where Element == PostgresRow { |
| 116 | + @inlinable |
| 117 | + public func decode<Column: PostgresDecodable>( |
| 118 | + _: Column.Type, |
| 119 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 120 | + file: String = #fileID, |
| 121 | + line: Int = #line |
| 122 | + ) -> AsyncThrowingMapSequence<Self, (Column)> { |
| 123 | + self.map { row in |
| 124 | + try row.decode(Column.self, context: context, file: file, line: line) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @inlinable |
| 129 | + public func decode<Column: PostgresDecodable>( |
| 130 | + _: Column.Type, |
| 131 | + file: String = #fileID, |
| 132 | + line: Int = #line |
| 133 | + ) -> AsyncThrowingMapSequence<Self, (Column)> { |
| 134 | + self.decode(Column.self, context: .default, file: file, line: line) |
| 135 | + } |
| 136 | + |
| 137 | + #if false // commented out since, the AsyncSequence map currently crashes the compiler |
| 138 | + public func decode<each Column: PostgresDecodable>( |
| 139 | + _ columnType: (repeat each Column).Type, |
| 140 | + context: PostgresDecodingContext<some PostgresJSONDecoder>, |
| 141 | + file: String = #fileID, |
| 142 | + line: Int = #line |
| 143 | + ) throws -> AsyncThrowingMapSequence<Self, (repeat each Column)> { |
| 144 | + self.map { row in |
| 145 | + try row.decode(columnType, context: context, file: file, line: line) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + public func decode<each Column: PostgresDecodable>( |
| 150 | + _ columnType: (repeat each Column).Type, |
| 151 | + file: String = #fileID, |
| 152 | + line: Int = #line |
| 153 | + ) throws -> AsyncThrowingMapSequence<Self, (repeat each Column)> { |
| 154 | + try self.decode(columnType, context: .default, file: file, line: line) |
| 155 | + } |
| 156 | + #endif // AsyncSequence extension |
| 157 | +} |
| 158 | + |
| 159 | +#endif // hasFeature |
| 160 | +#endif // compiler(>=5.9) |
0 commit comments