-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Require explicit typing for DocumentSnapshot decoding. DocumentReference decoding. #9101
Require explicit typing for DocumentSnapshot decoding. DocumentReference decoding. #9101
Conversation
…ference+ReadDecodable.swift
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
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.
- Docs needs to be cleaned up,
- I had a question around NSNull()
- The tests should be updated
- Adding a sample would be nice
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
And here's the async use case version of the code:
|
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 like a nice usability improvement. Thanks @mortenbekditlevsen! What is the impact on migrating code using the existing library?
If others agree, let's try to run this through API review in January.
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
If you have code that decodes a snapshot into an entity, the result will no longer be an There are two fixes: Inserting a question mark on the decodable type will give you the old behavior: The other fix - for instance in case the snapshot comes from a collection, meaning that you know the data exists, is to then skip the extra unwrapping. The compiler will likely hint at this since the value is no longer optional. I should probably sketch out both scenarios in an example to provide a bit of guidance on the change. |
I believe that all requested changes have been addressed. Would you mind a second review, @peterfriese ? |
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.
I ❤️ the improved usability of this change. However, making T
non-optional is a breaking change for all Firestore users. Can we try implementing this in a non-breaking way?
Other than that, a couple of small issues in the docs.
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentSnapshot+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Hi @peterfriese , Regarding the breaking change: Second best for users already using the API would be to provide a good guide with an update path. What do you think? New API and deprecation of existing - or rely on beta testers to be open to such a change? |
@ryanwilson Any suggestions about the best way to deprecate the current and naming of the new? |
Great questions, and thanks as always @mortenbekditlevsen for working through this with us. Since this is beta and we're already at the ideal naming, I'm in favour of making the breaking change and add instructions / an explanation in the CHANGELOG (or a link to instructions). cc @schmidt-sebastian do you have a preference one way or the other? |
We can ship this as a breaking change. There is no reason to have |
I updated the SwiftFormat we're using in #9239, so please rebase and update. Sorry about the inconvenience. |
I rebased and reacted to the 'changes requested' by requesting a re-review. |
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentSnapshot+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Thanks for the changes, Morten! Here are the next steps:
I will update this comment as I make progress. |
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.
Investigated what happens if we force unwrap the error, and if returning nil
/nil
(a.k.a. "this should never happen") works as expected (although - not sure if something that should never happen can have an expected outcome 😏 )
Some small nits in the docs.
API review was positive!
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentReference+WriteEncodable.swift
Outdated
Show resolved
Hide resolved
Firestore/Swift/Source/Codable/DocumentSnapshot+ReadDecodable.swift
Outdated
Show resolved
Hide resolved
Signed-off-by: Peter Friese <peter@peterfriese.de>
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 integration tests in Firestore/Swift/Tests/Integration/CodableIntegrationTests.swift
need to be updated as well.
@peterfriese I updated the code, but in blind as I haven't been able to figure out how to run the codable integration tests. |
I had to dig around a bit myself. To run the Firestore tests, you need to
|
Thanks @peterfriese FYI I got a linker error that the Firestore_Example_iOS couldn't be found, so that appears to be an implicit dependency. After building the app target I can now compile, link and run the tests successfully. So the previous commit should be good to go. :-) |
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.
Great work! Thanks @mortenbekditlevsen and @peterfriese
Signed-off-by: Peter Friese <peter@peterfriese.de>
A while back, the Codable API for RTDB was accepted.
That API differs from the existing Codable implementation for Firestore in one important aspect:
Decoding a
DocumentSnapshot
in Firestore always returns anOptional
result even though you may have prior knowledge (you are iterating over documents in a collection, which means they must exist) or you have prior explicit expectations (for instance: I consider it to be an error if I cannot find aConfiguration
entity in this specific Firestore document).The similar RTDB API allows the user to be more explicit about their expectations. If they know or have a hard expectation about the presence of an entity, they can use:
while if they only know that a specific reference only might contain an entity, they can be explicit about that expectation as well:
I suggest to allow the users of Firestore the same amount of flexibility and explicit control, decreasing the amount of required 'unwrapping' when dealing with Codable entities in Firestore.
I also added a
getDocument(as: ...)
API in order to allow for simpler usage of the API.To take a real world example, here's the guide from @peterfriese to simplify Firestore usage using Codable:
https://github.com/peterfriese/Swift-Firestore-Guide/blob/main/FirestoreCodableSamples/Screens/MappingSimpleTypesScreen.swift
with this PR this can be simplified a bit further to:
If you wish a dedicated
FirestoreDecodingError
for the situation where the snapshot can't be decoded and the reason is that the data doesn't exist, then it is fairly easy to accommodate that too by changing the decoding of the snapshot a bit:Let me know what you think: @ryanwilson, @paulb777, @schmidt-sebastian