From b90f876b5b0e4208c332f25fd5e4cd68d4376fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 9 Oct 2017 08:06:46 +0200 Subject: [PATCH] Records that adopt Encodable encode null columns --- GRDB/Record/Persistable+Encodable.swift | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/GRDB/Record/Persistable+Encodable.swift b/GRDB/Record/Persistable+Encodable.swift index ffcbc5093..4fa8d7779 100644 --- a/GRDB/Record/Persistable+Encodable.swift +++ b/GRDB/Record/Persistable+Encodable.swift @@ -28,7 +28,6 @@ private struct PersistableKeyedEncodingContainer : 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 : 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(_ 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 + } } }