-
Notifications
You must be signed in to change notification settings - Fork 253
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
Autogenerated swift Codable conformance #2490
base: main
Are you sure you want to change the base?
Conversation
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.
This seems reasonable to me. My biggest concern is this going stale - would it be possible to add a test for this? eg, the omit_argument_labels
option has a test at fixtures/swift-omit-labels
I guess another question I have is what happens when something can't be made codable? For example, if a record has an ie, I guess I'm asking whether the approach should be to explicitly name the objects you want to be Codable rather than a "global" boolean? |
My thinking is that there are actually similar rules as to when If we implement it in a way that in the config the user has to fill all the various records, enums, ... it might be very cumbersome and hard to maintain. It could be hundreds of entries in bigger projects. I'm thinking of 3 variants:
|
Right, that's a solid argument tbh - I guess I can't see a reason to suggest So yeah, I guess this makes sense and we can tweak it as we find it necessary to. You may have guessed by now that I don't really know much about Swift :) So cc @martinmose @Sajjon @crazytonyli, any thoughts here? |
OK, I can change the config toggle to enabled-by-default. I was just more cautious and didn't want to break somebody's code ;) |
I don't think the configuration should be on by default, simply because most of the configurations are not on by default. I wonder if there are some subtle implications when both the Swift binding and the underlying Rust type can ser/de into JSON. Would you expect them to produce and parse the same JSON content? For example, should a JSON produced by First, the serde attributes on the Rust type are invisible to the Swift binding type. For example, it's impossible for Swift's Codable implementation to know how serde's implementation on the Rust type works. Even if we ignore serde attributes, for a rust type like |
That's an interesting discussion. I was leaning towards things which cause a default behaviour change should not be on by default, but things which allow new capabilities, where otherwise you'd get a compiler error, are safer to enable by default. So in this scenario and in #2490, ISTM that the new capability would only be used when code tried to pass these objects as a ie, I see this as more like the
I don't think it's necessary for us to have an opinion about this. IIUC, this capability probably make the most sense when serde is not involved on the Rust side, but even if it is available on both sides and the objects would serialize differently in that case, I think that's beyond the scope of what we are trying to do here. Do you disagree, or where you just commenting on the fact they may be different? |
I just wanted to highlight the fact that this configuration may cause the Rust types and their Swift bindings to handle serialization & deserialization differently. I wouldn't say I disagree with introducing this configuration. It's a configuration after all, we as users can choose to turn it on or off. BTW, I think |
That's only true for objects, right? For records, we do implement That said though, if we do agree implementing |
OK, so from the discussion it seems everybody is now tending towards not producing the conformance code by default. To me it also seems to be more preferred. For users that don't need Regarding the differences in serialization between rust and swift sides, I honestly didn't think about that before you mentioned that. Obviously it can (and most likely will) produce different results. So we can simply mention that in the documentation to the config toggle? Something like: |
Sounds great.
That's true for eq etc too, right? So maybe a more general note that reinforces these are entirely "foreign" (or from the other POV, "local") concepts and these are examples. Happy to roll these into 1 PR if that's easier for you too, thanks! |
Feel free to make any adjustments or close my PRs and open another. |
This is complementary to the
CaseIterable
PR: #2489It seems that
Codable
could be safely autogenerated for all records, enums and errors that don't contain objects (recursively). I have tried it with various declarations, also globally with the whole test suite and didn't run into any errors. Although there might be cases where this would generate code that is either wrong (unable to compile) or undesirable (user wants to use some custom implementation instead). So I have also added a swift config flaggenerate_codable_conformance
, with default valuefalse
(Codable
conformance not generated)