Skip to content

Commit 3c4ace6

Browse files
committed
Working on Coding Key
1 parent 7c811a2 commit 3c4ace6

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

Sources/Decoder.swift

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -194,28 +194,14 @@ internal extension TLVDecoder.Decoder {
194194

195195
func typeCode <Key: CodingKey> (for key: Key) throws -> TLVTypeCode {
196196

197-
if let tlvCodingKey = key as? TLVCodingKey {
198-
199-
return tlvCodingKey.code
200-
201-
} else if let intValue = key.intValue {
202-
203-
guard intValue <= Int(UInt8.max),
204-
intValue >= Int(UInt8.min) else {
205-
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has an invalid integer value \(intValue)"))
197+
guard let typeCode = TLVTypeCode(codingKey: key) else {
198+
if let intValue = key.intValue {
199+
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has an invalid integer value \(intValue)"))
200+
} else {
201+
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has no integer value"))
206202
}
207-
208-
return TLVTypeCode(rawValue: UInt8(intValue))
209-
210-
} else if MemoryLayout<Key>.size == MemoryLayout<UInt8>.size,
211-
Mirror(reflecting: key).displayStyle == .enum {
212-
213-
return TLVTypeCode(rawValue: unsafeBitCast(key, to: UInt8.self))
214-
215-
} else {
216-
217-
throw DecodingError.keyNotFound(key, DecodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has no integer value"))
218203
}
204+
return typeCode
219205
}
220206
}
221207

Sources/Encoder.swift

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,29 +135,14 @@ internal extension TLVEncoder.Encoder {
135135

136136
func typeCode <Key: CodingKey, T> (for key: Key, value: T) throws -> TLVTypeCode {
137137

138-
if let tlvCodingKey = key as? TLVCodingKey {
139-
140-
return tlvCodingKey.code
141-
142-
} else if let intValue = key.intValue {
143-
144-
guard intValue <= Int(UInt8.max),
145-
intValue >= Int(UInt8.min) else {
146-
147-
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has an invalid integer value \(intValue)"))
138+
guard let typeCode = TLVTypeCode(codingKey: key) else {
139+
if let intValue = key.intValue {
140+
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has an invalid integer value \(intValue)"))
141+
} else {
142+
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has no integer value"))
148143
}
149-
150-
return TLVTypeCode(rawValue: UInt8(intValue))
151-
152-
} else if MemoryLayout<Key>.size == MemoryLayout<UInt8>.size,
153-
Mirror(reflecting: key).displayStyle == .enum {
154-
155-
return TLVTypeCode(rawValue: unsafeBitCast(key, to: UInt8.self))
156-
157-
} else {
158-
159-
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: "Coding key \(key) has no integer value"))
160144
}
145+
return typeCode
161146
}
162147
}
163148

0 commit comments

Comments
 (0)