From aed2b2ad7aac00eed5516e469936f4d351662ce2 Mon Sep 17 00:00:00 2001 From: Hamilton Chapman Date: Mon, 28 Oct 2024 17:27:39 +0000 Subject: [PATCH] Fix some warnings about encoding and decoding `Data` and `Date` types --- .../SingleValueDecodingContainer.swift | 33 ++++--------------- .../SingleValueEncodingContainer.swift | 9 +++-- 2 files changed, 10 insertions(+), 32 deletions(-) diff --git a/Sources/Decoder/SingleValueDecodingContainer.swift b/Sources/Decoder/SingleValueDecodingContainer.swift index 286be7a..f6b7446 100644 --- a/Sources/Decoder/SingleValueDecodingContainer.swift +++ b/Sources/Decoder/SingleValueDecodingContainer.swift @@ -231,35 +231,14 @@ extension _CBORDecoder.SingleValueContainer: SingleValueDecodingContainer { } } - func decode(_ type: Data.Type) throws -> Data { - guard let cbor = try? CBOR.decode(self.data.map { $0 }) else { - let context = DecodingError.Context(codingPath: self.codingPath, debugDescription: "Invalid format: \(self.data)") - throw DecodingError.dataCorrupted(context) - } - switch cbor { - case .byteString(let bytes): return Data(bytes) - default: - let context = DecodingError.Context(codingPath: self.codingPath, debugDescription: "Invalid format: \(self.data)") - throw DecodingError.typeMismatch(Data.self, context) - } - } - func decode(_ type: T.Type) throws -> T { - switch type { - // TODO: These seem unnecessary/wrong?! - case is Data.Type: - return try decode(Data.self) as! T - case is Date.Type: - return try decode(Date.self) as! T - default: - let decoder = _CBORDecoder(data: self.data, options: self.options) - let value = try T(from: decoder) - if let nextIndex = decoder.container?.index { - self.index = nextIndex - } - - return value + let decoder = _CBORDecoder(data: self.data, options: self.options) + let value = try T(from: decoder) + if let nextIndex = decoder.container?.index { + self.index = nextIndex } + + return value } } diff --git a/Sources/Encoder/SingleValueEncodingContainer.swift b/Sources/Encoder/SingleValueEncodingContainer.swift index a0b3dee..99503b4 100644 --- a/Sources/Encoder/SingleValueEncodingContainer.swift +++ b/Sources/Encoder/SingleValueEncodingContainer.swift @@ -131,14 +131,14 @@ extension _CBOREncoder.SingleValueContainer: SingleValueEncodingContainer { self.storage.append(contentsOf: CBOR.encode(value)) } - func encode(_ value: Date) throws { + func encodeDate(_ value: Date) throws { try checkCanEncode(value: value) defer { self.canEncodeNewValue = false } self.storage.append(contentsOf: CBOR.encodeDate(value, options: self.options.toCBOROptions())) } - func encode(_ value: Data) throws { + func encodeData(_ value: Data) throws { try checkCanEncode(value: value) defer { self.canEncodeNewValue = false } @@ -151,15 +151,14 @@ extension _CBOREncoder.SingleValueContainer: SingleValueEncodingContainer { switch value { case let data as Data: - try self.encode(data) + try self.encodeData(data) case let date as Date: - try self.encode(date) + try self.encodeDate(date) default: let encoder = _CBOREncoder(options: self.options) try value.encode(to: encoder) self.storage.append(encoder.data) } - } }