diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index fb76347fe..f66a852df 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -2,13 +2,12 @@ import Foundation private struct RowKeyedDecodingContainer: KeyedDecodingContainerProtocol { let decoder: RowDecoder - + var codingPath: [CodingKey] { return decoder.codingPath } + init(decoder: RowDecoder) { self.decoder = decoder } - var codingPath: [CodingKey] { return decoder.codingPath } - /// All the keys the `Decoder` has for this container. /// /// Different keyed containers from the same `Decoder` may return different keys here; it is possible to encode with multiple key types which are not convertible to one another. This should report all keys present which are convertible to the requested type. @@ -213,11 +212,10 @@ private struct RowKeyedDecodingContainer: KeyedDecodingContainer } private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer { - let row: Row + var row: Row + var columnIndex: Int var codingPath: [CodingKey] - let column: CodingKey - let columnIndex: Int - + /// Decodes a null value. /// /// - returns: Whether the encountered value was null. @@ -260,21 +258,14 @@ private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer { } else if let type = T.self as? DatabaseValueConvertible.Type { return type.decode(from: row, atUncheckedIndex: columnIndex) as! T } else { - return try T(from: RowSingleValueDecoder(row: row, columnIndex: columnIndex, codingPath: [column])) + return try T(from: RowSingleValueDecoder(row: row, columnIndex: columnIndex, codingPath: codingPath)) } } } private struct RowDecoder: Decoder { - let row: Row - - init(row: Row, codingPath: [CodingKey]) { - self.row = row - self.codingPath = codingPath - } - - // Decoder - let codingPath: [CodingKey] + var row: Row + var codingPath: [CodingKey] var userInfo: [CodingUserInfoKey : Any] { return [:] } func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer { @@ -291,18 +282,9 @@ private struct RowDecoder: Decoder { } private struct RowSingleValueDecoder: Decoder { - let row: Row - let columnIndex: Int - - init(row: Row, columnIndex: Int, codingPath: [CodingKey]) { - assert(!codingPath.isEmpty, "coding key required") - self.row = row - self.columnIndex = columnIndex - self.codingPath = codingPath - } - - // Decoder - let codingPath: [CodingKey] + var row: Row + var columnIndex: Int + var codingPath: [CodingKey] var userInfo: [CodingUserInfoKey : Any] { return [:] } func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer { @@ -314,10 +296,11 @@ private struct RowSingleValueDecoder: Decoder { } func singleValueContainer() throws -> SingleValueDecodingContainer { - return RowSingleValueDecodingContainer(row: row, codingPath: codingPath, column: codingPath.last!, columnIndex: columnIndex) + return RowSingleValueDecodingContainer(row: row, columnIndex: columnIndex, codingPath: codingPath) } } +/// The error that triggers JSON decoding private struct JSONDecodingRequiredError: Error { } extension FetchableRecord where Self: Decodable {