mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Records that adopt Encodable encode null columns
This commit is contained in:
1 parent
9037d51bc7
commit
b90f876b5b
1 file changed
+21
-2
@@ -28,7 +28,6 @@ private struct PersistableKeyedEncodingContainer<Key: CodingKey> : KeyedEncoding
|
||||
mutating func encode(_ value: Float, forKey key: Key) throws { encode(value, key.stringValue) }
|
||||
mutating func encode(_ value: Double, forKey key: Key) throws { encode(value, key.stringValue) }
|
||||
mutating func encode(_ value: String, forKey key: Key) throws { encode(value, key.stringValue) }
|
||||
mutating func encodeNil(forKey key: Key) throws { encode(nil, key.stringValue) }
|
||||
|
||||
/// Encodes the given value for the given key.
|
||||
///
|
||||
@@ -45,6 +44,24 @@ private struct PersistableKeyedEncodingContainer<Key: CodingKey> : KeyedEncoding
|
||||
}
|
||||
}
|
||||
|
||||
// Provide explicit encoding of optionals, because default implementation does not encode nil values.
|
||||
mutating func encodeNil(forKey key: Key) throws { encode(nil, key.stringValue) }
|
||||
mutating func encodeIfPresent(_ value: Bool?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: Int?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: Int8?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: Int16?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: Int32?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: Int64?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: UInt?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: UInt8?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: Float?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: Double?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent(_ value: String?, forKey key: Key) throws { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
mutating func encodeIfPresent<T>(_ value: T?, forKey key: Key) throws where T : Encodable { if let value = value { try encode(value, forKey: key) } else { try encodeNil(forKey: key) } }
|
||||
|
||||
/// Stores a keyed encoding container for the given key and returns it.
|
||||
///
|
||||
/// - parameter keyType: The key type to use for the container.
|
||||
@@ -189,6 +206,8 @@ extension MutablePersistable where Self: Encodable {
|
||||
try! self.encode(to: encoder)
|
||||
}
|
||||
}
|
||||
encode { (value, key) in container[key] = value }
|
||||
encode { (value, key) in
|
||||
container[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user