Skip to content

Commit aed2b2a

Browse files
committed
Fix some warnings about encoding and decoding Data and Date types
1 parent 074a260 commit aed2b2a

File tree

2 files changed

+10
-32
lines changed

2 files changed

+10
-32
lines changed

Sources/Decoder/SingleValueDecodingContainer.swift

+6-27
Original file line numberDiff line numberDiff line change
@@ -231,35 +231,14 @@ extension _CBORDecoder.SingleValueContainer: SingleValueDecodingContainer {
231231
}
232232
}
233233

234-
func decode(_ type: Data.Type) throws -> Data {
235-
guard let cbor = try? CBOR.decode(self.data.map { $0 }) else {
236-
let context = DecodingError.Context(codingPath: self.codingPath, debugDescription: "Invalid format: \(self.data)")
237-
throw DecodingError.dataCorrupted(context)
238-
}
239-
switch cbor {
240-
case .byteString(let bytes): return Data(bytes)
241-
default:
242-
let context = DecodingError.Context(codingPath: self.codingPath, debugDescription: "Invalid format: \(self.data)")
243-
throw DecodingError.typeMismatch(Data.self, context)
244-
}
245-
}
246-
247234
func decode<T: Decodable>(_ type: T.Type) throws -> T {
248-
switch type {
249-
// TODO: These seem unnecessary/wrong?!
250-
case is Data.Type:
251-
return try decode(Data.self) as! T
252-
case is Date.Type:
253-
return try decode(Date.self) as! T
254-
default:
255-
let decoder = _CBORDecoder(data: self.data, options: self.options)
256-
let value = try T(from: decoder)
257-
if let nextIndex = decoder.container?.index {
258-
self.index = nextIndex
259-
}
260-
261-
return value
235+
let decoder = _CBORDecoder(data: self.data, options: self.options)
236+
let value = try T(from: decoder)
237+
if let nextIndex = decoder.container?.index {
238+
self.index = nextIndex
262239
}
240+
241+
return value
263242
}
264243
}
265244

Sources/Encoder/SingleValueEncodingContainer.swift

+4-5
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,14 @@ extension _CBOREncoder.SingleValueContainer: SingleValueEncodingContainer {
131131
self.storage.append(contentsOf: CBOR.encode(value))
132132
}
133133

134-
func encode(_ value: Date) throws {
134+
func encodeDate(_ value: Date) throws {
135135
try checkCanEncode(value: value)
136136
defer { self.canEncodeNewValue = false }
137137

138138
self.storage.append(contentsOf: CBOR.encodeDate(value, options: self.options.toCBOROptions()))
139139
}
140140

141-
func encode(_ value: Data) throws {
141+
func encodeData(_ value: Data) throws {
142142
try checkCanEncode(value: value)
143143
defer { self.canEncodeNewValue = false }
144144

@@ -151,15 +151,14 @@ extension _CBOREncoder.SingleValueContainer: SingleValueEncodingContainer {
151151

152152
switch value {
153153
case let data as Data:
154-
try self.encode(data)
154+
try self.encodeData(data)
155155
case let date as Date:
156-
try self.encode(date)
156+
try self.encodeDate(date)
157157
default:
158158
let encoder = _CBOREncoder(options: self.options)
159159
try value.encode(to: encoder)
160160
self.storage.append(encoder.data)
161161
}
162-
163162
}
164163
}
165164

0 commit comments

Comments
 (0)