databaseEncodingUserInfo
Contextual information made available to the Encodable.encode(to:)
method.
static var databaseEncodingUserInfo: [CodingUserInfoKey : Any] { get }
This property is dedicated to EncodableRecord
types that also conform to the standard Encodable
protocol and use the default encode(to:)
implementation.
The returned dictionary is returned by Encoder.userInfo
when the record is encoded.
For example:
// A key that holds a encoder's name
let encoderName = CodingUserInfoKey(rawValue: "encoderName")!
struct Player: PersistableRecord, Encodable {
// Customize the encoder name when encoding a database row
static var databaseEncodingUserInfo: [CodingUserInfoKey: Any] {
[encoderName: "Database"]
}
func encode(to encoder: Encoder) throws {
// Print the encoder name
print(encoder.userInfo[encoderName])
...
}
}
let player = Player(...)
// prints "Database"
try player.insert(db)
// prints "JSON"
let encoder = JSONEncoder()
encoder.userInfo = [encoderName: "JSON"]
let data = try encoder.encode(player)