-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Added protocol CodingKeyRepresentable #34458
Added protocol CodingKeyRepresentable #34458
Conversation
…at you would like to use as keys for keyed containers
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.
The new and old behaviors will need to be tested somehow.
Co-authored-by: Ben Rimmington <[email protected]>
Co-authored-by: Ben Rimmington <[email protected]>
Co-authored-by: Ben Rimmington <[email protected]>
This comment has been minimized.
This comment has been minimized.
Will update debugDescription in next commit. Co-authored-by: Ben Rimmington <[email protected]>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
You can try the following, from within the utils/run-test \
--build-dir=../build/Ninja-RelWithDebInfoAssert \
--build=true \
--subset=all \
--target=macosx-x86_64 \
--verbose \
test/stdlib/CodableTests.swift (The exact |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…dingKeyRepresentable. Incorporated feedback from Ben Rimmington: _DictionaryCodingKey initializer made non-failable, Added default implementations of CodingKeyRepresentable for RawRepresentable, String and Int.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Co-authored-by: Ben Rimmington <[email protected]>
…sen/swift into stringkeycodable
This comment has been minimized.
This comment has been minimized.
@mortenbekditlevsen You could update the comment at the top of this page, with a link to the SE-0320 proposal. I don't know who the "code owner" is, but I've requested reviews from:
|
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 asking me to review, and for adding & updating the doc comments! Suggested some minor changes and left a question.
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 making the doc changes — I'll mark this as "approve" even though I asked you to add a comma. With that last change, these look good to me.
Co-authored-by: Alex Martini <[email protected]>
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.
Some lines are over the 80-character limit.
(I didn't attempt to reformat the CodableTests.swift file.)
Co-authored-by: Ben Rimmington <[email protected]>
Co-authored-by: Ben Rimmington <[email protected]>
Co-authored-by: Ben Rimmington <[email protected]>
Co-authored-by: Ben Rimmington <[email protected]>
Thank you @benrimmington , |
Apologies for bumping this: is there anything else I can do for now? I am not familiar with the usual process after a proposal has been accepted. @lorentey @tbkka Out of curiosity: I have not seen any plans for the next Swift version, but I assume that it will be 5.6. Are you familiar with any timeline for the next release? |
@swift-ci Please smoke test |
The next version is Swift 5.6, but I haven't seen an announcement yet. Based on the table of previous announcements, a |
Hi @drexin , |
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!
@swift-ci test windows |
@swift-ci smoke test |
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.
The mangledName
is now required: #39846
Co-authored-by: Ben Rimmington <[email protected]>
Co-authored-by: Ben Rimmington <[email protected]>
Thank you @benrimmington |
@swift-ci smoke test |
Yaaaaaay! Thank you so much to everyone who helped! 😊🥰🎉 |
This PR contains an implementation for the accepted Swift Evolution proposal: SE-0320.
The current
Codable
system has an issue that onlyString
andInt
can be used as keys in keyed containers.This has been discussed on various topics on the Swift Evolution forums <TODO - find references>. I believe that it has been mentioned by Itai Ferber that it was an early oversight that types that are
RawRepresentable
asString
orInt
could not be used as keys <TODO - find reference>.It is unfortunately too late to fix this now. For one it is a behavior breaking change - and furthermore the change is tied to the Swift stdlib, so the behavior would basically differ between different consumers of the code (for instance different behaviors on different iOS versions).
I had another take at this issue: #26257
The idea there was to be able to express an opt-in to the new (fixed?) behavior directly in the
JSONEncoder
andJSONDecoder
types by venting a new encoding/decoding 'strategy' configuration.I have since changed my personal opinion about this and I believe that the problem should not just be fixed for specific
Encoder
/Decoder
pairs, but rather for all.This PR is an attempt to fix the situation by adding a new protocol:
CodingKeyRepresentable
. By adding conformance to the type of a key, you will be opting in to having dictionaries with keys of this type to encode as dictionaries instead of arrays of key/value pairs.The opt-in can only happen for a version of swift where the protocol is available, so the user will be in full control of the situation. For instance I am currently using my own workaround, but once I only support iOS versions running a specific future Swift X version with this feature, I could skip my own workaround and rely on this behavior instead.
This draft PR is a placeholder so that I can start a Swift Evolution discussion. Perhaps even a pitch.