diff --git a/Documentation/GRDB7MigrationGuide.md b/Documentation/GRDB7MigrationGuide.md index fbfd12c6a..d6ec51736 100644 --- a/Documentation/GRDB7MigrationGuide.md +++ b/Documentation/GRDB7MigrationGuide.md @@ -32,3 +32,4 @@ Strategies - databaseDateEncodingStrategy(for:) - databaseUUIDEncodingStrategy(for:) - databaseDataDecodingStrategy(for:) +- databaseDateDecodingStrategy(for:) diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index eb3b71679..a48ce71fb 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -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: 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: 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 { diff --git a/GRDB/Record/FetchableRecord.swift b/GRDB/Record/FetchableRecord.swift index ebfa42867..8956d09f6 100644 --- a/GRDB/Record/FetchableRecord.swift +++ b/GRDB/Record/FetchableRecord.swift @@ -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 diff --git a/README.md b/README.md index 07dcd4a64..e31b2f94c 100644 --- a/README.md +++ b/README.md @@ -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 { diff --git a/TODO.md b/TODO.md index 1df7853c8..413800b42 100644 --- a/TODO.md +++ b/TODO.md @@ -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. diff --git a/Tests/GRDBTests/DatabaseDateDecodingStrategyTests.swift b/Tests/GRDBTests/DatabaseDateDecodingStrategyTests.swift index 50bbb4500..602368259 100644 --- a/Tests/GRDBTests/DatabaseDateDecodingStrategyTests.swift +++ b/Tests/GRDBTests/DatabaseDateDecodingStrategyTests.swift @@ -47,12 +47,18 @@ private enum StrategyCustom: StrategyProvider { } private struct RecordWithDate: FetchableRecord, Decodable { - static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { Strategy.strategy } + static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy { + Strategy.strategy + } + var date: Date } private struct RecordWithOptionalDate: FetchableRecord, Decodable { - static var databaseDateDecodingStrategy: DatabaseDateDecodingStrategy { Strategy.strategy } + static func databaseDateDecodingStrategy(for column: String) -> DatabaseDateDecodingStrategy { + Strategy.strategy + } + var date: Date? } diff --git a/Tests/GRDBTests/FetchableRecordDecodableTests.swift b/Tests/GRDBTests/FetchableRecordDecodableTests.swift index bca86742b..1cbba07d8 100644 --- a/Tests/GRDBTests/FetchableRecordDecodableTests.swift +++ b/Tests/GRDBTests/FetchableRecordDecodableTests.swift @@ -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