-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Optionality updates to Codable API #10667
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
Optionality updates to Codable API #10667
Conversation
* Add encodeNil(forKey:)/encodeNil() for keyed/unkeyed encoding containers * Add decodeNil(forKey:)/decodeNil() for keyed/unkeyed decoding containers * Give default implementation of decodeIfPresent(forKey:)/ decodeIfPresent(_:) on keyed/unkeyed decoding containers instead of decode(forKey:)/decode(_:) * Expose all encode/decode methods on keyed encoding & decoding containers to allow overriding default methods (which were previously not forwarding)
* Add encodeNil/decodeNil variants on keyed and unkeyed containers * Give implementations of decode() variants instead of decodeIfPresent()
* Add encodeNil/decodeNil variants on keyed and unkeyed containers * Give implementations of decode() variants instead of decodeIfPresent()
To make it slightly easier to tell at a glance if a method is implemented as part of API (public/open), shared implementation detail (fileprivate), or implementation detail meaningful only to a given type (private).
* Update {JSON,PropertyList}Decoder to allow superDecoder()s to wrap null value so current implementations of collections can decode contained objects from nil
* Eliminate null checks from unboxing in the general case (so types can at least attempt to decode from nil) * Add unit tests to confirm this behavior
@swift-ci Please test |
This change will affect those who have already written |
Build failed |
This will need the corresponding changes made to |
Applies swift-corelibs-foundation changes to mirror swiftlang/swift/pull/10667.
swiftlang/swift-corelibs-foundation#1087 |
Build failed |
Applies swift-corelibs-foundation changes to mirror swiftlang/swift/pull/10667.
Missed a |
swiftlang/swift-corelibs-foundation#1087 |
Build failed |
Applies swift-corelibs-foundation changes to mirror swiftlang/swift/pull/10667.
Build failed |
Applies swift-corelibs-foundation changes to mirror swiftlang/swift/pull/10667.
swiftlang/swift-corelibs-foundation#1087 |
Build failed |
Applies swift-corelibs-foundation changes to mirror swiftlang/swift/pull/10667.
swiftlang/swift-corelibs-foundation#1087 |
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.
Looks good to me.
// EnhancedBool is a type which encodes either as a Bool or as nil. | ||
_testEncodeFailure(of: EnhancedBool.true) | ||
_testEncodeFailure(of: EnhancedBool.false) | ||
_testEncodeFailure(of: EnhancedBool.fileNotFound) |
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.
👍 on test case
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.
Hehe, I told @phausler I was glad to finally be able to use this somewhere in real code.
What's in this pull request?
As laid out in a proposed update to SE-0166 and SE-0167, there are optionality updates that we wanted to introduce to the Codable API. This implements the remainder of those updates which had not yet been implemented:
encodeNil(forKey:)
anddecodeNil(forKey:)
have been added to keyed containersencodeNil()
anddecodeNil()
have been added to unkeyed containersdecode(...)
in terms ofdecodeIfPresent(...)
on all containers has been reversed: withdecodeNil(...)
in place, a default implementation ofdecodeIfPresent(...)
is now given on all containers, anddecode(...)
must be implemented insteadKeyedEncodingContainer
andKeyedDecodingContainer
so it should be possible to give actual implementations instead of calling into the defaultsThis is a long PR, but going through commit by commit should help. The first commit introduces these changes to the protocols; the second updates
JSONEncoder
andJSONDecoder
; the third provides the same changes onPropertyListEncoder
andPropertyListDecoder
; etc.This adds unit tests to confirm that decoding from null values works as expected.