Added nested Codable support by flattening into/from JSON

This commit is contained in:
Vlad Alexa committed 2018-08-07 15:29:16 +01:00
1 parent 566dae0574
commit feeedcb92d
4 files changed
+505 -5

No files matched your search

+24 -2
View File
@@ -1,3 +1,5 @@
import Foundation
private struct RowKeyedDecodingContainer<Key: CodingKey>: KeyedDecodingContainerProtocol {
let decoder: RowDecoder
@@ -79,7 +81,17 @@ private struct RowKeyedDecodingContainer<Key: CodingKey>: 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<Key: CodingKey>: 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)")
}
}
}
}
+105 -3
View File
@@ -1,3 +1,5 @@
import Foundation
private struct PersistableRecordKeyedEncodingContainer<Key: CodingKey> : KeyedEncodingContainerProtocol {
let encode: (_ value: DatabaseValueConvertible?, _ key: String) -> Void
@@ -40,7 +42,21 @@ private struct PersistableRecordKeyedEncodingContainer<Key: CodingKey> : 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<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> {
// 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<Key>(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<KeyType: CodingKey>: 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<T>(_ value: T, forKey key: KeyType) throws where T : Encodable { throw errorMessage }
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: KeyType) -> KeyedEncodingContainer<NestedKey> 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<T>(_ value: T) throws where T : Encodable { throw errorMessage }
func encode(_ value: Bool) throws { throw errorMessage }
func encodeNil() throws { throw errorMessage }
func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> 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
@@ -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")
}
}
}
@@ -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)
}
}
}
}