-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Enhancements to Codable API #9791
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
Enhancements to Codable API #9791
Conversation
|
@swift-ci Please smoke test |
|
I don't think we want to take the unconditional conformance of Optional to Codable. Someone could accidentally depend on that in a case where the conditional conformance would said no, and then us changing it would break source compatibility. Please continue to special-case Optional. |
|
@jrose-apple The same case can be made for Encoding/decoding will fail at runtime (so you can't actually encode a non- |
|
I don't think it's correct for Array, Set, or Dictionary either, and I'm sorry I didn't catch that the first time around. That still doesn't mean you can introduce a pitfall like this, though. It's much easier to add the API that handles these wrapping types, and then later (or even now) provide a default implementation for them. |
|
@jrose-apple This is a stop-gap until conditional conformance lands. We can't prevent people from encoding arrays, sets, dictionaries, and optionals. If we add a combinatorial explosion of overloads supporting these types, we cannot remove them later, nor can we handle recursive types with them. Let's take this to email though. Please CC @parkera too. |
|
@swift-ci test |
|
Build failed |
|
Build failed |
* Adds conformance of Optional to Codable * encode(...) arguments are no longer Optional; Optional values go through generic version * encodeIfPresent added to KeyedEncodingContainerProtocol to mirror decodeIfPresent * JSONEncoder and PropertyListEncoder updated to reflect these changes
Optional, Array, Set, and Dictionary currently all conform to Codable regardless of the type they are generic on. Until conditional conformance lands and we can rely on their conditional conformance, we want to prevent Codable derived conformance if a type contains a property of one of these types when we know it's actually going to fail at runtime.
Optional properties now get synthesized encodeIfPresent(...) and decodeIfPresent(...) calls (to avoid encoding needless information, and to be more accepting on input).
2dd134f to
26d0d5d
Compare
Following feedback in swiftlang#9758: * Use a struct type for the result of hasValidCodingKeysEnum * Use an enum type for the result of * Skip fetching a canonical type before calling getAnyNominal() (getAnyNominal() looks through sugar already) * No need to cast to IterableDeclContext (or DeclContext, even)
|
@swift-ci Please test |
|
Build failed |
|
Build failed |
Since implicitly unwrapped optional types do not reflect conformances added to Optional, we need to explicitly allow and handle them.
|
@swift-ci Please test |
|
Build failed |
|
Build failed |
What's in this pull request?
OptionaltoCodableencode(...)arguments are no longerOptional;Optionalvalues go through generic versionencodeIfPresentadded toKeyedEncodingContainerProtocolto mirrordecodeIfPresentOptionalvalues synthesizeencodeIfPresent(...)anddecodeIfPresent(...)calls instead ofencode(...)anddecode(...)encodeNil()anddecodeNil()added as proper primitives onSingleValueEncodingContainerandSingleValueDecodingContainer, as well asencode<T : Encodable>(_: T)anddecode<T : Decodable>(_: T) throws -> TJSONEncoderandPropertyListEncoderupdated to reflect these changes