Skip to content

Commit

Permalink
Refactor to remove duplicated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Pilkington committed Oct 15, 2018
1 parent 940c429 commit 2ef05f8
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions Sources/XMLCoding/Decoder/XMLDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ internal class _XMLDecoder : Decoder {

// MARK: - Decoder Methods

private func getMapFromListOfEntries(entryList: [Any], keyTag: String, valueTag: String) -> [String : Any] {
var newContainer: [String: Any] = [:]

// construct a dictionary from each entry and the key and value tags
entryList.forEach { entry in
if let keyedContainer = entry as? [String : Any],
let key = keyedContainer[keyTag] as? String,
let value = keyedContainer[valueTag] {
newContainer[key] = value
}
}

return newContainer
}

public func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> {
guard !(self.storage.topContainer is NSNull) else {
throw DecodingError.valueNotFound(KeyedDecodingContainer<Key>.self,
Expand All @@ -254,36 +269,14 @@ internal class _XMLDecoder : Decoder {
if case let .collapseListUsingItemTag(itemTag) = options.listDecodingStrategy,
case let .collapseMapUsingTags(keyTag: keyTag, valueTag: valueTag) = options.mapDecodingStrategy,
let itemList = currentContainer[itemTag] as? [Any] {
var newContainer: [String: Any] = [:]

// construct a dictionary from each entry and the key and value tags
itemList.forEach { entry in
if let keyedContainer = entry as? [String : Any],
let key = keyedContainer[keyTag] as? String,
let value = keyedContainer[valueTag] {
newContainer[key] = value
}
}

topContainer = newContainer
topContainer = getMapFromListOfEntries(entryList: itemList, keyTag: keyTag, valueTag: valueTag)
} else {
topContainer = currentContainer
}
// if this is a list and the mapDecodingStrategy is collapseMapUsingTags
} else if let currentContainer = self.storage.topContainer as? [Any],
case let .collapseMapUsingTags(keyTag: keyTag, valueTag: valueTag) = options.mapDecodingStrategy {
var newContainer: [String: Any] = [:]

// construct a dictionary from each entry and the key and value tags
currentContainer.forEach { entry in
if let keyedContainer = entry as? [String : Any],
let key = keyedContainer[keyTag] as? String,
let value = keyedContainer[valueTag] {
newContainer[key] = value
}
}

topContainer = newContainer
topContainer = getMapFromListOfEntries(entryList: currentContainer, keyTag: keyTag, valueTag: valueTag)
} else {
throw DecodingError._typeMismatch(at: self.codingPath, expectation: [String : Any].self, reality: self.storage.topContainer)
}
Expand Down

0 comments on commit 2ef05f8

Please sign in to comment.