-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Require explicit typing for DocumentSnapshot decoding. DocumentReference decoding. #9101
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
Merged
schmidt-sebastian
merged 12 commits into
firebase:master
from
mortenbekditlevsen:firestore_documentreference_codable
Feb 23, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0e4769c
Require explicit typing for DocumentSnapshot decoding. Add DocumentRe…
mortenbekditlevsen 7815580
Updated doc comments. Added async version of getDocument(as:)
mortenbekditlevsen 4ff41d7
Ran style.sh
mortenbekditlevsen 37fb5be
Added LLC to copyright notice
mortenbekditlevsen 6a94a62
Fixed up comments
mortenbekditlevsen 31e3341
Merge branch 'master' into firestore_documentreference_codable
mortenbekditlevsen 2eb244f
Ran style.sh
mortenbekditlevsen 1d2f5d4
Change defaulting mechanism
mortenbekditlevsen c3e8cb3
Changes after review from Peter Friese
mortenbekditlevsen 848f5e2
Updated CodableIntegrationTests to match updated API
mortenbekditlevsen f6a3733
Added offline behavior description from Objective-C documentation and…
mortenbekditlevsen 9ce7b61
Updated comment after review
mortenbekditlevsen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
Firestore/Swift/Source/Codable/DocumentReference+ReadDecodable.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import Foundation | ||
import FirebaseFirestore | ||
|
||
public extension DocumentReference { | ||
/// Fetches and decodes the document referenced by this `DocumentReference`. | ||
/// | ||
/// This allows users to retrieve a Firestore document and have it decoded to | ||
/// an instance of caller-specified type as follows: | ||
/// ```swift | ||
/// ref.getDocument(as: Book.self) { result in | ||
/// do { | ||
/// let book = try result.get() | ||
/// } catch { | ||
/// // Handle error | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// This method attempts to provide up-to-date data when possible by waiting | ||
/// for data from the server, but it may return cached data or fail if you are | ||
/// offline and the server cannot be reached. If `T` denotes an optional | ||
/// type, the method returns a successful status with a value of `nil` for | ||
/// non-existing documents. | ||
/// | ||
/// - Parameters: | ||
/// - as: A `Decodable` type to convert the document fields to. | ||
/// - serverTimestampBehavior: Configures how server timestamps that have | ||
/// not yet been set to their final value are returned from the snapshot. | ||
/// - decoder: The decoder to use to convert the document. Defaults to use | ||
/// the default decoder. | ||
/// - completion: The closure to call when the document snapshot has been | ||
/// fetched and decoded. | ||
func getDocument<T: Decodable>(as type: T.Type, | ||
with serverTimestampBehavior: ServerTimestampBehavior = | ||
.none, | ||
decoder: Firestore.Decoder = .init(), | ||
completion: @escaping (Result<T, Error>) -> Void) { | ||
getDocument { snapshot, error in | ||
guard let snapshot = snapshot else { | ||
/** | ||
* Force unwrapping here is fine since this logic corresponds to the auto-synthesized | ||
* async/await wrappers for Objective-C functions with callbacks taking an object and an error | ||
* parameter. The API should (and does) guarantee that either object or error is set, but never both. | ||
* For more details see: | ||
* https://github.com/firebase/firebase-ios-sdk/pull/9101#discussion_r809117034 | ||
*/ | ||
completion(.failure(error!)) | ||
return | ||
} | ||
let result = Result { | ||
try snapshot.data(as: T.self, | ||
with: serverTimestampBehavior, | ||
decoder: decoder) | ||
} | ||
completion(result) | ||
} | ||
} | ||
|
||
#if compiler(>=5.5) && canImport(_Concurrency) | ||
/// Fetches and decodes the document referenced by this `DocumentReference`. | ||
/// | ||
/// This allows users to retrieve a Firestore document and have it decoded | ||
/// to an instance of caller-specified type as follows: | ||
/// ```swift | ||
/// do { | ||
/// let book = try await ref.getDocument(as: Book.self) | ||
/// } catch { | ||
/// // Handle error | ||
/// } | ||
/// ``` | ||
/// | ||
/// This method attempts to provide up-to-date data when possible by waiting | ||
/// for data from the server, but it may return cached data or fail if you | ||
/// are offline and the server cannot be reached. If `T` denotes | ||
/// an optional type, the method returns a successful status with a value | ||
/// of `nil` for non-existing documents. | ||
/// | ||
/// - Parameters: | ||
/// - as: A `Decodable` type to convert the document fields to. | ||
/// - serverTimestampBehavior: Configures how server timestamps that have | ||
/// not yet been set to their final value are returned from the | ||
/// snapshot. | ||
/// - decoder: The decoder to use to convert the document. Defaults to use | ||
/// the default decoder. | ||
/// - Returns: This instance of the supplied `Decodable` type `T`. | ||
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *) | ||
func getDocument<T: Decodable>(as type: T.Type, | ||
with serverTimestampBehavior: ServerTimestampBehavior = | ||
.none, | ||
decoder: Firestore.Decoder = .init()) async throws -> T { | ||
let snapshot = try await getDocument() | ||
return try snapshot.data(as: T.self, | ||
with: serverTimestampBehavior, | ||
decoder: decoder) | ||
} | ||
#endif | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.