Skip to content
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

Merged
merged 36 commits into from
Oct 30, 2021

Conversation

mortenbekditlevsen
Copy link
Contributor

@mortenbekditlevsen mortenbekditlevsen commented Oct 27, 2020

This PR contains an implementation for the accepted Swift Evolution proposal: SE-0320.

The current Codable system has an issue that only String and Int 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 as String or Int 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 and JSONDecoder 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.

@mortenbekditlevsen mortenbekditlevsen changed the title [WIP] Added protocol StringKeyCodable [WIP] Added protocol CodingKeyRepresentable Jan 24, 2021
Copy link
Contributor

@benrimmington benrimmington left a 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.

@mortenbekditlevsen

This comment has been minimized.

@benrimmington

This comment has been minimized.

@mortenbekditlevsen

This comment has been minimized.

@benrimmington
Copy link
Contributor

You can try the following, from within the swift directory:

utils/run-test \
  --build-dir=../build/Ninja-RelWithDebInfoAssert \
  --build=true \
  --subset=all \
  --target=macosx-x86_64 \
  --verbose \
  test/stdlib/CodableTests.swift

(The exact --build-dir and --target arguments may vary, depending on your machine and build options.)

@mortenbekditlevsen

This comment has been minimized.

@benrimmington

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.
@mortenbekditlevsen

This comment has been minimized.

@mortenbekditlevsen

This comment has been minimized.

@benrimmington

This comment has been minimized.

@mortenbekditlevsen

This comment has been minimized.

@benrimmington
Copy link
Contributor

Is this ready for merging, or is there anything else I can do? 😊

@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:

Copy link
Member

@amartini51 amartini51 left a 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.

@amartini51 amartini51 self-requested a review September 29, 2021 21:06
Copy link
Member

@amartini51 amartini51 left a 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.

Copy link
Contributor

@benrimmington benrimmington left a 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.)

@mortenbekditlevsen
Copy link
Contributor Author

Thank you @benrimmington ,
I also didn't attempt reformatting CodableTests.swift - it appears that the 80 character line limit is not respected elsewhere in that file.

@mortenbekditlevsen
Copy link
Contributor Author

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?

@benrimmington
Copy link
Contributor

@swift-ci Please smoke test

@benrimmington
Copy link
Contributor

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?

The next version is Swift 5.6, but I haven't seen an announcement yet. Based on the table of previous announcements, a release/5.6 branch might be created in December (and the release might be in March or April).

@benrimmington benrimmington requested a review from drexin October 18, 2021 02:01
@mortenbekditlevsen
Copy link
Contributor Author

Hi @drexin ,
Excuse me for bumping, but I'm so excited for my SE proposal to land in Swift.
Anything I can do to push this forward?
Sincerely,
/morten

Copy link
Contributor

@drexin drexin left a 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!

@drexin
Copy link
Contributor

drexin commented Oct 29, 2021

@swift-ci test windows

@drexin
Copy link
Contributor

drexin commented Oct 29, 2021

@swift-ci smoke test

Copy link
Contributor

@benrimmington benrimmington left a 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

@mortenbekditlevsen
Copy link
Contributor Author

Thank you @benrimmington

@drexin
Copy link
Contributor

drexin commented Oct 30, 2021

@swift-ci smoke test

@benrimmington benrimmington merged commit 0251957 into swiftlang:main Oct 30, 2021
@mortenbekditlevsen
Copy link
Contributor Author

Yaaaaaay! Thank you so much to everyone who helped! 😊🥰🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants