Skip to content

Commit 2ef05f8

Browse files
author
Simon Pilkington
committed
Refactor to remove duplicated code.
1 parent 940c429 commit 2ef05f8

1 file changed

Lines changed: 17 additions & 24 deletions

File tree

Sources/XMLCoding/Decoder/XMLDecoder.swift

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,21 @@ internal class _XMLDecoder : Decoder {
239239

240240
// MARK: - Decoder Methods
241241

242+
private func getMapFromListOfEntries(entryList: [Any], keyTag: String, valueTag: String) -> [String : Any] {
243+
var newContainer: [String: Any] = [:]
244+
245+
// construct a dictionary from each entry and the key and value tags
246+
entryList.forEach { entry in
247+
if let keyedContainer = entry as? [String : Any],
248+
let key = keyedContainer[keyTag] as? String,
249+
let value = keyedContainer[valueTag] {
250+
newContainer[key] = value
251+
}
252+
}
253+
254+
return newContainer
255+
}
256+
242257
public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
243258
guard !(self.storage.topContainer is NSNull) else {
244259
throw DecodingError.valueNotFound(KeyedDecodingContainer<Key>.self,
@@ -254,36 +269,14 @@ internal class _XMLDecoder : Decoder {
254269
if case let .collapseListUsingItemTag(itemTag) = options.listDecodingStrategy,
255270
case let .collapseMapUsingTags(keyTag: keyTag, valueTag: valueTag) = options.mapDecodingStrategy,
256271
let itemList = currentContainer[itemTag] as? [Any] {
257-
var newContainer: [String: Any] = [:]
258-
259-
// construct a dictionary from each entry and the key and value tags
260-
itemList.forEach { entry in
261-
if let keyedContainer = entry as? [String : Any],
262-
let key = keyedContainer[keyTag] as? String,
263-
let value = keyedContainer[valueTag] {
264-
newContainer[key] = value
265-
}
266-
}
267-
268-
topContainer = newContainer
272+
topContainer = getMapFromListOfEntries(entryList: itemList, keyTag: keyTag, valueTag: valueTag)
269273
} else {
270274
topContainer = currentContainer
271275
}
272276
// if this is a list and the mapDecodingStrategy is collapseMapUsingTags
273277
} else if let currentContainer = self.storage.topContainer as? [Any],
274278
case let .collapseMapUsingTags(keyTag: keyTag, valueTag: valueTag) = options.mapDecodingStrategy {
275-
var newContainer: [String: Any] = [:]
276-
277-
// construct a dictionary from each entry and the key and value tags
278-
currentContainer.forEach { entry in
279-
if let keyedContainer = entry as? [String : Any],
280-
let key = keyedContainer[keyTag] as? String,
281-
let value = keyedContainer[valueTag] {
282-
newContainer[key] = value
283-
}
284-
}
285-
286-
topContainer = newContainer
279+
topContainer = getMapFromListOfEntries(entryList: currentContainer, keyTag: keyTag, valueTag: valueTag)
287280
} else {
288281
throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: self.storage.topContainer)
289282
}

0 commit comments

Comments
 (0)