Mixed choice/non-choice decoding#155
Merged
MaxDesiatov merged 2 commits intoCoreOffice:masterfrom Dec 1, 2019
Merged
Conversation
arjungupta0107
pushed a commit
to salido/XMLCoder
that referenced
this pull request
Jun 26, 2020
Mirrors CoreOffice#154 Fixes bugs 1) Decoding multiple choice elements in the same Keyed Container. 2) Decoding choice elements after decoding regular keyed elements in the same container. Case 1 refers to decoding implementations of the form: ```swift init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) otherValue = try container.decode(String.self, forKey: .otherValue) intOrString = try IntOrString(from: decoder) } ``` where `IntOrString` is a choice coding element `IntOrString` defined as follows: ```swift enum IntOrString { case int(Int) case string(String) } extension IntOrString: Decodable { enum CodingKeys: String, CodingKey { case int case string } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) if container.contains(.int) { self = .int(try container.decode(Int.self, forKey: .int)) } else { self = .string(try container.decode(String.self, forKey: .string)) } } } extension IntOrString.CodingKeys: XMLChoiceCodingKey {} // signifies that `IntOrString` is a choice element ``` Case 2 refers to decoding implementations of the following form: ```swift init(from decoder: Decoder) throws { self.first = try IntOrString(from: decoder) self.second = try AlternateIntOrString(from: decoder) } ``` Where both `first: IntOrString` and `second: AlternateIntOrString` are choice elements. `AlternateIntOrString` defined as follows: ```swift private enum AlternateIntOrString { case alternateInt(Int) case alternateString(String) } extension AlternateIntOrString: Decodable { enum CodingKeys: String, CodingKey { case alternateInt = "alternate-int" case alternateString = "alternate-string" } init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) if container.contains(.alternateInt) { self = .alternateInt(try container.decode(Int.self, forKey: .alternateInt)) } else { self = .alternateString(try container.decode(String.self, forKey: .alternateString)) } } } extension AlternateIntOrString.CodingKeys: XMLChoiceCodingKey {} // signifies that `AlternateIntOrString` is a choice element ``` * Add tests * Fix failing tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Mirrors #154
Fixes bugs
Case 1 refers to decoding implementations of the form:
where
IntOrStringis a choice coding element(
IntOrStringdefined as follows)Case 2 refers to decoding implementations of the following form:
Where both
first: IntOrStringandsecond: AlternateIntOrStringare choice elements.(
AlternateIntOrStringdefined as follows)