Decoding choice elements that can hold empty structs #120
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 aSingleKeyedBox
at themerge
phase (called bytransformToBoxTree
).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.
where one of the associated values could be an empty struct (represented by null):
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 theboxTree
. Only later will we know if the key is needed (if this wrapped element is transformed to aChoiceBox
); 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 inmerge
and wrap them inSingleKeyedBox
instances.Detailed Design
In
merge
, we wrap eachNullBox
in aSingleKeyedBox
with the appropriate key bundled in. AnXMLChoiceDecodingContainer
can be constructed from theSingleKeyedBox
by converting it to aChoiceBox
(just transferring over the contents) - as normal. InXMLKeyedDecodingContainer
, when preparing theelements
for concrete decoding, we unwrap allSingleKeyedBox
values that may be contained therein, as any choice elements contained would have already been transformed to aChoiceBox
by this point in decoding: any straySingleKeyedBox
wrappers can thus be thrown out.Source compatibility
This is purely an additive change.