-
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
Foundation Encoders #9005
Foundation Encoders #9005
Conversation
@swift-ci please test |
@DougGregor This isn't quite ready to be merged yet — I've got unit tests incoming either later this evening or early tomorrow. But when they make it I'll test and ping you if ready to merge |
Build failed |
Ok! |
Build failed |
* Integrate {JSON,PropertyList}{Encoder,Decoder} types to facilitate encoding types in JSON and property list formats * Adds Foundation-specific extensions to allow errors exposed from the stdlib to bridge to NSErrors
0922ea2
to
d177bdf
Compare
@swift-ci Please test |
Build failed |
Build failed |
@swift-ci please clean test |
Build failed |
@swift-ci clean test macOS |
@DougGregor @tkremenek I think the linker errors are legitimate, no? The box types that I added were marked as |
@itaiferber great point; I wasn't certain if those failures were genuine or not. I'll cancel the job I just initiated. |
Build failed |
The box types were previously fileprivate because they lived in the Foundation overlay. As part of the Swift stdlib, though, they need to be internal so they can be linked against.
@swift-ci Please test |
Build failed |
Build failed |
🎉 @DougGregor @tkremenek When either of you gets a chance, do you mind merging this in please? Thanks! |
@tkremenek Thanks! |
Congrats to @itaiferber for getting this landed |
Thanks, @DougGregor! Looking forward to feedback when people start using it. :) |
@itaiferber I have another question. |
@norio-nomura This is another bug: |
@itaiferber Thank you for clarifying. 🙏 |
@itaiferber Is Codable support for NSKeyedArchiver part of another PR ? It's in the SE but can't seems to find it in code for now. |
@Dimillian That integration hasn't been completed yet. This will be coming in in a future PR. |
@itaiferber |
@bubski How recent a snapshot of Swift are you using? |
@CodaFi I tried both |
It seems that because this has landed in the Darwin SDK that it will need to explicitly be put into Corelibs Foundation for it to be usable on Linux. The proposal does not make mention of platform lock-in, so I think this will need to be mirrored over. |
@bubski As @CodaFi mentions, this will need to be introduced into swift-corelibs-foundation in order to be available on Linux. The code as-is should be easy to transfer over to there without requiring many changes (if at all), but we haven't gotten the chance to (and it seems that no one from the community has either). |
I've filed SR-5195 about this. |
@itaiferber I started hacking around the idea But I don't have a good I'll try to clean it up and make a PR to |
@CodaFi Thanks for that! |
@bubski Cool, thanks! The one annoyance will be keeping these things in sync. There are still some outstanding changes that will be landing in |
@itaiferber Yea, I figured. I'm open to any suggestions. |
@bubski Yep, there are a few places where I've noted (in the Darwin implementation) places which may have this discrepancy. I think overall, it might be worth updating As for keeping things in sync, there's not much at the moment, unfortunately. We struggle with this ourselves. I think we need a general process/solution for improving this. |
@itaiferber Is having a default value + support optional Codable property something planed? Let me explain, you define a var as such in a Codable object:
But then if you decode your object with a JSON that does not contain progressMax it'll throw a missing key error. Which make sense, but it also make sense to have a default, hardcoded value, that is overwritten by a JSON defined one if available. |
@Dimillian It's possible to support, but I don't know if that's something we'd want to do out of the box. It's impossible for us to tell whether the default value is assigned for the benefit of other initializers, or whether you meant for it to be a default value if the JSON couldn't be read. I don't know if we can read into the intent there — did you want us to assign a default, or were you expecting the error? No real way for us to know. |
@itaiferber In this specific case, the JSON can be different and still represent the same object once decoded.
The JSON will always have the title and author value defined, but the progress value will be in only in some cases (let's say it's an API and the server control it). Is there a way in current implementation to represent that? But I understand that you can't really define our intent at a lower level, and it make sense for other use case to throw an error. |
@Dimillian You've got a few options here (of course, not all perfect):
None of these solutions are great, but they are at least explicit. I'll think on this some more; perhaps there is a cleaner way to improve the experience of doing this. |
@itaiferber Actually, your 1. is what we did. It's a bit subpar because we have to unwrap it each time yes. But that's acceptable. In our case we have a lot of var like that now, as we converted NSCoding/Reflection code to Codable. And we could write custom decoder and encoder but in that case we lose a bit of the benefit of Codable. |
This implements the
JSONEncoder
,JSONDecoder
,PropertyListEncoder
, andPropertyListDecoder
types proposed by SE-0167.This pulls all Foundation-specific work out of #8124 for separate integration.
This PR depends on #9004 and should NOT be merged until #9004 is complete and merged.
rdar://problem/30471969
rdar://problem/30472028