-
Notifications
You must be signed in to change notification settings - Fork 80
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
LossyDictionary
: added failing test and fix for key conversion issue
#51
Merged
Merged
Changes from all commits
Commits
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 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 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 |
---|---|---|
|
@@ -75,4 +75,27 @@ class LossyDictionaryTests: XCTestCase { | |
XCTAssertEqual(fixture.stringToInt, ["one": 1, "two": 2, "three": 3]) | ||
XCTAssertEqual(fixture.intToString, [1: "one", 2: "two", 3: "three"]) | ||
} | ||
|
||
func testEncodingLosslessDictionaryRetainsKeys() throws { | ||
let decoder = JSONDecoder() | ||
decoder.keyDecodingStrategy = .convertFromSnakeCase | ||
|
||
let encoder = JSONEncoder() | ||
encoder.keyEncodingStrategy = .convertToSnakeCase | ||
|
||
let fixture = Fixture( | ||
stringToInt: [ | ||
"snake_case.with.dots_99.and_numbers": 1, | ||
"dots.and_2.00.1_numbers": 2, | ||
"key.1": 3, | ||
"normal key": 4, | ||
"another_key": 5 | ||
], | ||
intToString: [:] | ||
) | ||
|
||
let reencodedFixture = try decoder.decode(Fixture.self, from: encoder.encode(fixture)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encode and decode to ensure that |
||
|
||
XCTAssertEqual(reencodedFixture, fixture) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand why this is necessary, but how robust is it? Can you think of a scenario where the sorting would be different when converting from snake case? I'd hate for there to be an edge case that's missed.
I think the answer is "no", but is it possible to route through this only when the key strategy isn't the default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't think of a way, but I agree it's not ideal.
Unfortunately the
Decoder
protocol doesn't expose that. I went through the whole Swift implementation to understand how it uses private types in a way that we can't.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the due diligence. I'll play around with this tonight and try and button up some other in flight work.