From ba3c938eb9beb8734fbf6c790c18e01b1664fc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Rou=C3=A9?= Date: Tue, 9 Jul 2024 13:32:06 +0200 Subject: [PATCH] Fix failing test for #1565 --- GRDB/Record/EncodableRecord+Encodable.swift | 83 ++++++++++++++++++--- 1 file changed, 71 insertions(+), 12 deletions(-) diff --git a/GRDB/Record/EncodableRecord+Encodable.swift b/GRDB/Record/EncodableRecord+Encodable.swift index c7816c6723..500a4814d4 100644 --- a/GRDB/Record/EncodableRecord+Encodable.swift +++ b/GRDB/Record/EncodableRecord+Encodable.swift @@ -34,18 +34,7 @@ private class RecordEncoder: Encoder { } func singleValueContainer() -> SingleValueEncodingContainer { - // @itaiferber on https://forums.swift.org/t/how-to-encode-objects-of-unknown-type/12253/11 - // - // > Encoding a value into a single-value container is equivalent to - // > encoding the value directly into the encoder, with the primary - // > difference being the above: encoding into the encoder writes the - // > contents of a type into the encoder, while encoding to a - // > single-value container gives the encoder a chance to intercept the - // > type as a whole. - // - // Wait for somebody hitting this fatal error so that we can write a - // meaningful regression test. - fatalError("single value encoding is not supported") + self } private struct KeyedContainer: KeyedEncodingContainerProtocol { @@ -169,6 +158,76 @@ private class RecordEncoder: Encoder { } } +extension RecordEncoder: SingleValueEncodingContainer { + private func unsupportedSingleValueEncoding() { + fatalError("Can't encode a single value in a database row.") + } + + func encodeNil() throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Bool) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: String) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Double) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Float) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Int) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Int8) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Int16) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Int32) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: Int64) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: UInt) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: UInt8) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: UInt16) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: UInt32) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: UInt64) throws { + unsupportedSingleValueEncoding() + } + + func encode(_ value: T) throws where T : Encodable { + try value.encode(to: self) + } +} + // MARK: - ColumnEncoder /// The encoder that encodes into a database column