StrictConcurrency: FetchableRecord.databaseDecodingUserInfo must be declared as a computed property

This commit is contained in:
Gwendal Roué committed 2024-09-22 10:10:01 +02:00
1 parent 1be676280f
commit cc0b401f0a
3 files changed
+32 -6

No files matched your search

+20 -1
View File
@@ -129,7 +129,9 @@ public protocol FetchableRecord {
/// // A FetchableRecord + Decodable record
/// struct Player: FetchableRecord, Decodable {
/// // Customize the decoder name when decoding a database row
/// static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [decoderName: "Database"]
/// static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] {
/// [decoderName: "Database"]
/// }
///
/// init(from decoder: Decoder) throws {
/// // Print the decoder name
@@ -146,6 +148,23 @@ public protocol FetchableRecord {
/// decoder.userInfo = [decoderName: "JSON"]
/// let player = try decoder.decode(Player.self, from: ...)
/// ```
///
/// > Important: Make sure the `databaseDecodingUserInfo` property is
/// > explicitly declared as `[CodingUserInfoKey: Any]`. If it is not,
/// > the Swift compiler may silently miss the protocol requirement.
///
/// > Important: Make sure the property is declared as a computed
/// > property (`static var`), instead of a stored property
/// > (`static let`). Computed properties avoid a compiler diagnostic
/// > with stored properties:
/// >
/// > ```swift
/// > // static property 'databaseDecodingUserInfo' is not
/// > // concurrency-safe because non-'Sendable' type
/// > // '[CodingUserInfoKey: Any]' may have shared
/// > // mutable state.
/// > static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [decoderName: "Database"]
/// > ```
static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] { get }
/// Returns the `JSONDecoder` that decodes the value for a given column.
+3 -1
View File
@@ -2797,7 +2797,9 @@ let player = try decoder.decode(Player.self, from: jsonData)
```swift
extension Player: FetchableRecord {
static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [decoderName: "database row"]
static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] {
[decoderName: "database row"]
}
}
// prints "Decoded from database row"
@@ -1361,9 +1361,12 @@ extension FetchableRecordDecodableTests {
context = decoder.userInfo[testKeyRoot] as? String
}
static let databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [
testKeyRoot: "GRDB root",
testKeyNested: "GRDB column or scope"]
static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] {
[
testKeyRoot: "GRDB root",
testKeyNested: "GRDB column or scope",
]
}
static func databaseJSONDecoder(for column: String) -> JSONDecoder {
let decoder = JSONDecoder()
@@ -1666,7 +1669,9 @@ extension FetchableRecordDecodableTests {
struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable {
static let databaseTableName = "t1"
static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] = [CodingUserInfoKey.testKey: "correct"]
static var databaseDecodingUserInfo: [CodingUserInfoKey: Any] {
[CodingUserInfoKey.testKey: "correct"]
}
let nested: NestedStruct?
}