From 7cb58cfffc7a23464731216a71422e85d24ab4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 18 Aug 2018 20:27:07 +0200 Subject: [PATCH] Major cleanup of FetchableRecord+Decodable --- GRDB/Record/FetchableRecord+Decodable.swift | 520 ++++++++---------- GRDB/Record/PersistableRecord+Encodable.swift | 14 +- 2 files changed, 231 insertions(+), 303 deletions(-) diff --git a/GRDB/Record/FetchableRecord+Decodable.swift b/GRDB/Record/FetchableRecord+Decodable.swift index a1f8f378f..3ba5fdb6a 100644 --- a/GRDB/Record/FetchableRecord+Decodable.swift +++ b/GRDB/Record/FetchableRecord+Decodable.swift @@ -1,285 +1,264 @@ import Foundation -private struct RowKeyedDecodingContainer: KeyedDecodingContainerProtocol { - let decoder: RowDecoder - var codingPath: [CodingKey] { return decoder.codingPath } +// MARK: - RowDecoder - init(decoder: RowDecoder) { - self.decoder = decoder +/// The decoder that decodes a record from a database row +private struct RowDecoder: Decoder { + var row: Row + var codingPath: [CodingKey] + var userInfo: [CodingUserInfoKey: Any] { return Record.databaseDecodingUserInfo } + + func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer { + return KeyedDecodingContainer(KeyedContainer(decoder: self)) } - /// 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. - var allKeys: [Key] { - let row = decoder.row - let columnNames = Set(row.columnNames) - let scopeNames = Set(row.scopesTree.names) - return columnNames.union(scopeNames).compactMap { Key(stringValue: $0) } + func unkeyedContainer() throws -> UnkeyedDecodingContainer { + fatalError("unkeyed decoding from database row is not supported") } - /// Returns whether the `Decoder` contains a value associated with the given key. - /// - /// The value associated with the given key may be a null value as appropriate for the data format. - /// - /// - parameter key: The key to search for. - /// - returns: Whether the `Decoder` has an entry for the given key. - func contains(_ key: Key) -> Bool { - let row = decoder.row - return row.hasColumn(key.stringValue) || (row.scopesTree[key.stringValue] != nil) + func singleValueContainer() throws -> SingleValueDecodingContainer { + fatalError("single value decoding from database row is not supported") } - /// Decodes a null value for the given key. - /// - /// - parameter key: The key that the decoded value is associated with. - /// - returns: Whether the encountered value was null. - /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry for the given key. - func decodeNil(forKey key: Key) throws -> Bool { - let row = decoder.row - return row[key.stringValue] == nil && (row.scopesTree[key.stringValue] == nil) - } - - /// Decodes a value of the given type for the given key. - /// - /// - parameter type: The type of value to decode. - /// - parameter key: The key that the decoded value is associated with. - /// - returns: A value of the requested type, if present for the given key and convertible to the requested type. - /// - throws: `DecodingError.typeMismatch` if the encountered encoded value is not convertible to the requested type. - /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry for the given key. - /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for the given key. - func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { return decoder.row[key.stringValue] } - func decode(_ type: Int.Type, forKey key: Key) throws -> Int { return decoder.row[key.stringValue] } - func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { return decoder.row[key.stringValue] } - func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 { return decoder.row[key.stringValue] } - func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 { return decoder.row[key.stringValue] } - func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 { return decoder.row[key.stringValue] } - func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt { return decoder.row[key.stringValue] } - func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 { return decoder.row[key.stringValue] } - func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 { return decoder.row[key.stringValue] } - func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { return decoder.row[key.stringValue] } - func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { return decoder.row[key.stringValue] } - func decode(_ type: Float.Type, forKey key: Key) throws -> Float { return decoder.row[key.stringValue] } - func decode(_ type: Double.Type, forKey key: Key) throws -> Double { return decoder.row[key.stringValue] } - func decode(_ type: String.Type, forKey key: Key) throws -> String { return decoder.row[key.stringValue] } - - /// Decodes a value of the given type for the given key, if present. - /// - /// This method returns nil if the container does not have a value - /// associated with key, or if the value is null. The difference between - /// these states can be distinguished with a contains(_:) call. - func decodeIfPresent(_ type: T.Type, forKey key: Key) throws -> T? where T : Decodable { - let row = decoder.row - let keyName = key.stringValue - - // Column? - if let index = row.index(ofColumn: keyName) { - // Prefer DatabaseValueConvertible decoding over Decodable. - // This allows decoding Date from String, or DatabaseValue from NULL. - if let type = T.self as? (DatabaseValueConvertible & StatementColumnConvertible).Type { - return type.fastDecodeIfPresent(from: row, atUncheckedIndex: index) as! T? - } else if let type = T.self as? DatabaseValueConvertible.Type { - return type.decodeIfPresent(from: row, atUncheckedIndex: index) as! T? - } else if row.impl.hasNull(atUncheckedIndex: index) { - return nil - } else { - do { - // This decoding will fail for types that decode from keyed - // or unkeyed containers, because we're decoding a single - // value here (string, int, double, data, null). If such an - // error happens, we'll switch to JSON decoding. - let singleValueDecoder = RowSingleValueDecoder( - row: row, - columnIndex: index, - codingPath: codingPath + [key], - userInfo: decoder.userInfo) - return try T(from: singleValueDecoder) - } catch is JSONRequiredError { - guard let data = row.dataNoCopy(atIndex: index) else { - fatalConversionError(to: T.self, from: row[index], conversionContext: ValueConversionContext(row).atColumn(index)) - } - return try decoder - .JSONDecoder(keyName) - .decode(type.self, from: data) + struct KeyedContainer: KeyedDecodingContainerProtocol { + let decoder: RowDecoder + var codingPath: [CodingKey] { return decoder.codingPath } + + init(decoder: RowDecoder) { + self.decoder = decoder + } + + var allKeys: [Key] { + let row = decoder.row + let columnNames = Set(row.columnNames) + let scopeNames = Set(row.scopesTree.names) + return columnNames.union(scopeNames).compactMap { Key(stringValue: $0) } + } + + func contains(_ key: Key) -> Bool { + let row = decoder.row + return row.hasColumn(key.stringValue) || (row.scopesTree[key.stringValue] != nil) + } + + func decodeNil(forKey key: Key) throws -> Bool { + let row = decoder.row + return row[key.stringValue] == nil && (row.scopesTree[key.stringValue] == nil) + } + + func decode(_ type: Bool.Type, forKey key: Key) throws -> Bool { return decoder.row[key.stringValue] } + func decode(_ type: Int.Type, forKey key: Key) throws -> Int { return decoder.row[key.stringValue] } + func decode(_ type: Int8.Type, forKey key: Key) throws -> Int8 { return decoder.row[key.stringValue] } + func decode(_ type: Int16.Type, forKey key: Key) throws -> Int16 { return decoder.row[key.stringValue] } + func decode(_ type: Int32.Type, forKey key: Key) throws -> Int32 { return decoder.row[key.stringValue] } + func decode(_ type: Int64.Type, forKey key: Key) throws -> Int64 { return decoder.row[key.stringValue] } + func decode(_ type: UInt.Type, forKey key: Key) throws -> UInt { return decoder.row[key.stringValue] } + func decode(_ type: UInt8.Type, forKey key: Key) throws -> UInt8 { return decoder.row[key.stringValue] } + func decode(_ type: UInt16.Type, forKey key: Key) throws -> UInt16 { return decoder.row[key.stringValue] } + func decode(_ type: UInt32.Type, forKey key: Key) throws -> UInt32 { return decoder.row[key.stringValue] } + func decode(_ type: UInt64.Type, forKey key: Key) throws -> UInt64 { return decoder.row[key.stringValue] } + func decode(_ type: Float.Type, forKey key: Key) throws -> Float { return decoder.row[key.stringValue] } + func decode(_ type: Double.Type, forKey key: Key) throws -> Double { return decoder.row[key.stringValue] } + func decode(_ type: String.Type, forKey key: Key) throws -> String { return decoder.row[key.stringValue] } + + func decodeIfPresent(_ type: T.Type, forKey key: Key) throws -> T? where T : Decodable { + let row = decoder.row + let keyName = key.stringValue + + // Column? + if let index = row.index(ofColumn: keyName) { + // Prefer DatabaseValueConvertible decoding over Decodable. + // This allows decoding Date from String, or DatabaseValue from NULL. + if let type = T.self as? (DatabaseValueConvertible & StatementColumnConvertible).Type { + return type.fastDecodeIfPresent(from: row, atUncheckedIndex: index) as! T? + } else if let type = T.self as? DatabaseValueConvertible.Type { + return type.decodeIfPresent(from: row, atUncheckedIndex: index) as! T? + } else if row.impl.hasNull(atUncheckedIndex: index) { + return nil + } else { + return try decode(type, fromColumnAtIndex: index, key: key) } } - } - - // Scope? - if let scopedRow = row.scopesTree[keyName], scopedRow.containsNonNullValue { - if let type = T.self as? FetchableRecord.Type { - // Prefer FetchableRecord decoding over Decodable. - // This allows custom row decoding - return (type.init(row: scopedRow) as! T) - } else { - let scopedDecoder = RowDecoder( - row: scopedRow, - codingPath: codingPath + [key], - userInfo: decoder.userInfo, - JSONDecoder: decoder.JSONDecoder) - return try T(from: scopedDecoder) + + // Scope? (beware left joins, and check if scoped row contains non-null values) + if let scopedRow = row.scopesTree[keyName], scopedRow.containsNonNullValue { + return try decode(type, fromRow: scopedRow, codingPath: codingPath + [key]) } + + // Key is not a column, and not a scope. + return nil } - return nil - } - - /// Decodes a value of the given type for the given key. - /// - /// - parameter type: The type of value to decode. - /// - parameter key: The key that the decoded value is associated with. - /// - returns: A value of the requested type, if present for the given key and convertible to the requested type. - /// - throws: `DecodingError.typeMismatch` if the encountered encoded value is not convertible to the requested type. - /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry for the given key. - /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for the given key. - func decode(_ type: T.Type, forKey key: Key) throws -> T where T : Decodable { - let row = decoder.row - let keyName = key.stringValue - - // Column? - if let index = row.index(ofColumn: keyName) { - // Prefer DatabaseValueConvertible decoding over Decodable. - // This allows decoding Date from String, or DatabaseValue from NULL. - if let type = T.self as? (DatabaseValueConvertible & StatementColumnConvertible).Type { - return type.fastDecode(from: row, atUncheckedIndex: index) as! T - } else if let type = T.self as? DatabaseValueConvertible.Type { - return type.decode(from: row, atUncheckedIndex: index) as! T - } else { - do { - // This decoding will fail for types that decode from keyed - // or unkeyed containers, because we're decoding a single - // value here (string, int, double, data, null). If such an - // error happens, we'll switch to JSON decoding. - let singleValueDecoder = RowSingleValueDecoder( - row: row, - columnIndex: index, - codingPath: codingPath + [key], - userInfo: decoder.userInfo) - return try T(from: singleValueDecoder) - } catch is JSONRequiredError { - guard let data = row.dataNoCopy(atIndex: index) else { - fatalConversionError(to: T.self, from: row[index], conversionContext: ValueConversionContext(row).atColumn(index)) - } - return try decoder - .JSONDecoder(keyName) - .decode(type.self, from: data) + /// Decodes a value of the given type for the given key. + /// + /// - parameter type: The type of value to decode. + /// - parameter key: The key that the decoded value is associated with. + /// - returns: A value of the requested type, if present for the given key and convertible to the requested type. + /// - throws: `DecodingError.typeMismatch` if the encountered encoded value is not convertible to the requested type. + /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry for the given key. + /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for the given key. + func decode(_ type: T.Type, forKey key: Key) throws -> T where T : Decodable { + let row = decoder.row + let keyName = key.stringValue + + // Column? + if let index = row.index(ofColumn: keyName) { + // Prefer DatabaseValueConvertible decoding over Decodable. + // This allows decoding Date from String, or DatabaseValue from NULL. + if let type = T.self as? (DatabaseValueConvertible & StatementColumnConvertible).Type { + return type.fastDecode(from: row, atUncheckedIndex: index) as! T + } else if let type = T.self as? DatabaseValueConvertible.Type { + return type.decode(from: row, atUncheckedIndex: index) as! T + } else { + return try decode(type, fromColumnAtIndex: index, key: key) } } + + // Scope? + if let scopedRow = row.scopesTree[keyName] { + return try decode(type, fromRow: scopedRow, codingPath: codingPath + [key]) + } + + // Key is not a column, and not a scope. + // + // Should be throw an error? Well... The use case is the following: + // + // // SELECT book.*, author.* FROM book + // // JOIN author ON author.id = book.authorId + // let request = Book.including(required: Book.author) + // + // Rows loaded from this request don't have any "book" key: + // + // let row = try Row.fetchOne(db, request)! + // print(row.debugDescription) + // // ▿ [id:1 title:"Moby-Dick" authorId:2] + // // unadapted: [id:1 title:"Moby-Dick" authorId:2 id:2 name:"Melville"] + // // author: [id:2 name:"Melville"] + // + // And yet we have to decode the "book" key when we decode the + // BookInfo type below: + // + // struct BookInfo { + // var book: Book // <- decodes from the "book" key + // var author: Author + // } + // let infos = try BookInfos.fetchAll(db, request) + // + // Our current strategy is to assume that a missing key (such as + // "book", which is not the name of a column, and not the name of a + // scope) has to be decoded right from the base row. + // + // Yeah, there may be better ways to handle this. + return try decode(type, fromRow: row, codingPath: codingPath + [key]) } - - // Scope? - if let scopedRow = row.scopesTree[keyName] { + + /// Returns the data stored for the given key as represented in a container keyed by the given key type. + /// + /// - parameter type: The key type to use for the container. + /// - parameter key: The key that the nested container is associated with. + /// - returns: A keyed decoding container view into `self`. + /// - throws: `DecodingError.typeMismatch` if the encountered stored value is not a keyed container. + func nestedContainer(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer where NestedKey : CodingKey { + fatalError("not implemented") + } + + func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer { + throw DecodingError.typeMismatch( + UnkeyedDecodingContainer.self, + DecodingError.Context(codingPath: codingPath, debugDescription: "unkeyed decoding is not supported")) + } + + func superDecoder() throws -> Decoder { + // Not sure + return decoder + } + + func superDecoder(forKey key: Key) throws -> Decoder { + fatalError("not implemented") + } + + // Helper methods + + @inline(__always) + private func decode(_ type: T.Type, fromRow row: Row, codingPath: [CodingKey]) throws -> T where T: Decodable { if let type = T.self as? FetchableRecord.Type { // Prefer FetchableRecord decoding over Decodable. - // This allows custom row decoding - return type.init(row: scopedRow) as! T + return type.init(row: row) as! T } else { - let scopedDecoder = RowDecoder( - row: scopedRow, - codingPath: codingPath + [key], - userInfo: decoder.userInfo, - JSONDecoder: decoder.JSONDecoder) - return try T(from: scopedDecoder) + let decoder = RowDecoder(row: row, codingPath: codingPath) + return try T(from: decoder) } } - // Base row - if let type = T.self as? FetchableRecord.Type { - // Prefer FetchableRecord decoding over Decodable. - // This allows custom row decoding - return type.init(row: row) as! T - } else { - let baseDecoder = RowDecoder( - row: row, - codingPath: codingPath + [key], - userInfo: decoder.userInfo, - JSONDecoder: decoder.JSONDecoder) - return try T(from: baseDecoder) + @inline(__always) + private func decode(_ type: T.Type, fromColumnAtIndex index: Int, key: Key) throws -> T where T: Decodable { + let row = decoder.row + do { + // This decoding will fail for types that decode from keyed + // or unkeyed containers, because we're decoding a single + // value here (string, int, double, data, null). If such an + // error happens, we'll switch to JSON decoding. + let columnDecoder = ColumnDecoder( + row: row, + columnIndex: index, + codingPath: codingPath + [key]) + return try T(from: columnDecoder) + } catch is JSONRequiredError { + // Decode from JSON + guard let data = row.dataNoCopy(atIndex: index) else { + fatalConversionError(to: T.self, from: row[index], conversionContext: ValueConversionContext(row).atColumn(index)) + } + return try Record + .databaseJSONDecoder(for: key.stringValue) + .decode(type.self, from: data) + } } } - - /// Returns the data stored for the given key as represented in a container keyed by the given key type. - /// - /// - parameter type: The key type to use for the container. - /// - parameter key: The key that the nested container is associated with. - /// - returns: A keyed decoding container view into `self`. - /// - throws: `DecodingError.typeMismatch` if the encountered stored value is not a keyed container. - func nestedContainer(keyedBy type: NestedKey.Type, forKey key: Key) throws -> KeyedDecodingContainer where NestedKey : CodingKey { - fatalError("not implemented") - } - - /// Returns the data stored for the given key as represented in an unkeyed container. - /// - /// - parameter key: The key that the nested container is associated with. - /// - returns: An unkeyed decoding container view into `self`. - /// - throws: `DecodingError.typeMismatch` if the encountered stored value is not an unkeyed container. - func nestedUnkeyedContainer(forKey key: Key) throws -> UnkeyedDecodingContainer { - throw DecodingError.typeMismatch( - UnkeyedDecodingContainer.self, - DecodingError.Context(codingPath: codingPath, debugDescription: "unkeyed decoding is not supported")) - } - - /// Returns a `Decoder` instance for decoding `super` from the container associated with the default `super` key. - /// - /// Equivalent to calling `superDecoder(forKey:)` with `Key(stringValue: "super", intValue: 0)`. - /// - /// - returns: A new `Decoder` to pass to `super.init(from:)`. - /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry for the default `super` key. - /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for the default `super` key. - public func superDecoder() throws -> Decoder { - return decoder - } - - /// Returns a `Decoder` instance for decoding `super` from the container associated with the given key. - /// - /// - parameter key: The key to decode `super` for. - /// - returns: A new `Decoder` to pass to `super.init(from:)`. - /// - throws: `DecodingError.keyNotFound` if `self` does not have an entry for the given key. - /// - throws: `DecodingError.valueNotFound` if `self` has a null entry for the given key. - public func superDecoder(forKey key: Key) throws -> Decoder { - return decoder - } } -private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer { +// MARK: - ColumnDecoder + +/// The decoder that decodes from a database column +private struct ColumnDecoder: Decoder { var row: Row var columnIndex: Int var codingPath: [CodingKey] - var userInfo: [CodingUserInfoKey: Any] + var userInfo: [CodingUserInfoKey: Any] { return Record.databaseDecodingUserInfo } + + func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer { + // We need to switch to JSON decoding + throw JSONRequiredError() + } + + func unkeyedContainer() throws -> UnkeyedDecodingContainer { + // We need to switch to JSON decoding + throw JSONRequiredError() + } + + func singleValueContainer() throws -> SingleValueDecodingContainer { + return self + } +} - /// Decodes a null value. - /// - /// - returns: Whether the encountered value was null. +extension ColumnDecoder: SingleValueDecodingContainer { func decodeNil() -> Bool { return row.hasNull(atIndex: columnIndex) } - - /// Decodes a single value of the given type. - /// - /// - parameter type: The type to decode as. - /// - returns: A value of the requested type. - /// - throws: `DecodingError.typeMismatch` if the encountered encoded value cannot be converted to the requested type. - /// - throws: `DecodingError.valueNotFound` if the encountered encoded value is null. - func decode(_ type: Bool.Type) throws -> Bool { return row[columnIndex] } - func decode(_ type: Int.Type) throws -> Int { return row[columnIndex] } - func decode(_ type: Int8.Type) throws -> Int8 { return row[columnIndex] } - func decode(_ type: Int16.Type) throws -> Int16 { return row[columnIndex] } - func decode(_ type: Int32.Type) throws -> Int32 { return row[columnIndex] } - func decode(_ type: Int64.Type) throws -> Int64 { return row[columnIndex] } - func decode(_ type: UInt.Type) throws -> UInt { return row[columnIndex] } - func decode(_ type: UInt8.Type) throws -> UInt8 { return row[columnIndex] } + + func decode(_ type: Bool.Type ) throws -> Bool { return row[columnIndex] } + func decode(_ type: Int.Type ) throws -> Int { return row[columnIndex] } + func decode(_ type: Int8.Type ) throws -> Int8 { return row[columnIndex] } + func decode(_ type: Int16.Type ) throws -> Int16 { return row[columnIndex] } + func decode(_ type: Int32.Type ) throws -> Int32 { return row[columnIndex] } + func decode(_ type: Int64.Type ) throws -> Int64 { return row[columnIndex] } + func decode(_ type: UInt.Type ) throws -> UInt { return row[columnIndex] } + func decode(_ type: UInt8.Type ) throws -> UInt8 { return row[columnIndex] } func decode(_ type: UInt16.Type) throws -> UInt16 { return row[columnIndex] } func decode(_ type: UInt32.Type) throws -> UInt32 { return row[columnIndex] } func decode(_ type: UInt64.Type) throws -> UInt64 { return row[columnIndex] } - func decode(_ type: Float.Type) throws -> Float { return row[columnIndex] } + func decode(_ type: Float.Type ) throws -> Float { return row[columnIndex] } func decode(_ type: Double.Type) throws -> Double { return row[columnIndex] } func decode(_ type: String.Type) throws -> String { return row[columnIndex] } - - /// Decodes a single value of the given type. - /// - /// - parameter type: The type to decode as. - /// - returns: A value of the requested type. - /// - throws: `DecodingError.typeMismatch` if the encountered encoded value cannot be converted to the requested type. - /// - throws: `DecodingError.valueNotFound` if the encountered encoded value is null. + func decode(_ type: T.Type) throws -> T where T : Decodable { // Prefer DatabaseValueConvertible decoding over Decodable. // This allows decoding Date from String, or DatabaseValue from NULL. @@ -288,69 +267,18 @@ private struct RowSingleValueDecodingContainer: SingleValueDecodingContainer { } else if let type = T.self as? DatabaseValueConvertible.Type { return type.decode(from: row, atUncheckedIndex: columnIndex) as! T } else { - let singleValueDecoder = RowSingleValueDecoder( - row: row, - columnIndex: columnIndex, - codingPath: codingPath, - userInfo: userInfo) - return try T(from: singleValueDecoder) + return try T(from: self) } } } -private struct RowDecoder: Decoder { - var row: Row - var codingPath: [CodingKey] - var userInfo: [CodingUserInfoKey: Any] - var JSONDecoder: (String) -> JSONDecoder - - func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer { - return KeyedDecodingContainer(RowKeyedDecodingContainer(decoder: self)) - } - - func unkeyedContainer() throws -> UnkeyedDecodingContainer { - throw JSONRequiredError() - } - - func singleValueContainer() throws -> SingleValueDecodingContainer { - throw JSONRequiredError() - } -} - -private struct RowSingleValueDecoder: Decoder { - var row: Row - var columnIndex: Int - var codingPath: [CodingKey] - var userInfo: [CodingUserInfoKey: Any] - - func container(keyedBy type: Key.Type) throws -> KeyedDecodingContainer { - throw JSONRequiredError() - } - - func unkeyedContainer() throws -> UnkeyedDecodingContainer { - throw JSONRequiredError() - } - - func singleValueContainer() throws -> SingleValueDecodingContainer { - return RowSingleValueDecodingContainer( - row: row, - columnIndex: columnIndex, - codingPath: codingPath, - userInfo: userInfo) - } -} - /// The error that triggers JSON decoding private struct JSONRequiredError: Error { } extension FetchableRecord where Self: Decodable { /// Initializes a record from `row`. public init(row: Row) { - let decoder = RowDecoder( - row: row, - codingPath: [], - userInfo: Self.databaseDecodingUserInfo, - JSONDecoder: Self.databaseJSONDecoder) + let decoder = RowDecoder(row: row, codingPath: []) try! self.init(from: decoder) } } diff --git a/GRDB/Record/PersistableRecord+Encodable.swift b/GRDB/Record/PersistableRecord+Encodable.swift index 23b8a6033..9d4dcf890 100644 --- a/GRDB/Record/PersistableRecord+Encodable.swift +++ b/GRDB/Record/PersistableRecord+Encodable.swift @@ -13,12 +13,6 @@ private class RecordEncoder: Encoder { _persistenceContainer = PersistenceContainer() } - /// Helper method - @inline(__always) - fileprivate func encode(_ value: DatabaseValueConvertible?, forKey key: CodingKey) { - _persistenceContainer[key.stringValue] = value - } - func container(keyedBy type: Key.Type) -> KeyedEncodingContainer { let container = KeyedContainer(recordEncoder: self) return KeyedEncodingContainer(container) @@ -32,7 +26,13 @@ private class RecordEncoder: Encoder { fatalError("single value encoding is not supported") } - struct KeyedContainer : KeyedEncodingContainerProtocol { + /// Helper method + @inline(__always) + fileprivate func encode(_ value: DatabaseValueConvertible?, forKey key: CodingKey) { + _persistenceContainer[key.stringValue] = value + } + + private struct KeyedContainer : KeyedEncodingContainerProtocol { var recordEncoder: RecordEncoder var userInfo: [CodingUserInfoKey: Any] { return Record.databaseEncodingUserInfo }