From feeedcb92dfa8941599ae458ad5893a47021d762 Mon Sep 17 00:00:00 2001 From: Vlad Alexa Date: Tue, 7 Aug 2018 15:29:16 +0100 Subject: [PATCH] Added nested Codable support by flattening into/from JSON --- GRDB/Record/FetchableRecord+Decodable.swift | 26 ++- GRDB/Record/PersistableRecord+Encodable.swift | 108 +++++++++- .../FetchableRecordDecodableTests.swift | 194 ++++++++++++++++++ Tests/GRDBTests/PersistableRecordTests.swift | 182 ++++++++++++++++ 4 files changed, 505 insertions(+), 5 deletions(-) diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index 44e510cfa..105e92b96 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -1,3 +1,5 @@ +import Foundation + private struct RowKeyedDecodingContainer: KeyedDecodingContainerProtocol { let decoder: RowDecoder @@ -79,7 +81,17 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer } else if dbValue.isNull { return nil } else { - return try T(from: RowDecoder(row: row, codingPath: codingPath + [key])) + do { + let natural = try T(from: RowDecoder(row: row, codingPath: codingPath + [key])) + return natural + } catch { + if let data = row.dataNoCopy(named: key.stringValue), let dataString = String(data: data, encoding: .utf8), dataString.hasPrefix("[{") || dataString.hasPrefix("{"), dataString.hasSuffix("}]") || dataString.hasSuffix("}") { + // If data looks like JSON data then decode it into model (nested model as JSON) + return try JSONDecoder().decode(type.self, from: data) + } else { + fatalError("\(error)") + } + } } } @@ -116,7 +128,17 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer // This allows decoding Date from String, or DatabaseValue from NULL. return type.fromDatabaseValue(dbValue) as! T } else { - return try T(from: RowDecoder(row: row, codingPath: codingPath + [key])) + do { + let natural = try T(from: RowDecoder(row: row, codingPath: codingPath + [key])) + return natural + } catch { + if let data = row.dataNoCopy(named: key.stringValue), let dataString = String(data: data, encoding: .utf8), dataString.hasPrefix("[{") || dataString.hasPrefix("{"), dataString.hasSuffix("}]") || dataString.hasSuffix("}") { + // If data looks like JSON data then decode it into model (nested model as JSON) + return try JSONDecoder().decode(type.self, from: data) + } else { + fatalError("\(error)") + } + } } } diff --git a/GRDB/Record/PersistableRecord+Encodable.swift b/GRDB/Record/PersistableRecord+Encodable.swift index 135e8a149..0cc002327 100644 --- a/GRDB/Record/PersistableRecord+Encodable.swift +++ b/GRDB/Record/PersistableRecord+Encodable.swift @@ -1,3 +1,5 @@ +import Foundation + private struct PersistableRecordKeyedEncodingContainer : KeyedEncodingContainerProtocol { let encode: (_ value: DatabaseValueConvertible?, _ key: String) -> Void @@ -40,7 +42,21 @@ private struct PersistableRecordKeyedEncodingContainer : KeyedEn // This allows us to encode Date as String, for example. encode((value as! DatabaseValueConvertible), key.stringValue) } else { - try value.encode(to: PersistableRecordEncoder(codingPath: [key], encode: encode)) + do { + try value.encode(to: PersistableRecordEncoder(codingPath: [key], encode: encode)) + } catch { + // If value.encode does not work e.g. "unkeyed encoding is not supported" then see if model can be stored as JSON + let encodeError = error + do { + let json = try JSONEncoder().encode(value) + guard let modelAsString = String(data: json, encoding: .utf8) else { + throw JsonStringError.covertStringError("Error, could not make string out of JSON data") + } + return encode(modelAsString, key.stringValue) + } catch { + fatalError("Encode error: \(encodeError), tried to encode to Json, got error: \(error)") + } + } } } @@ -169,7 +185,7 @@ private struct PersistableRecordEncoder : Encoder { func container(keyedBy type: Key.Type) -> KeyedEncodingContainer { // Asked for a keyed type: top level required guard codingPath.isEmpty else { - fatalError("unkeyed encoding is not supported") + return KeyedEncodingContainer(ThrowingKeyedContainer(error: EncodingError.invalidValue(codingPath.isEmpty, EncodingError.Context(codingPath: codingPath, debugDescription: "unkeyed encoding is not supported")))) } return KeyedEncodingContainer(PersistableRecordKeyedEncodingContainer(encode: encode)) } @@ -180,7 +196,7 @@ private struct PersistableRecordEncoder : Encoder { /// - precondition: May not be called after a prior `self.container(keyedBy:)` call. /// - precondition: May not be called after a value has been encoded through a previous `self.singleValueContainer()` call. func unkeyedContainer() -> UnkeyedEncodingContainer { - fatalError("unkeyed encoding is not supported") + return ThrowingUnkeyedContainer(error: EncodingError.invalidValue(encode, EncodingError.Context(codingPath: [], debugDescription: "unkeyed encoding is not supported"))) } /// Returns an encoding container appropriate for holding a single primitive value. @@ -194,6 +210,92 @@ private struct PersistableRecordEncoder : Encoder { } } +class ThrowingKeyedContainer: KeyedEncodingContainerProtocol { + let errorMessage: Error + var codingPath: [CodingKey] = [] + + init(error: Error) { + errorMessage = error + } + + func encodeNil(forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Bool, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Int, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Int8, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Int16, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Int32, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Int64, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: UInt, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: UInt8, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: UInt16, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: UInt32, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: UInt64, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Float, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: Double, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: String, forKey key: KeyType) throws { throw errorMessage } + func encode(_ value: T, forKey key: KeyType) throws where T : Encodable { throw errorMessage } + + func nestedContainer(keyedBy keyType: NestedKey.Type, forKey key: KeyType) -> KeyedEncodingContainer where NestedKey : CodingKey { + fatalError("Not implemented") + } + func nestedUnkeyedContainer(forKey key: KeyType) -> UnkeyedEncodingContainer { + fatalError("Not implemented") + } + func superEncoder() -> Encoder { + fatalError("Not implemented") + } + + func superEncoder(forKey key: KeyType) -> Encoder { + fatalError("Not implemented") + } +} + +class ThrowingUnkeyedContainer: UnkeyedEncodingContainer { + let errorMessage: Error + var codingPath: [CodingKey] = [] + var count: Int = 0 + + init(error: Error) { + errorMessage = error + } + + func encode(_ value: Int) throws { throw errorMessage } + func encode(_ value: Int8) throws { throw errorMessage } + func encode(_ value: Int16) throws { throw errorMessage } + func encode(_ value: Int32) throws { throw errorMessage } + func encode(_ value: Int64) throws { throw errorMessage } + func encode(_ value: UInt) throws { throw errorMessage } + func encode(_ value: UInt8) throws { throw errorMessage } + func encode(_ value: UInt16) throws { throw errorMessage } + func encode(_ value: UInt32) throws { throw errorMessage } + func encode(_ value: UInt64) throws { throw errorMessage } + func encode(_ value: Float) throws { throw errorMessage } + func encode(_ value: Double) throws { throw errorMessage } + func encode(_ value: String) throws { throw errorMessage } + func encode(_ value: T) throws where T : Encodable { throw errorMessage } + func encode(_ value: Bool) throws { throw errorMessage } + func encodeNil() throws { throw errorMessage } + + func nestedContainer(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer where NestedKey : CodingKey { + fatalError("Not implemented") + + } + + func nestedUnkeyedContainer() -> UnkeyedEncodingContainer { + fatalError("Not implemented") + + } + + func superEncoder() -> Encoder { + fatalError("Not implemented") + + } +} + +enum JsonStringError: Error { + case covertStringError(String) +} + extension MutablePersistableRecord where Self: Encodable { public func encode(to container: inout PersistenceContainer) { // The inout container parameter won't enter an escaping closure since diff --git a/Tests/GRDBTests/FetchableRecordDecodableTests.swift b/Tests/GRDBTests/FetchableRecordDecodableTests.swift index 03ab93312..d72c1222d 100644 --- a/Tests/GRDBTests/FetchableRecordDecodableTests.swift +++ b/Tests/GRDBTests/FetchableRecordDecodableTests.swift @@ -301,3 +301,197 @@ extension FetchableRecordDecodableTests { XCTAssertEqual(value.uuid, uuid) } } + +// MARK: - Custom nested Decodable types - nested saved as JSON + +extension FetchableRecordDecodableTests { + func testOptionalNestedStruct() throws { + struct NestedStruct : PersistableRecord, FetchableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable { + static let databaseTableName = "t1" + let nested: NestedStruct? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: NestedStruct()) + try value.insert(db) + + let parentModel = try StructWithNestedType.fetchAll(db) + + guard let nestedModel = parentModel.first?.nested else { + XCTFail() + return + } + + // Check the nested model contains the expected values of first and last name + XCTAssertEqual(nestedModel.firstName, "Bob") + XCTAssertEqual(nestedModel.lastName, "Dylan") + } + } + + func testOptionalNestedStructNil() throws { + struct NestedStruct : PersistableRecord, FetchableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable { + static let databaseTableName = "t1" + let nested: NestedStruct? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: nil) + try value.insert(db) + + let parentModel = try StructWithNestedType.fetchAll(db) + + XCTAssertNil(parentModel.first?.nested) + } + } + + func testOptionalNestedArrayStruct() throws { + struct NestedStruct : PersistableRecord, FetchableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable { + static let databaseTableName = "t1" + let nested: [NestedStruct]? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: [NestedStruct(), NestedStruct()]) + try value.insert(db) + + let parentModel = try StructWithNestedType.fetchAll(db) + + guard let arrayOfNestedModel = parentModel.first?.nested, let firstNestedModelInArray = arrayOfNestedModel.first else { + XCTFail() + return + } + + // Check there are two models in array + XCTAssertTrue(arrayOfNestedModel.count == 2) + + // Check the nested model contains the expected values of first and last name + XCTAssertEqual(firstNestedModelInArray.firstName, "Bob") + XCTAssertEqual(firstNestedModelInArray.lastName, "Dylan") + } + } + + func testOptionalNestedArrayStructNil() throws { + struct NestedStruct : PersistableRecord, FetchableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable { + static let databaseTableName = "t1" + let nested: [NestedStruct]? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: nil) + try value.insert(db) + + let parentModel = try StructWithNestedType.fetchAll(db) + + XCTAssertNil(parentModel.first?.nested) + } + } + + func testNonOptionalNestedStruct() throws { + struct NestedStruct : PersistableRecord, FetchableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable { + static let databaseTableName = "t1" + let nested: NestedStruct + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: NestedStruct()) + try value.insert(db) + + let parentModel = try StructWithNestedType.fetchAll(db) + + guard let nestedModel = parentModel.first?.nested else { + XCTFail() + return + } + + // Check the nested model contains the expected values of first and last name + XCTAssertEqual(nestedModel.firstName, "Bob") + XCTAssertEqual(nestedModel.lastName, "Dylan") + } + } + + func testNonOptionalNestedArrayStruct() throws { + struct NestedStruct : PersistableRecord, FetchableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, FetchableRecord, Codable { + static let databaseTableName = "t1" + let nested: [NestedStruct] + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: [NestedStruct(), NestedStruct()]) + try value.insert(db) + + let parentModel = try StructWithNestedType.fetchAll(db) + + guard let arrayOfNestedModel = parentModel.first?.nested, let firstNestedModelInArray = arrayOfNestedModel.first else { + XCTFail() + return + } + + // Check there are two models in array + XCTAssertTrue(arrayOfNestedModel.count == 2) + + // Check the nested model contains the expected values of first and last name + XCTAssertEqual(firstNestedModelInArray.firstName, "Bob") + XCTAssertEqual(firstNestedModelInArray.lastName, "Dylan") + } + } +} diff --git a/Tests/GRDBTests/PersistableRecordTests.swift b/Tests/GRDBTests/PersistableRecordTests.swift index 3d101212f..3853d0ad9 100644 --- a/Tests/GRDBTests/PersistableRecordTests.swift +++ b/Tests/GRDBTests/PersistableRecordTests.swift @@ -761,3 +761,185 @@ class PersistableRecordTests: GRDBTestCase { } } } + +// MARK: - Custom nested Codable types - nested saved as JSON + +extension PersistableRecordTests { + + func testOptionalNestedStruct() throws { + struct NestedStruct : PersistableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, Codable { + static let databaseTableName = "t1" + let nested: NestedStruct? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: NestedStruct()) + try value.insert(db) + + let dbValue = try DatabaseValue.fetchOne(db, "SELECT nested FROM t1")! + + // Encodable has a default implementation which encodes a model to JSON as String. + // We expect here JSON in the form of a String + XCTAssert(dbValue.storage.value is String) + let string = dbValue.storage.value as! String + if let data = string.data(using: .utf8) { + do { + let decoded = try JSONDecoder().decode(NestedStruct.self, from: data) + XCTAssertEqual(NestedStruct().firstName, decoded.firstName) + XCTAssertEqual(NestedStruct().lastName, decoded.lastName) + } catch { + XCTFail(error.localizedDescription) + } + } else { + XCTFail("Failed to convert " + string) + } + } + } + + func testOptionalNestedStructNil() throws { + struct NestedStruct : PersistableRecord, Encodable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, Encodable { + static let databaseTableName = "t1" + let nested: NestedStruct? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: nil) + try value.insert(db) + + let dbValue = try DatabaseValue.fetchOne(db, "SELECT nested FROM t1")! + + // We expect here nil + XCTAssertNil(dbValue.storage.value) + } + } + + func testOptionalNestedArrayStruct() throws { + struct NestedStruct : PersistableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, Codable { + static let databaseTableName = "t1" + let nested: [NestedStruct]? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: [NestedStruct(), NestedStruct()]) + try value.insert(db) + + let dbValue = try DatabaseValue.fetchOne(db, "SELECT nested FROM t1")! + + // Encodable has a default implementation which encodes a model to JSON as String. + // We expect here JSON in the form of a String + XCTAssert(dbValue.storage.value is String) + let string = dbValue.storage.value as! String + if let data = string.data(using: .utf8) { + do { + let decoded = try JSONDecoder().decode([NestedStruct].self, from: data) + XCTAssertEqual(decoded.count, 2) + XCTAssertEqual(NestedStruct().firstName, decoded.first!.firstName) + XCTAssertEqual(NestedStruct().lastName, decoded.first!.lastName) + XCTAssertEqual(NestedStruct().firstName, decoded.last!.firstName) + XCTAssertEqual(NestedStruct().lastName, decoded.last!.lastName) + } catch { + XCTFail(error.localizedDescription) + } + } else { + XCTFail("Failed to convert " + string) + } + } + } + + func testOptionalNestedArrayStructNil() throws { + struct NestedStruct : PersistableRecord, Encodable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, Encodable { + static let databaseTableName = "t1" + let nested: [NestedStruct]? + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: nil) + try value.insert(db) + + let dbValue = try DatabaseValue.fetchOne(db, "SELECT nested FROM t1")! + + // We expect here nil + XCTAssertNil(dbValue.storage.value) + } + } + + func testNonOptionalNestedStruct() throws { + struct NestedStruct : PersistableRecord, Codable { + let firstName = "Bob" + let lastName = "Dylan" + } + + struct StructWithNestedType : PersistableRecord, Codable { + static let databaseTableName = "t1" + let nested: NestedStruct + } + + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "t1") { t in + t.column("nested", .text) + } + + let value = StructWithNestedType(nested: NestedStruct()) + try value.insert(db) + + let dbValue = try DatabaseValue.fetchOne(db, "SELECT nested FROM t1")! + + // Encodable has a default implementation which encodes a model to JSON as String. + // We expect here JSON in the form of a String + XCTAssert(dbValue.storage.value is String) + let string = dbValue.storage.value as! String + if let data = string.data(using: .utf8) { + do { + let decoded = try JSONDecoder().decode(NestedStruct.self, from: data) + XCTAssertEqual(NestedStruct().firstName, decoded.firstName) + XCTAssertEqual(NestedStruct().lastName, decoded.lastName) + } catch { + XCTFail(error.localizedDescription) + } + } else { + XCTFail("Failed to convert " + string) + } + } + } +}