-
Notifications
You must be signed in to change notification settings - Fork 116
Mixed choice/non-choice encoding #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MaxDesiatov
merged 15 commits into
CoreOffice:master
from
bwetherfield:mixed-choice-encoding
Nov 27, 2019
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
869b58e
Add breaking case
bwetherfield afdc196
Add choice and keyed merging encode functionality
bwetherfield 2e2b843
Refactor
bwetherfield 40f9f42
Fix commented code
bwetherfield c335791
Fix misnamed file
bwetherfield 5d2aeb0
Fix xcode project
bwetherfield 8712a67
Fix precondition catch
bwetherfield 8ac3944
Use switch syntax
bwetherfield fd6ec42
Add multiple choice element case
bwetherfield 63f02cd
Add explicit types in KeyedBox initialization
bwetherfield a847719
Add explicitly empty parameter to KeyedBox initializer
bwetherfield d27e756
Use more concise type inference
bwetherfield 900a530
Unify switch syntax
bwetherfield f5c63ba
Cut down code duplication
bwetherfield 0a9681e
Fix formatting
bwetherfield File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // | ||
| // IntOrString.swift | ||
| // XMLCoderTests | ||
| // | ||
| // Created by Benjamin Wetherfield on 11/24/19. | ||
| // | ||
|
|
||
| import XMLCoder | ||
|
|
||
| internal enum IntOrString: Equatable { | ||
| case int(Int) | ||
| case string(String) | ||
| } | ||
|
|
||
| extension IntOrString: Codable { | ||
| enum CodingKeys: String, CodingKey { | ||
| case int | ||
| case string | ||
| } | ||
|
|
||
| func encode(to encoder: Encoder) throws { | ||
| var container = encoder.container(keyedBy: CodingKeys.self) | ||
| switch self { | ||
| case let .int(value): | ||
| try container.encode(value, forKey: .int) | ||
| case let .string(value): | ||
| try container.encode(value, forKey: .string) | ||
| } | ||
| } | ||
|
|
||
| init(from decoder: Decoder) throws { | ||
| let container = try decoder.container(keyedBy: CodingKeys.self) | ||
| do { | ||
| self = .int(try container.decode(Int.self, forKey: .int)) | ||
| } catch { | ||
| self = .string(try container.decode(String.self, forKey: .string)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension IntOrString.CodingKeys: XMLChoiceCodingKey {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // | ||
| // MixedChoiceAndNonChoiceTests.swift | ||
| // XMLCoderTests | ||
| // | ||
| // Created by Benjamin Wetherfield on 11/24/19. | ||
| // | ||
|
|
||
| import XCTest | ||
| import XMLCoder | ||
|
|
||
| private struct MixedIntOrStringFirst: Equatable { | ||
| let intOrString: IntOrString | ||
| let otherValue: String | ||
| } | ||
|
|
||
| extension MixedIntOrStringFirst: Encodable { | ||
| enum CodingKeys: String, CodingKey { | ||
| case otherValue = "other-value" | ||
| } | ||
|
|
||
| func encode(to encoder: Encoder) throws { | ||
| try intOrString.encode(to: encoder) | ||
| var container = encoder.container(keyedBy: CodingKeys.self) | ||
| try container.encode(otherValue, forKey: .otherValue) | ||
| } | ||
| } | ||
|
|
||
| private struct MixedOtherFirst: Equatable { | ||
| let intOrString: IntOrString | ||
| let otherValue: String | ||
| } | ||
|
|
||
| extension MixedOtherFirst: Encodable { | ||
| enum CodingKeys: String, CodingKey { | ||
| case otherValue = "other-value" | ||
| } | ||
|
|
||
| func encode(to encoder: Encoder) throws { | ||
| var container = encoder.container(keyedBy: CodingKeys.self) | ||
| try container.encode(otherValue, forKey: .otherValue) | ||
| try intOrString.encode(to: encoder) | ||
| } | ||
| } | ||
|
|
||
| private struct MixedEitherSide { | ||
| let leading: String | ||
| let intOrString: IntOrString | ||
| let trailing: String | ||
| } | ||
|
|
||
| extension MixedEitherSide: Encodable { | ||
| enum CodingKeys: String, CodingKey { | ||
| case leading | ||
| case trailing | ||
| } | ||
|
|
||
| func encode(to encoder: Encoder) throws { | ||
| var container = encoder.container(keyedBy: CodingKeys.self) | ||
| try container.encode(leading, forKey: .leading) | ||
| try intOrString.encode(to: encoder) | ||
| try container.encode(trailing, forKey: .trailing) | ||
| } | ||
| } | ||
|
|
||
| private struct TwoChoiceElements { | ||
| let first: IntOrString | ||
| let second: IntOrString | ||
| } | ||
|
|
||
| extension TwoChoiceElements: Encodable { | ||
| func encode(to encoder: Encoder) throws { | ||
| try first.encode(to: encoder) | ||
| try second.encode(to: encoder) | ||
| } | ||
| } | ||
|
|
||
| class MixedChoiceAndNonChoiceTests: XCTestCase { | ||
| func testMixedChoiceFirstEncode() throws { | ||
| let first = MixedIntOrStringFirst(intOrString: .int(4), otherValue: "other") | ||
| let firstEncoded = try XMLEncoder().encode(first, withRootKey: "container") | ||
| let firstExpectedXML = "<container><int>4</int><other-value>other</other-value></container>" | ||
| XCTAssertEqual(String(data: firstEncoded, encoding: .utf8), firstExpectedXML) | ||
| } | ||
|
|
||
| func testMixedChoiceSecondEncode() throws { | ||
| let second = MixedOtherFirst(intOrString: .int(4), otherValue: "other") | ||
| let secondEncoded = try XMLEncoder().encode(second, withRootKey: "container") | ||
| let secondExpectedXML = "<container><other-value>other</other-value><int>4</int></container>" | ||
| XCTAssertEqual(String(data: secondEncoded, encoding: .utf8), secondExpectedXML) | ||
| } | ||
|
|
||
| func testMixedChoiceFlankedEncode() throws { | ||
| let flanked = MixedEitherSide(leading: "first", intOrString: .string("then"), trailing: "second") | ||
| let flankedEncoded = try XMLEncoder().encode(flanked, withRootKey: "container") | ||
| let flankedExpectedXML = """ | ||
| <container><leading>first</leading><string>then</string><trailing>second</trailing></container> | ||
| """ | ||
| XCTAssertEqual(String(data: flankedEncoded, encoding: .utf8), flankedExpectedXML) | ||
| } | ||
|
|
||
| func testTwoChoiceElementsEncode() throws { | ||
| let twoChoiceElements = TwoChoiceElements(first: .int(1), second: .string("one")) | ||
| let encoded = try XMLEncoder().encode(twoChoiceElements, withRootKey: "container") | ||
| let expectedXML = "<container><int>1</int><string>one</string></container>" | ||
| XCTAssertEqual(String(data: encoded, encoding: .utf8), expectedXML) | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.