diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index 9fbf0cfab..cf2f25a92 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -61,7 +61,7 @@ extension FetchableRecord where Self: Decodable { /// The behavior of the decoder depends on the decoded type. See: /// /// - ``FetchableRecord/databaseColumnDecodingStrategy-6uefz`` -/// - ``FetchableRecord/databaseDataDecodingStrategy-71bh1`` +/// - ``FetchableRecord/databaseDataDecodingStrategy(for:)`` /// - ``FetchableRecord/databaseDateDecodingStrategy-78y03`` /// - ``FetchableRecord/databaseDecodingUserInfo-77jim`` /// - ``FetchableRecord/databaseJSONDecoder(for:)-7lmxd`` @@ -285,9 +285,9 @@ private struct _RowDecoder: Decoder { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. if type == Data.self { - return try R.databaseDataDecodingStrategy.decodeIfPresent( - fromRow: row, - atUncheckedIndex: index) as! T? + return try R + .databaseDataDecodingStrategy(for: column) + .decodeIfPresent(fromRow: row, atUncheckedIndex: index) as! T? } else if type == Date.self { return try R.databaseDateDecodingStrategy.decodeIfPresent( fromRow: row, @@ -334,7 +334,9 @@ private struct _RowDecoder: Decoder { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. if type == Data.self { - return try R.databaseDataDecodingStrategy.decode(fromRow: row, atUncheckedIndex: index) as! T + return try R + .databaseDataDecodingStrategy(for: column) + .decode(fromRow: row, atUncheckedIndex: index) as! T } else if type == Date.self { return try R.databaseDateDecodingStrategy.decode(fromRow: row, atUncheckedIndex: index) as! T } else if let type = T.self as? any (DatabaseValueConvertible & StatementColumnConvertible).Type { @@ -652,9 +654,11 @@ extension ColumnDecoder: SingleValueDecodingContainer { func decode(_ type: String.Type) throws -> String { try row.decode(atIndex: columnIndex) } func decode(_ type: T.Type) throws -> T where T: Decodable { - // TODO: not tested if type == Data.self { - return try R.databaseDataDecodingStrategy.decode(fromRow: row, atUncheckedIndex: columnIndex) as! T + let columnName = row.impl.columnName(atUncheckedIndex: columnIndex) + return try R + .databaseDataDecodingStrategy(for: columnName) + .decode(fromRow: row, atUncheckedIndex: columnIndex) as! T } else if type == Date.self { return try R.databaseDateDecodingStrategy.decode(fromRow: row, atUncheckedIndex: columnIndex) as! T } else if let type = T.self as? any (DatabaseValueConvertible & StatementColumnConvertible).Type { diff --git a/GRDB/Record/FetchableRecord.swift b/GRDB/Record/FetchableRecord.swift index 90dd6669c..f52782a3b 100644 --- a/GRDB/Record/FetchableRecord.swift +++ b/GRDB/Record/FetchableRecord.swift @@ -89,10 +89,10 @@ import Foundation /// ### Configuring Row Decoding for the Standard Decodable Protocol /// /// - ``databaseColumnDecodingStrategy-6uefz`` -/// - ``databaseDataDecodingStrategy-71bh1`` +/// - ``databaseDataDecodingStrategy(for:)`` /// - ``databaseDateDecodingStrategy-78y03`` -/// - ``databaseDecodingUserInfo-77jim`` /// - ``databaseJSONDecoder(for:)-7lmxd`` +/// - ``databaseDecodingUserInfo-77jim`` /// - ``DatabaseColumnDecodingStrategy`` /// - ``DatabaseDataDecodingStrategy`` /// - ``DatabaseDateDecodingStrategy`` @@ -165,18 +165,20 @@ public protocol FetchableRecord { /// /// ```swift /// struct Player: FetchableRecord, Decodable { - /// static let databaseDataDecodingStrategy = DatabaseDataDecodingStrategy.custom { dbValue - /// guard let base64Data = Data.fromDatabaseValue(dbValue) else { - /// return nil + /// static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy { + /// .custom { dbValue + /// guard let base64Data = Data.fromDatabaseValue(dbValue) else { + /// return nil + /// } + /// return Data(base64Encoded: base64Data) /// } - /// return Data(base64Encoded: base64Data) /// } /// /// // Decoded from both database base64 strings and blobs /// var myData: Data /// } /// ``` - static var databaseDataDecodingStrategy: DatabaseDataDecodingStrategy { get } + static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy /// The strategy for decoding `Date` columns. /// @@ -243,7 +245,7 @@ extension FetchableRecord { /// The default strategy for decoding `Data` columns is /// ``DatabaseDataDecodingStrategy/deferredToData``. - public static var databaseDataDecodingStrategy: DatabaseDataDecodingStrategy { + public static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy { .deferredToData } @@ -865,11 +867,13 @@ extension RecordCursor: Sendable { } /// /// ```swift /// struct Player: FetchableRecord, Decodable { -/// static let databaseDataDecodingStrategy = DatabaseDataDecodingStrategy.custom { dbValue -/// guard let base64Data = Data.fromDatabaseValue(dbValue) else { -/// return nil +/// static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy { +/// .custom { dbValue +/// guard let base64Data = Data.fromDatabaseValue(dbValue) else { +/// return nil +/// } +/// return Data(base64Encoded: base64Data) /// } -/// return Data(base64Encoded: base64Data) /// } /// /// // Decoded from both database base64 strings and blobs diff --git a/README.md b/README.md index 189935b4f..b2f7791df 100644 --- a/README.md +++ b/README.md @@ -2700,7 +2700,7 @@ Those behaviors can be overridden: ```swift protocol FetchableRecord { - static var databaseDataDecodingStrategy: DatabaseDataDecodingStrategy { get } + static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { get } } diff --git a/Tests/GRDBTests/DatabaseDataDecodingStrategyTests.swift b/Tests/GRDBTests/DatabaseDataDecodingStrategyTests.swift index 8ff1da13b..a11493297 100644 --- a/Tests/GRDBTests/DatabaseDataDecodingStrategyTests.swift +++ b/Tests/GRDBTests/DatabaseDataDecodingStrategyTests.swift @@ -20,12 +20,18 @@ private enum StrategyCustom: StrategyProvider { } private struct RecordWithData: FetchableRecord, Decodable { - static var databaseDataDecodingStrategy: DatabaseDataDecodingStrategy { Strategy.strategy } + static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy { + Strategy.strategy + } + var data: Data } private struct RecordWithOptionalData: FetchableRecord, Decodable { - static var databaseDataDecodingStrategy: DatabaseDataDecodingStrategy { Strategy.strategy } + static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy { + Strategy.strategy + } + var data: Data? } diff --git a/Tests/GRDBTests/FetchableRecordDecodableTests.swift b/Tests/GRDBTests/FetchableRecordDecodableTests.swift index 8c7d0c195..59f6054fb 100644 --- a/Tests/GRDBTests/FetchableRecordDecodableTests.swift +++ b/Tests/GRDBTests/FetchableRecordDecodableTests.swift @@ -121,6 +121,49 @@ extension FetchableRecordDecodableTests { } } + func testSingleValueDataProperty() throws { + struct Value : Decodable { + let data: Data + + init(from decoder: Decoder) throws { + data = try decoder.singleValueContainer().decode(Data.self) + } + } + + struct Struct : FetchableRecord, Decodable { + static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy { + if column == "value" { + return .custom { _ in Data([1, 2, 3]) } + } else { + return .deferredToData + } + } + let value: Value + let optionalValue: Value? + } + + do { + // No null values + let s = try Struct(row: ["value": "foo", "optionalValue": "bar"]) + XCTAssertEqual(s.value.data, Data([1, 2, 3])) + XCTAssertEqual(s.optionalValue?.data, Data([98, 97, 114])) + } + + do { + // Null values + let s = try Struct(row: ["value": "foo", "optionalValue": nil]) + XCTAssertEqual(s.value.data, Data([1, 2, 3])) + XCTAssertNil(s.optionalValue) + } + + do { + // Missing and extra values + let s = try Struct(row: ["value": "foo", "ignored": "?"]) + XCTAssertEqual(s.value.data, Data([1, 2, 3])) + XCTAssertNil(s.optionalValue) + } + } + func testNonTrivialSingleValueDecodableProperty() throws { struct NestedValue : Decodable { let string: String