[BREAKING] databaseDateDecodingStrategy depends on the column

This commit is contained in:
Gwendal Roué committed 2024-07-30 08:43:28 +02:00
1 parent 8a680a9719
commit 7ac91619cb
7 files changed
+74 -14

No files matched your search

+1
View File
@@ -32,3 +32,4 @@ Strategies
- databaseDateEncodingStrategy(for:)
- databaseUUIDEncodingStrategy(for:)
- databaseDataDecodingStrategy(for:)
- databaseDateDecodingStrategy(for:)
+11 -6
View File
@@ -62,7 +62,7 @@ extension FetchableRecord where Self: Decodable {
///
/// - ``FetchableRecord/databaseColumnDecodingStrategy-6uefz``
/// - ``FetchableRecord/databaseDataDecodingStrategy(for:)``
/// - ``FetchableRecord/databaseDateDecodingStrategy-78y03``
/// - ``FetchableRecord/databaseDateDecodingStrategy(for:)``
/// - ``FetchableRecord/databaseDecodingUserInfo-77jim``
/// - ``FetchableRecord/databaseJSONDecoder(for:)-7lmxd``
public class FetchableRecordDecoder {
@@ -289,9 +289,9 @@ private struct _RowDecoder<R: FetchableRecord>: Decoder {
.databaseDataDecodingStrategy(for: column)
.decodeIfPresent(fromRow: row, atUncheckedIndex: index) as! T?
} else if type == Date.self {
return try R.databaseDateDecodingStrategy.decodeIfPresent(
fromRow: row,
atUncheckedIndex: index) as! T?
return try R
.databaseDateDecodingStrategy(for: column)
.decodeIfPresent(fromRow: row, atUncheckedIndex: index) as! T?
} else if let type = T.self as? any (DatabaseValueConvertible & StatementColumnConvertible).Type {
return try type.fastDecodeIfPresent(fromRow: row, atUncheckedIndex: index) as! T?
} else if let type = T.self as? any DatabaseValueConvertible.Type {
@@ -338,7 +338,9 @@ private struct _RowDecoder<R: FetchableRecord>: Decoder {
.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
return try R
.databaseDateDecodingStrategy(for: column)
.decode(fromRow: row, atUncheckedIndex: index) as! T
} else if let type = T.self as? any (DatabaseValueConvertible & StatementColumnConvertible).Type {
return try type.fastDecode(fromRow: row, atUncheckedIndex: index) as! T
} else if let type = T.self as? any DatabaseValueConvertible.Type {
@@ -660,7 +662,10 @@ extension ColumnDecoder: SingleValueDecodingContainer {
.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
let columnName = row.impl.columnName(atUncheckedIndex: columnIndex)
return try R
.databaseDateDecodingStrategy(for: columnName)
.decode(fromRow: row, atUncheckedIndex: columnIndex) as! T
} else if let type = T.self as? any (DatabaseValueConvertible & StatementColumnConvertible).Type {
return try type.fastDecode(fromRow: row, atUncheckedIndex: columnIndex) as! T
} else if let type = T.self as? any DatabaseValueConvertible.Type {
+9 -5
View File
@@ -90,7 +90,7 @@ import Foundation
///
/// - ``databaseColumnDecodingStrategy-6uefz``
/// - ``databaseDataDecodingStrategy(for:)``
/// - ``databaseDateDecodingStrategy-78y03``
/// - ``databaseDateDecodingStrategy(for:)``
/// - ``databaseJSONDecoder(for:)-7lmxd``
/// - ``databaseDecodingUserInfo-77jim``
/// - ``DatabaseColumnDecodingStrategy``
@@ -209,13 +209,15 @@ public protocol FetchableRecord {
///
/// ```swift
/// struct Player: FetchableRecord, Decodable {
/// static let databaseDateDecodingStrategy = DatabaseDateDecodingStrategy.timeIntervalSince1970
/// static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy {
/// .timeIntervalSince1970
/// }
///
/// // Decoded from an epoch timestamp
/// var creationDate: Date
/// }
/// ```
static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { get }
static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy
/// The strategy for converting column names to coding keys.
///
@@ -270,7 +272,7 @@ extension FetchableRecord {
/// The default strategy for decoding `Date` columns is
/// ``DatabaseDateDecodingStrategy/deferredToDate``.
public static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy {
public static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy {
.deferredToDate
}
@@ -916,7 +918,9 @@ public enum DatabaseDataDecodingStrategy: Sendable {
/// For example:
///
/// struct Player: FetchableRecord, Decodable {
/// static let databaseDateDecodingStrategy = DatabaseDateDecodingStrategy.timeIntervalSince1970
/// static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy {
/// .timeIntervalSince1970
/// }
///
/// var name: String
/// var registrationDate: Date // decoded from epoch timestamp
+1 -1
View File
@@ -2681,7 +2681,7 @@ Those behaviors can be overridden:
```swift
protocol FetchableRecord {
static func databaseDataDecodingStrategy(for column: String) -> DatabaseDataDecodingStrategy
static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { get }
static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy
}
protocol EncodableRecord {
+1
View File
@@ -95,6 +95,7 @@
- [X] databaseDateEncodingStrategy
- [X] databaseUUIDEncodingStrategy
- [X] databaseDataDecodingStrategy
- [X] databaseDateDecodingStrategy
- [ ] GRDB7 JSONB https://hachyderm.io/@jason_abbott/111908186742242971#.
- [X] GRDB7: stop exporting CSQLite https://github.com/groue/GRDB.swift/issues/642#issuecomment-1937347696
- [ ] GRDB7: AsyncValueObservation: experiment with an immediate scheduler that does not hop to the main queue.
@@ -47,12 +47,18 @@ private enum StrategyCustom: StrategyProvider {
}
private struct RecordWithDate<Strategy: StrategyProvider>: FetchableRecord, Decodable {
static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { Strategy.strategy }
static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy {
Strategy.strategy
}
var date: Date
}
private struct RecordWithOptionalDate<Strategy: StrategyProvider>: FetchableRecord, Decodable {
static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { Strategy.strategy }
static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy {
Strategy.strategy
}
var date: Date?
}
@@ -164,6 +164,49 @@ extension FetchableRecordDecodableTests {
}
}
func testSingleValueDateProperty() throws {
struct Value : Decodable {
let date: Date
init(from decoder: Decoder) throws {
date = try decoder.singleValueContainer().decode(Date.self)
}
}
struct Struct : FetchableRecord, Decodable {
static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy {
if column == "value" {
return .custom { _ in Date(timeIntervalSince1970: 0) }
} else {
return .deferredToDate
}
}
let value: Value
let optionalValue: Value?
}
do {
// No null values
let s = try Struct(row: ["value": "foo", "optionalValue": "2001-01-01 00:00:00"])
XCTAssertEqual(s.value.date, Date(timeIntervalSince1970: 0))
XCTAssertEqual(s.optionalValue?.date, Date(timeIntervalSinceReferenceDate: 0))
}
do {
// Null values
let s = try Struct(row: ["value": "foo", "optionalValue": nil])
XCTAssertEqual(s.value.date, Date(timeIntervalSince1970: 0))
XCTAssertNil(s.optionalValue)
}
do {
// Missing and extra values
let s = try Struct(row: ["value": "foo", "ignored": "?"])
XCTAssertEqual(s.value.date, Date(timeIntervalSince1970: 0))
XCTAssertNil(s.optionalValue)
}
}
func testNonTrivialSingleValueDecodableProperty() throws {
struct NestedValue : Decodable {
let string: String