-
Notifications
You must be signed in to change notification settings - Fork 82
Test cases for LosslessValue Bool Int value #28
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
Test cases for LosslessValue Bool Int value #28
Conversation
|
Thanks for this! Something similar to this came up in a conversation today, so this is good timing 😄. I'll try to find some time this week to noodle on this. |
|
Anytime! If I can contribute more let me know. I'll maybe add my proposed solutions to this PR as an example with test cases so we can have an idea how it would work in reality. |
|
@serjooo thanks for the new commits! The only thing that has me hesitating is that NSJSONSerialization has historically treated |
|
Having slept on it I think your solution of the container extension is the best way to go. It's the least surprising result and arguably programmer intended if this code path is ever executed. I'll take a closer look today before merging it in—thanks again! 👍 |
|
@marksands don't mention it. It is my pleasure to be able to contribute! I was thinking about this whole day at work as well and I was just about to start with a proposed solution for the I think what you mentioned is very valid, but what is mind boggling me is why did the Swift team not take care of this directly in the Swift standard library. They only check that the value references the One thing I also thought of today where implementing it in the container extension is advantageous over the Let me know what you decide. We can also continue having the discussion and come up with new ideas! |
|
I think we can benefit from both solutions. I pushed a potential solve for the LosslessValue decoding, let me know what you think. I'll add some minor comments in a review as well. |
…terCodable into LosslessValue-Bool-Int-Value
|
@marksands there is still one more test case we haven't covered yet, and not sure if we should. Would love to hear your opinion. It is when the data for both |
I don't think this is an edge case I'm too worried about. I think at that point |
Currently, the
LosslessValuePropertyWrapper saves you the trouble of managing the inconsistencies of the server sending different types for one expected type and it does it well. However, it doesn't cover all the cases. It misses the case where aBoolvalue might be sent as anIntvalue, instead of aString. This PR shows the failure ofLosslessValuewhen you attempt to decode a Bool as an Int by adding test cases for the scenario.Expected Behavior
The
LosslessValuePropertyWrapper to successfully decode a Bool value from an Int 0/1 value.Current Behavior
The
LosslessValuePropertyWrapper throws atypeMismatchErroras Bool can not be constructed from a number string "0"/"1"Possible Solution
This PR is to discuss possible solutions as it can be done many ways. Two possible solutions might be:
First solution can be that we add explicit code in
LosslessValuePropertyWrapper to handle the Bool being constructed from an Int value. Problem with this solution is that it seems a bit hacky to explicitly handle the bool value case decoding in a more general PropertyWrapper like theLosslessValue.Second solution that might also be desirable is to write a
KeyedDecodingContainerextension for when theDecodabletype isDefaultFalsePropertyWrapper and handle the specific scenario in that extension. If everything fails while attempting to decode successfully, we default to the default value. Problem with this solution is that if someone were to add aDefaultTruePropertyWrapper the code would have to be duplicated.Steps to Reproduce
Run tests added in this PR