Skip to content

Commit 5a68eda

Browse files
authored
Convert FIRGetOptions.initWithSource to factory method. (#655) (#711)
* Convert FIRGetOptions.initWithSource to factory method. * source -> optionsWithSource * Swift: GetOptions.source(source:) -> source(_:)
1 parent 3440fef commit 5a68eda

File tree

5 files changed

+42
-45
lines changed

5 files changed

+42
-45
lines changed

Firestore/Example/SwiftBuildTest/main.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ func readDocument(at docRef: DocumentReference) {
228228
func readDocumentWithOptions(at docRef: DocumentReference) {
229229
docRef.getDocument(options:GetOptions.defaultOptions()) { document, error in
230230
}
231-
docRef.getDocument(options:GetOptions(source:GetSource.default)) { document, error in
231+
docRef.getDocument(options:GetOptions.source(GetSource.default)) { document, error in
232232
}
233-
docRef.getDocument(options:GetOptions(source:.server)) { document, error in
233+
docRef.getDocument(options:GetOptions.source(.server)) { document, error in
234234
}
235-
docRef.getDocument(options:GetOptions(source:GetSource.cache)) { document, error in
235+
docRef.getDocument(options:GetOptions.source(GetSource.cache)) { document, error in
236236
}
237237
}
238238

@@ -249,11 +249,11 @@ func readDocuments(matching query: Query) {
249249
func readDocumentsWithOptions(matching query: Query) {
250250
query.getDocuments(options:GetOptions.defaultOptions()) { querySnapshot, error in
251251
}
252-
query.getDocuments(options:GetOptions.init(source:GetSource.default)) { querySnapshot, error in
252+
query.getDocuments(options:GetOptions.source(GetSource.default)) { querySnapshot, error in
253253
}
254-
query.getDocuments(options:GetOptions.init(source:GetSource.server)) { querySnapshot, error in
254+
query.getDocuments(options:GetOptions.source(.server)) { querySnapshot, error in
255255
}
256-
query.getDocuments(options:GetOptions.init(source:GetSource.cache)) { querySnapshot, error in
256+
query.getDocuments(options:GetOptions.source(GetSource.cache)) { querySnapshot, error in
257257
}
258258
}
259259

Firestore/Example/Tests/Integration/API/FIRGetOptionsTests.m

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ - (void)testGetDocumentWhileOnlineCacheOnly {
145145
// get doc and ensure that it exists, *is* from the cache, and matches
146146
// the initialData.
147147
FIRDocumentSnapshot *result =
148-
[self readDocumentForRef:doc
149-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
148+
[self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
150149
XCTAssertTrue(result.exists);
151150
XCTAssertTrue(result.metadata.fromCache);
152151
XCTAssertFalse(result.metadata.hasPendingWrites);
@@ -167,8 +166,7 @@ - (void)testGetCollectionWhileOnlineCacheOnly {
167166
// get docs and ensure they *are* from the cache, and matches the
168167
// initialDocs.
169168
FIRQuerySnapshot *result =
170-
[self readDocumentSetForRef:col
171-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
169+
[self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
172170
XCTAssertTrue(result.metadata.fromCache);
173171
XCTAssertFalse(result.metadata.hasPendingWrites);
174172
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
@@ -205,8 +203,7 @@ - (void)testGetDocumentWhileOfflineCacheOnly {
205203
// get doc and ensure it exists, *is* from the cache, and matches the
206204
// newData.
207205
FIRDocumentSnapshot *result =
208-
[self readDocumentForRef:doc
209-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
206+
[self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
210207
XCTAssertTrue(result.exists);
211208
XCTAssertTrue(result.metadata.fromCache);
212209
XCTAssertTrue(result.metadata.hasPendingWrites);
@@ -237,8 +234,7 @@ - (void)testGetCollectionWhileOfflineCacheOnly {
237234
// get docs and ensure they *are* from the cache, and matches the updated
238235
// data.
239236
FIRQuerySnapshot *result =
240-
[self readDocumentSetForRef:col
241-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
237+
[self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
242238
XCTAssertTrue(result.metadata.fromCache);
243239
XCTAssertTrue(result.metadata.hasPendingWrites);
244240
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
@@ -264,8 +260,7 @@ - (void)testGetDocumentWhileOnlineServerOnly {
264260
// get doc and ensure that it exists, is *not* from the cache, and matches
265261
// the initialData.
266262
FIRDocumentSnapshot *result =
267-
[self readDocumentForRef:doc
268-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]];
263+
[self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]];
269264
XCTAssertTrue(result.exists);
270265
XCTAssertFalse(result.metadata.fromCache);
271266
XCTAssertFalse(result.metadata.hasPendingWrites);
@@ -286,8 +281,7 @@ - (void)testGetCollectionWhileOnlineServerOnly {
286281
// get docs and ensure they are *not* from the cache, and matches the
287282
// initialData.
288283
FIRQuerySnapshot *result =
289-
[self readDocumentSetForRef:col
290-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]];
284+
[self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]];
291285
XCTAssertFalse(result.metadata.fromCache);
292286
XCTAssertFalse(result.metadata.hasPendingWrites);
293287
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
@@ -314,7 +308,7 @@ - (void)testGetDocumentWhileOfflineServerOnly {
314308

315309
// attempt to get doc and ensure it cannot be retreived
316310
XCTestExpectation *failedGetDocCompletion = [self expectationWithDescription:@"failedGetDoc"];
317-
[doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]
311+
[doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer]
318312
completion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
319313
XCTAssertNotNil(error);
320314
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
@@ -340,7 +334,7 @@ - (void)testGetCollectionWhileOfflineServerOnly {
340334

341335
// attempt to get docs and ensure they cannot be retreived
342336
XCTestExpectation *failedGetDocsCompletion = [self expectationWithDescription:@"failedGetDocs"];
343-
[col getDocumentsWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]
337+
[col getDocumentsWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer]
344338
completion:^(FIRQuerySnapshot *snapshot, NSError *error) {
345339
XCTAssertNotNil(error);
346340
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
@@ -380,24 +374,23 @@ - (void)testGetDocumentWhileOfflineWithDifferentGetOptions {
380374
// get doc (from cache) and ensure it exists, *is* from the cache, and
381375
// matches the newData.
382376
FIRDocumentSnapshot *result =
383-
[self readDocumentForRef:doc
384-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
377+
[self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
385378
XCTAssertTrue(result.exists);
386379
XCTAssertTrue(result.metadata.fromCache);
387380
XCTAssertTrue(result.metadata.hasPendingWrites);
388381
XCTAssertEqualObjects(result.data, newData);
389382

390383
// attempt to get doc (with default get options)
391-
result = [self readDocumentForRef:doc
392-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault]];
384+
result =
385+
[self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceDefault]];
393386
XCTAssertTrue(result.exists);
394387
XCTAssertTrue(result.metadata.fromCache);
395388
XCTAssertTrue(result.metadata.hasPendingWrites);
396389
XCTAssertEqualObjects(result.data, newData);
397390

398391
// attempt to get doc (from the server) and ensure it cannot be retreived
399392
XCTestExpectation *failedGetDocCompletion = [self expectationWithDescription:@"failedGetDoc"];
400-
[doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]
393+
[doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer]
401394
completion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
402395
XCTAssertNotNil(error);
403396
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
@@ -440,8 +433,7 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions {
440433
// get docs (from cache) and ensure they *are* from the cache, and
441434
// matches the updated data.
442435
FIRQuerySnapshot *result =
443-
[self readDocumentSetForRef:col
444-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
436+
[self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
445437
XCTAssertTrue(result.metadata.fromCache);
446438
XCTAssertTrue(result.metadata.hasPendingWrites);
447439
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
@@ -458,7 +450,7 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions {
458450

459451
// attempt to get docs (with default get options)
460452
result = [self readDocumentSetForRef:col
461-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault]];
453+
options:[FIRGetOptions optionsWithSource:FIRGetSourceDefault]];
462454
XCTAssertTrue(result.metadata.fromCache);
463455
XCTAssertEqualObjects(FIRQuerySnapshotGetData(result), (@[
464456
@{@"key1" : @"value1"}, @{@"key2" : @"value2", @"key2b" : @"value2b"},
@@ -474,7 +466,7 @@ - (void)testGetCollectionWhileOfflineWithDifferentGetOptions {
474466

475467
// attempt to get docs (from the server) and ensure they cannot be retreived
476468
XCTestExpectation *failedGetDocsCompletion = [self expectationWithDescription:@"failedGetDocs"];
477-
[col getDocumentsWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]
469+
[col getDocumentsWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer]
478470
completion:^(FIRQuerySnapshot *snapshot, NSError *error) {
479471
XCTAssertNotNil(error);
480472
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
@@ -547,7 +539,7 @@ - (void)testGetNonExistingDocWhileOnlineCacheOnly {
547539
// certain documents *don't* exist.
548540
XCTestExpectation *getNonExistingDocCompletion =
549541
[self expectationWithDescription:@"getNonExistingDoc"];
550-
[doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]
542+
[doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceCache]
551543
completion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
552544
XCTAssertNotNil(error);
553545
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
@@ -562,8 +554,7 @@ - (void)testGetNonExistingCollectionWhileOnlineCacheOnly {
562554

563555
// get collection and ensure it's empty and that it *is* from the cache.
564556
FIRQuerySnapshot *snapshot =
565-
[self readDocumentSetForRef:col
566-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
557+
[self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
567558
XCTAssertEqual(snapshot.count, 0);
568559
XCTAssertEqual(snapshot.documentChanges.count, 0);
569560
XCTAssertTrue(snapshot.metadata.fromCache);
@@ -581,7 +572,7 @@ - (void)testGetNonExistingDocWhileOfflineCacheOnly {
581572
// certain documents *don't* exist.
582573
XCTestExpectation *getNonExistingDocCompletion =
583574
[self expectationWithDescription:@"getNonExistingDoc"];
584-
[doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]
575+
[doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceCache]
585576
completion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
586577
XCTAssertNotNil(error);
587578
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
@@ -599,8 +590,7 @@ - (void)testGetNonExistingCollectionWhileOfflineCacheOnly {
599590

600591
// get collection and ensure it's empty and that it *is* from the cache.
601592
FIRQuerySnapshot *snapshot =
602-
[self readDocumentSetForRef:col
603-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceCache]];
593+
[self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceCache]];
604594
XCTAssertEqual(snapshot.count, 0);
605595
XCTAssertEqual(snapshot.documentChanges.count, 0);
606596
XCTAssertTrue(snapshot.metadata.fromCache);
@@ -612,8 +602,7 @@ - (void)testGetNonExistingDocWhileOnlineServerOnly {
612602

613603
// get doc and ensure that it does not exist and is *not* from the cache.
614604
FIRDocumentSnapshot *snapshot =
615-
[self readDocumentForRef:doc
616-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]];
605+
[self readDocumentForRef:doc options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]];
617606
XCTAssertFalse(snapshot.exists);
618607
XCTAssertFalse(snapshot.metadata.fromCache);
619608
XCTAssertFalse(snapshot.metadata.hasPendingWrites);
@@ -624,8 +613,7 @@ - (void)testGetNonExistingCollectionWhileOnlineServerOnly {
624613

625614
// get collection and ensure that it's empty and that it's *not* from the cache.
626615
FIRQuerySnapshot *snapshot =
627-
[self readDocumentSetForRef:col
628-
options:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]];
616+
[self readDocumentSetForRef:col options:[FIRGetOptions optionsWithSource:FIRGetSourceServer]];
629617
XCTAssertEqual(snapshot.count, 0);
630618
XCTAssertEqual(snapshot.documentChanges.count, 0);
631619
XCTAssertFalse(snapshot.metadata.fromCache);
@@ -643,7 +631,7 @@ - (void)testGetNonExistingDocWhileOfflineServerOnly {
643631
// certain documents *don't* exist.
644632
XCTestExpectation *getNonExistingDocCompletion =
645633
[self expectationWithDescription:@"getNonExistingDoc"];
646-
[doc getDocumentWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]
634+
[doc getDocumentWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer]
647635
completion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
648636
XCTAssertNotNil(error);
649637
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
@@ -661,7 +649,7 @@ - (void)testGetNonExistingCollectionWhileOfflineServerOnly {
661649

662650
// attempt to get collection and ensure that it cannot be retreived
663651
XCTestExpectation *failedGetDocsCompletion = [self expectationWithDescription:@"failedGetDocs"];
664-
[col getDocumentsWithOptions:[[FIRGetOptions alloc] initWithSource:FIRGetSourceServer]
652+
[col getDocumentsWithOptions:[FIRGetOptions optionsWithSource:FIRGetSourceServer]
665653
completion:^(FIRQuerySnapshot *snapshot, NSError *error) {
666654
XCTAssertNotNil(error);
667655
XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);

Firestore/Source/API/FIRGetOptions+Internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ NS_ASSUME_NONNULL_BEGIN
2323
/** Where getDocument[s] calls should get their data from. */
2424
@property(nonatomic, readonly, getter=source) FIRGetSource source;
2525

26+
- (instancetype)initWithSource:(FIRGetSource)source;
27+
2628
@end
2729

2830
NS_ASSUME_NONNULL_END

Firestore/Source/API/FIRGetOptions.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@implementation FIRGetOptions
2222

23-
+ (FIRGetOptions *)defaultOptions {
23+
+ (instancetype)defaultOptions {
2424
return [[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault];
2525
}
2626

@@ -31,6 +31,10 @@ - (instancetype)initWithSource:(FIRGetSource)source {
3131
return self;
3232
}
3333

34+
+ (instancetype)optionsWithSource:(FIRGetSource)source {
35+
return [[FIRGetOptions alloc] initWithSource:source];
36+
}
37+
3438
@end
3539

3640
NS_ASSUME_NONNULL_END

Firestore/Source/Public/FIRGetOptions.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ NS_ASSUME_NONNULL_BEGIN
2828
NS_SWIFT_NAME(GetOptions)
2929
@interface FIRGetOptions : NSObject
3030

31+
/** */
32+
- (instancetype)init __attribute((unavailable("FIRGetOptions cannot be created directly.")));
33+
3134
/**
3235
* Returns the default options.
3336
*
34-
* Equiavlent to `[[FIRGetOptions alloc] initWithSource:FIRGetSourceDefault]` in
37+
* Equiavlent to `[FIRGetOptions source:FIRGetSourceDefault]` in
3538
* objective-c.
3639
*/
37-
+ (FIRGetOptions *)defaultOptions NS_SWIFT_NAME(defaultOptions());
40+
+ (instancetype)defaultOptions NS_SWIFT_NAME(defaultOptions());
3841

3942
/**
4043
* Describes whether we should get from server or cache.
@@ -62,7 +65,7 @@ typedef NS_ENUM(NSUInteger, FIRGetSource) {
6265
/**
6366
* Initializes the get options with the specified source.
6467
*/
65-
- (instancetype)initWithSource:(FIRGetSource)source NS_SWIFT_NAME(init(source:));
68+
+ (instancetype)optionsWithSource:(FIRGetSource)source NS_SWIFT_NAME(source(_:));
6669

6770
@end
6871

0 commit comments

Comments
 (0)