Skip to content

Commit 4ce74e2

Browse files
bwetherfieldMaxDesiatov
authored andcommitted
Decoding choice elements that can hold empty structs (#120)
## Introduction In merging in #119, we fixed most but not quite all of #91! Decoding of _null_ choice elements (represented as enums with empty struct associated values on the Swift side) still results in errors. This PR adds a test to demonstrate and fixes this issue by wrapping each `NullBox` inside of a `SingleKeyedBox` at the `merge` phase (called by `transformToBoxTree`). ## Motivation One of the main lessons from #119 was that we have to wrap choice elements in the decoding phase to hold onto their keys. The keys are needed for directing us to the correct branch of the do-catch pyramid used for decoding. ```swift private enum Entry: Equatable { case run(Run) case properties(Properties) case br(Break) } extension Entry: Decodable { private enum CodingKeys: String, XMLChoiceCodingKey { case run, properties, br } public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) do { self = .run(try container.decode(Run.self, forKey: .run)) } catch { do { self = .properties(try container.decode(Properties.self, forKey: .properties)) } catch { self = .br(try container.decode(Break.self, forKey: .br)) } } } } ``` where one of the associated values could be an empty struct (represented by null): ```swift private struct Break: Decodable {} ``` Although we _can_ throw out keys for non-choice null elements, a mechanism is needed for holding onto the keys while transforming from the `XMLCoderElement` tree to the `boxTree`. Only later will we know if the key is needed (if this wrapped element is transformed to a `ChoiceBox`); if not, we will be able to throw out the key. ## Proposed solution The Public API is unchanged. On the implementation side, we catch `NullBox` values in `merge` and wrap them in `SingleKeyedBox` instances. ## Detailed Design In `merge`, we wrap each `NullBox` in a `SingleKeyedBox` with the appropriate key bundled in. An `XMLChoiceDecodingContainer` can be constructed from the `SingleKeyedBox` by converting it to a `ChoiceBox` (just transferring over the contents) - as normal. In `XMLKeyedDecodingContainer`, when preparing the `elements` for concrete decoding, we unwrap all `SingleKeyedBox` values that may be contained therein, as any choice elements contained would have already been transformed to a `ChoiceBox` by this point in decoding: any stray `SingleKeyedBox` wrappers can thus be thrown out. ## Source compatibility This is purely an additive change.
1 parent ab9fef0 commit 4ce74e2

3 files changed

Lines changed: 63 additions & 3 deletions

File tree

Sources/XMLCoder/Auxiliaries/KeyedStorage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ extension KeyedStorage where Key == String, Value == Box {
8383
} else if let value = element.value {
8484
result.append(StringBox(value), at: element.key)
8585
} else {
86-
result.append(NullBox(), at: element.key)
86+
result.append(SingleKeyedBox(key: element.key, element: NullBox()), at: element.key)
8787
}
8888

8989
return result

Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ struct XMLKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
7979

8080
let box = elements.first ?? attributes.first
8181

82+
if let singleKeyed = box as? SingleKeyedBox {
83+
return singleKeyed.element.isNull
84+
}
85+
8286
return box?.isNull ?? true
8387
}
8488

@@ -232,14 +236,26 @@ extension XMLKeyedDecodingContainer {
232236
let keyString = key.stringValue.isEmpty ? "value" : key.stringValue
233237
let value = keyedBox.elements[keyString]
234238
if !value.isEmpty {
235-
return value
239+
return value.map {
240+
if let singleKeyed = $0 as? SingleKeyedBox {
241+
return singleKeyed.element
242+
} else {
243+
return $0
244+
}
245+
}
236246
} else if let value = keyedBox.value {
237247
return [value]
238248
} else {
239249
return []
240250
}
241251
} else {
242-
return keyedBox.elements[key.stringValue]
252+
return keyedBox.elements[key.stringValue].map {
253+
if let singleKeyed = $0 as? SingleKeyedBox {
254+
return singleKeyed.element
255+
} else {
256+
return $0
257+
}
258+
}
243259
}
244260
}
245261

Tests/XMLCoderTests/NestedChoiceTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,50 @@ class NestedChoiceTests: XCTestCase {
227227
XCTAssertEqual(result, expected)
228228
}
229229

230+
func testNestedEnumsWithEmptyStruct() throws {
231+
let xml = """
232+
<container>
233+
<p>
234+
<br></br>
235+
<run>
236+
<id>1518</id>
237+
<text>I am answering it again.</text>
238+
</run>
239+
<properties>
240+
<id>431</id>
241+
<title>A Word About Wake Times</title>
242+
</properties>
243+
</p>
244+
<p>
245+
<run>
246+
<id>1519</id>
247+
<text>I am answering it again.</text>
248+
</run>
249+
<br />
250+
</p>
251+
</container>
252+
"""
253+
let result = try XMLDecoder().decode(Container.self, from: xml.data(using: .utf8)!)
254+
let expected = Container(
255+
paragraphs: [
256+
Paragraph(
257+
entries: [
258+
.br(Break()),
259+
.run(Run(id: 1518, text: "I am answering it again.")),
260+
.properties(Properties(id: 431, title: "A Word About Wake Times")),
261+
]
262+
),
263+
Paragraph(
264+
entries: [
265+
.run(Run(id: 1519, text: "I am answering it again.")),
266+
.br(Break()),
267+
]
268+
),
269+
]
270+
)
271+
XCTAssertEqual(result, expected)
272+
}
273+
230274
func testNestedEnumsRoundTrip() throws {
231275
let original = Container(
232276
paragraphs: [

0 commit comments

Comments
 (0)