Skip to content

Commit 3a2077f

Browse files
authored
Fixes for the Codable API (#3734)
* Annotate Transaction and WriteBatch methods as discardable These methods exist to allow chaining if that's useful but chaining isn't expected or required. Note that Objective-C methods in these types are implicitly @discardableResult and don't need any extra help. * No need to explicitly discard results * Fix typos in FIRCollectionReference docs * Add support for CollectionReference.addDocument(from:)
1 parent ab78972 commit 3a2077f

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

Firestore/Source/Public/FIRCollectionReference.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ NS_SWIFT_NAME(CollectionReference)
6666
- (FIRDocumentReference *)documentWithPath:(NSString *)documentPath NS_SWIFT_NAME(document(_:));
6767

6868
/**
69-
* Add a new document to this collection with the specified data, assigning it a document ID
69+
* Adds a new document to this collection with the specified data, assigning it a document ID
7070
* automatically.
7171
*
7272
* @param data An `NSDictionary` containing the data for the new document.
@@ -77,7 +77,7 @@ NS_SWIFT_NAME(CollectionReference)
7777
NS_SWIFT_NAME(addDocument(data:));
7878

7979
/**
80-
* Add a new document to this collection with the specified data, assigning it a document ID
80+
* Adds a new document to this collection with the specified data, assigning it a document ID
8181
* automatically.
8282
*
8383
* @param data An `NSDictionary` containing the data for the new document.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Foundation
18+
import FirebaseFirestore
19+
20+
extension CollectionReference {
21+
/// Encodes an instance of `Encodable` and adds a new document to this collection
22+
/// with the encoded data, assigning it a document ID automatically.
23+
///
24+
/// See `Firestore.Encoder` for more details about the encoding process.
25+
///
26+
/// - Parameters:
27+
/// - value: An instance of `Encodable` to be encoded to a document.
28+
/// - encoder: An encoder instance to use to run the encoding.
29+
/// - completion: A block to execute once the document has been successfully
30+
/// written to the server. This block will not be called while
31+
/// the client is offline, though local changes will be visible
32+
/// immediately.
33+
/// - Returns: A `DocumentReference` pointing to the newly created document.
34+
public func addDocument<T: Encodable>(from value: T,
35+
encoder: Firestore.Encoder = Firestore.Encoder(),
36+
completion: ((Error?) -> Void)? = nil) throws -> DocumentReference {
37+
return addDocument(data: try encoder.encode(value), completion: completion)
38+
}
39+
}

Firestore/Swift/Source/Codable/Transaction+WriteEncodable.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extension Transaction {
2929
/// - encoder: The encoder instance to use to run the encoding.
3030
/// - doc: The document to create/overwrite the encoded data to.
3131
/// - Returns: This instance of `Transaction`. Used for chaining method calls.
32+
@discardableResult
3233
public func setData<T: Encodable>(from value: T,
3334
forDocument doc: DocumentReference,
3435
encoder: Firestore.Encoder = Firestore.Encoder()) throws -> Transaction {
@@ -50,6 +51,7 @@ extension Transaction {
5051
/// document.
5152
/// - encoder: The encoder instance to use to run the encoding.
5253
/// - Returns: This instance of `Transaction`. Used for chaining method calls.
54+
@discardableResult
5355
public func setData<T: Encodable>(from value: T,
5456
forDocument doc: DocumentReference,
5557
merge: Bool,
@@ -76,6 +78,7 @@ extension Transaction {
7678
/// document.
7779
/// - encoder: The encoder instance to use to run the encoding.
7880
/// - Returns: This instance of `Transaction`. Used for chaining method calls.
81+
@discardableResult
7982
public func setData<T: Encodable>(from value: T,
8083
forDocument doc: DocumentReference,
8184
mergeFields: [Any],

Firestore/Swift/Source/Codable/WriteBatch+WriteEncodable.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extension WriteBatch {
2929
/// - encoder: The encoder instance to use to run the encoding.
3030
/// - doc: The document to create/overwrite the encoded data to.
3131
/// - Returns: This instance of `WriteBatch`. Used for chaining method calls.
32+
@discardableResult
3233
public func setData<T: Encodable>(from value: T,
3334
forDocument doc: DocumentReference,
3435
encoder: Firestore.Encoder = Firestore.Encoder()) throws -> WriteBatch {
@@ -50,6 +51,7 @@ extension WriteBatch {
5051
/// document.
5152
/// - encoder: The encoder instance to use to run the encoding.
5253
/// - Returns: This instance of `WriteBatch`. Used for chaining method calls.
54+
@discardableResult
5355
public func setData<T: Encodable>(from value: T,
5456
forDocument doc: DocumentReference,
5557
merge: Bool,
@@ -76,6 +78,7 @@ extension WriteBatch {
7678
/// document.
7779
/// - encoder: The encoder instance to use to run the encoding.
7880
/// - Returns: This instance of `WriteBatch`. Used for chaining method calls.
81+
@discardableResult
7982
public func setData<T: Encodable>(from value: T,
8083
forDocument doc: DocumentReference,
8184
mergeFields: [Any],

Firestore/Swift/Tests/Integration/CodableIntegrationTests.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class CodableIntegrationTests: FSTIntegrationTestCase {
5555
doc.firestore.runTransaction({ (transaction, errorPointer) -> Any? in
5656
do {
5757
if let merge = merge {
58-
_ = try transaction.setData(from: value, forDocument: doc, merge: merge)
58+
try transaction.setData(from: value, forDocument: doc, merge: merge)
5959
} else if let mergeFields = mergeFields {
60-
_ = try transaction.setData(from: value, forDocument: doc, mergeFields: mergeFields)
60+
try transaction.setData(from: value, forDocument: doc, mergeFields: mergeFields)
6161
} else {
62-
_ = try transaction.setData(from: value, forDocument: doc)
62+
try transaction.setData(from: value, forDocument: doc)
6363
}
6464
} catch {
6565
XCTFail("setData with transaction failed.")
@@ -198,4 +198,23 @@ class CodableIntegrationTests: FSTIntegrationTestCase {
198198
age: 10, hobby: "Play"), "Failed with flavor \(flavor)")
199199
}
200200
}
201+
202+
func testAddDocument() throws {
203+
struct Model: Codable, Equatable {
204+
var name: String
205+
}
206+
207+
let collection = collectionRef()
208+
let model = Model(name: "test")
209+
210+
let added = expectation(description: "Add document")
211+
let docRef = try collection.addDocument(from: model) { error in
212+
XCTAssertNil(error)
213+
added.fulfill()
214+
}
215+
awaitExpectations()
216+
217+
let result = try readDocument(forRef: docRef).data(as: Model.self)
218+
XCTAssertEqual(model, result)
219+
}
201220
}

0 commit comments

Comments
 (0)