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

Require explicit typing for DocumentSnapshot decoding. DocumentReference decoding. #9101

Merged
Prev Previous commit
Next Next commit
Changes after review from Peter Friese
  • Loading branch information
mortenbekditlevsen committed Feb 17, 2022
commit c3e8cb3419beb8db27f25ee51ed2fb935019c0ae
1 change: 0 additions & 1 deletion Firestore/Swift/Source/Codable/CodableErrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
public enum FirestoreDecodingError: Error {
case decodingIsNotSupported(String)
case fieldNameConflict(String)
case `internal`
}

public enum FirestoreEncodingError: Error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ 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.
/// caller-specified type as follows:
/// ```swift
/// ref.getDocument(as: Book.self) { result in
/// do {
/// let book = try result.get()
/// } catch {
/// // Handle error
/// }
/// }
/// ref.getDocument(as: Book.self) { result in
/// do {
/// let book = try result.get()
/// } catch {
/// // Handle error
/// }
/// }
/// ```
/// - Parameters:
/// - as: A `Decodable` type to convert the document fields to.
Expand All @@ -45,7 +45,14 @@ public extension DocumentReference {
completion: @escaping (Result<T, Error>) -> Void) {
getDocument { snapshot, error in
guard let snapshot = snapshot else {
completion(.failure(error ?? FirestoreDecodingError.internal))
/**
* 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 {
Expand All @@ -63,11 +70,11 @@ public extension DocumentReference {
/// This allows users to retrieve a Firestore document and have it decoded to an instance of
/// caller-specified type.
/// ```swift
/// do {
/// let book = try await ref.getDocument(as: Book.self)
/// } catch {
/// // Handle error
/// }
/// do {
/// let book = try await ref.getDocument(as: Book.self)
/// } catch {
/// // Handle error
/// }
/// ```
/// - Parameters:
/// - as: A `Decodable` type to convert the document fields to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public extension DocumentReference {
/// - value: An instance of `Encodable` to be encoded to a document.
/// - encoder: An encoder instance to use to run the encoding.
/// - completion: A closure to execute once the document has been successfully
/// written to the server. This block will not be called while
/// written to the server. This closure will not be called while
/// the client is offline, though local changes will be visible
/// immediately.
func setData<T: Encodable>(from value: T,
Expand All @@ -50,7 +50,7 @@ public extension DocumentReference {
/// document.
/// - encoder: An encoder instance to use to run the encoding.
/// - completion: A closure to execute once the document has been successfully
/// written to the server. This block will not be called while
/// written to the server. This closure will not be called while
/// the client is offline, though local changes will be visible
/// immediately.
func setData<T: Encodable>(from value: T,
Expand All @@ -77,7 +77,7 @@ public extension DocumentReference {
/// document.
/// - encoder: An encoder instance to use to run the encoding.
/// - completion: A closure to execute once the document has been successfully
/// written to the server. This block will not be called while
/// written to the server. This closure will not be called while
/// the client is offline, though local changes will be visible
/// immediately.
func setData<T: Encodable>(from value: T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public extension DocumentSnapshot {
///
/// By default, server-provided timestamps that have not yet been set to their
/// final value will be returned as `NSNull`. Pass `serverTimestampBehavior`
mortenbekditlevsen marked this conversation as resolved.
Show resolved Hide resolved
/// configure this behavior.
/// to configure this behavior.
///
/// See `Firestore.Decoder` for more details about the decoding process.
///
Expand All @@ -32,7 +32,7 @@ public extension DocumentSnapshot {
/// - 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
/// default decoder.
/// the default decoder.
func data<T: Decodable>(as type: T.Type,
with serverTimestampBehavior: ServerTimestampBehavior = .none,
decoder: Firestore.Decoder = .init()) throws -> T {
Expand Down