Skip to content

Commit

Permalink
Fix failing test for #1565
Browse files Browse the repository at this point in the history
  • Loading branch information
groue committed Jul 9, 2024
1 parent 5999227 commit ba3c938
Showing 1 changed file with 71 additions and 12 deletions.
83 changes: 71 additions & 12 deletions GRDB/Record/EncodableRecord+Encodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ private class RecordEncoder<Record: EncodableRecord>: 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<Key: CodingKey>: KeyedEncodingContainerProtocol {
Expand Down Expand Up @@ -169,6 +158,76 @@ private class RecordEncoder<Record: EncodableRecord>: 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<T>(_ value: T) throws where T : Encodable {
try value.encode(to: self)
}
}

// MARK: - ColumnEncoder

/// The encoder that encodes into a database column
Expand Down

0 comments on commit ba3c938

Please sign in to comment.