mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
[BREAKING] databaseDataDecodingStrategy depends on the column
This commit is contained in:
1 parent
bcd6e03d7a
commit
95ca9f5bf3
5 files changed
+79
-22
No files matched your search
@@ -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<R: FetchableRecord>: 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<R: FetchableRecord>: 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<T>(_ 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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
|
||||
@@ -20,12 +20,18 @@ private enum StrategyCustom: StrategyProvider {
|
||||
}
|
||||
|
||||
private struct RecordWithData<Strategy: StrategyProvider>: FetchableRecord, Decodable {
|
||||
static var databaseDataDecodingStrategy: DatabaseDataDecodingStrategy { Strategy.strategy }
|
||||
static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy {
|
||||
Strategy.strategy
|
||||
}
|
||||
|
||||
var data: Data
|
||||
}
|
||||
|
||||
private struct RecordWithOptionalData<Strategy: StrategyProvider>: FetchableRecord, Decodable {
|
||||
static var databaseDataDecodingStrategy: DatabaseDataDecodingStrategy { Strategy.strategy }
|
||||
static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy {
|
||||
Strategy.strategy
|
||||
}
|
||||
|
||||
var data: Data?
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user