Skip to content

Commit

Permalink
Merge pull request CoreOffice#52 from jsbean/tidy-singlekeyed
Browse files Browse the repository at this point in the history
Refactor XMLKeyedDecodingContainer.decodeConcrete elements massaging
  • Loading branch information
jsbean committed Oct 1, 2019
2 parents 6a07cba + 6805ec6 commit f2cac43
Showing 1 changed file with 12 additions and 30 deletions.
42 changes: 12 additions & 30 deletions Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,37 +235,19 @@ extension XMLKeyedDecodingContainer {
)
}

let elements = container
.withShared { keyedBox -> [KeyedBox.Element] in
if ["value", ""].contains(key.stringValue) {
let keyString = key.stringValue.isEmpty ? "value" : key.stringValue
let value = keyedBox.elements[keyString]
if !value.isEmpty {
return value.map {
if let singleKeyed = $0 as? SingleKeyedBox {
return singleKeyed.element
} else {
return $0
}
}
} else if let value = keyedBox.value {
return [value]
} else {
return []
}
} else {
#warning("TODO: just return keyedBox.elements[key.stringValue]")
return keyedBox.elements[key.stringValue].map {
if let singleKeyed = $0 as? SingleKeyedBox {
#warning("Don't get rid of key info just yet!")
// return singleKeyed.element
return singleKeyed
} else {
return $0
}
}
}
let elements = container.withShared { keyedBox -> [KeyedBox.Element] in
// Handle the coding key value intrinsic case
if ["value", ""].contains(key.stringValue) {
// For each boxed-up element we have, peer in to see if it is a `SingleKeyedBox`.
// A `SingleKeyedBox` bundles an `element` along with its `key` in order to defer
// its evaluation. This is used here in order to prevent the wiping away of
// `NullBox` elements prematurely which may be an empty `String` value in disguise.
// If we have a `SingledKeyedBox` here, extract its element. Otherwise, return the
// boxed-up element as-is.
return keyedBox.elements["value"].map { ($0 as? SingleKeyedBox)?.element ?? $0 }
}
return keyedBox.elements[key.stringValue]
}

let attributes = container.withShared { keyedBox in
keyedBox.attributes[key.stringValue]
Expand Down

0 comments on commit f2cac43

Please sign in to comment.