-
Notifications
You must be signed in to change notification settings - Fork 79
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
Adds a @LossyOptional property wrapper #30
Conversation
I don't think |
@marksands Okay yeah, I see what you mean. I like |
I like It also might be worth adding a test that demonstrates the Lossy problem outside of URL types: func testDecodingWithBadIntegerConversion() throws {
struct Fixture: Codable {
@LossyOptional var a: Int?
@LossyOptional var b: Int?
}
let jsonData = #"{ "a": 3.14, "b": 3 }"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertNil(fixture.a)
XCTAssertEqual(fixture.b, 3)
} |
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.
See also the comment I left about adding an additional fixture with another type(s).
@marksands Thanks for the feedback! Changes addressed, and original PR description updated with name change 😃 |
Thank you! 🙇 |
When decoding an
Optional
type, it is still possible for decoding to fail and throw an error, rather than falling back to anil
value. This PR addresses that issue by adding a@LossyOptional
property wrapper, which is just atypealias
of@DefaultCodable
using a newDefaultNilStrategy
. Possible README usage description below:@LossyOptional
Optional codable values can be frustrating in that, if decoding fails for any reason, we are often given an error instead of a
nil
value, like we might expect.@LossyOptional
fixes this annoyance by defaulting decoded Optionals to nil if the Decoder is unable to decode the value.Usage
Without the
@LossyOptional
property wrapper here, we would get aDecodingError.dataCorrupted(_:)
error instead.