diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index 5d894df30..f89a12a5c 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -108,8 +108,12 @@ private struct _RowDecoder: Decoder { func singleValueContainer() throws -> SingleValueDecodingContainer { guard let key = codingPath.last else { - // Decoding an array of scalars from rows: pick the first column - return ColumnDecoder(row: row, columnIndex: 0, codingPath: codingPath) + // Not yet sure what we are decoding, this will be decided in the SingleValueDecodingContainer functions. + // For decoding an array of scalars (in case of prefetched rows) we pick the first column. + return SingleValueRowDecoder( + columnDecoder: ColumnDecoder(row: row, columnIndex: 0, codingPath: codingPath), + columnDecodingStrategy: columnDecodingStrategy + ) } guard let index = row.index(forColumn: key.stringValue) else { // Don't use DecodingError.keyNotFound: @@ -346,7 +350,7 @@ private struct _RowDecoder: Decoder { // Unknown key // - // Should be throw an error? Well... The use case is the following: + // Should we throw an error? Well... The use case is the following: // // // SELECT book.*, author.* FROM book // // JOIN author ON author.id = book.authorId @@ -487,6 +491,50 @@ private struct _RowDecoder: Decoder { } } +private struct SingleValueRowDecoder: SingleValueDecodingContainer { + var columnDecoder: ColumnDecoder + var columnDecodingStrategy: DatabaseColumnDecodingStrategy + let codingPath: [any CodingKey] = [] + + func decodeNil() -> Bool { columnDecoder.decodeNil() } + func decode(_ type: Bool.Type) throws -> Bool { try columnDecoder.decode(type) } + func decode(_ type: String.Type) throws -> String { try columnDecoder.decode(type) } + func decode(_ type: Double.Type) throws -> Double { try columnDecoder.decode(type) } + func decode(_ type: Float.Type) throws -> Float { try columnDecoder.decode(type) } + func decode(_ type: Int.Type) throws -> Int { try columnDecoder.decode(type) } + func decode(_ type: Int8.Type) throws -> Int8 { try columnDecoder.decode(type) } + func decode(_ type: Int16.Type) throws -> Int16 { try columnDecoder.decode(type) } + func decode(_ type: Int32.Type) throws -> Int32 { try columnDecoder.decode(type) } + func decode(_ type: Int64.Type) throws -> Int64 { try columnDecoder.decode(type) } +#if compiler(>=6) + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) + func decode(_ type: Int128.Type) throws -> Int128 { try columnDecoder.decode(type) } +#endif + func decode(_ type: UInt.Type) throws -> UInt { try columnDecoder.decode(type) } + func decode(_ type: UInt8.Type) throws -> UInt8 { try columnDecoder.decode(type) } + func decode(_ type: UInt16.Type) throws -> UInt16 { try columnDecoder.decode(type) } + func decode(_ type: UInt32.Type) throws -> UInt32 { try columnDecoder.decode(type) } + func decode(_ type: UInt64.Type) throws -> UInt64 { try columnDecoder.decode(type) } +#if compiler(>=6) + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) + func decode(_ type: UInt128.Type) throws -> UInt128 { try columnDecoder.decode(type) } +#endif + + func decode(_ type: T.Type) throws -> T where T: Decodable { + if let type = T.self as? any FetchableRecord.Type { + // Prefer FetchableRecord decoding over Decodable. + return try type.init(row: columnDecoder.row) as! T + } else { + let decoder = _RowDecoder( + row: columnDecoder.row, + codingPath: [], + columnDecodingStrategy: columnDecodingStrategy + ) + return try T(from: decoder) + } + } +} + // MARK: - PrefetchedRowsDecoder private struct PrefetchedRowsDecoder: Decoder { diff --git a/Tests/GRDBTests/FetchableRecordDecodableTests.swift b/Tests/GRDBTests/FetchableRecordDecodableTests.swift index 1f7789a5e..8c7d0c195 100644 --- a/Tests/GRDBTests/FetchableRecordDecodableTests.swift +++ b/Tests/GRDBTests/FetchableRecordDecodableTests.swift @@ -1913,4 +1913,70 @@ extension FetchableRecordDecodableTests { } } } + + // Regression test for + func testSingleValueContainer() throws { + struct Struct: Decodable { + let value: String + } + + struct Wrapper: FetchableRecord, Decodable { + var model: Model + var otherValue: String + + enum CodingKeys: String, CodingKey { + case otherValue + } + + init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + otherValue = try container.decode(String.self, forKey: . otherValue) + + let singleValueContainer = try decoder.singleValueContainer() + model = try singleValueContainer.decode(Model.self) + } + } + + let row = Row(["value": "foo", "otherValue": "bar"]) + + let wrapper = try Wrapper(row: row) + XCTAssertEqual(wrapper.model.value, "foo") + XCTAssertEqual(wrapper.otherValue, "bar") + } + + // Regression test for + // Here we test that `FetchableRecord` takes precedence over `Decodable` + // when a record is encoded with a `SingleValueEncodingContainer`. + func testSingleValueContainerWithFetchableRecord() throws { + struct Struct: Decodable, FetchableRecord { + let value: String + + init(row: Row) throws { + value = row["actualValue"] + } + } + + struct Wrapper: FetchableRecord, Decodable { + var model: Model + var otherValue: String + + enum CodingKeys: String, CodingKey { + case otherValue + } + + init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + otherValue = try container.decode(String.self, forKey: . otherValue) + + let singleValueContainer = try decoder.singleValueContainer() + model = try singleValueContainer.decode(Model.self) + } + } + + let row = Row(["value": "foo", "otherValue": "bar", "actualValue": "test"]) + + let wrapper = try Wrapper(row: row) + XCTAssertEqual(wrapper.model.value, "test") + XCTAssertEqual(wrapper.otherValue, "bar") + } }