Skip to content

Commit 0643b36

Browse files
var-constwilhuff
authored andcommitted
C++ migration: make FIRFirestore implementation delegate to core API (#2492)
1 parent 2d297eb commit 0643b36

24 files changed

+774
-443
lines changed

Firestore/Example/Tests/API/FIRQuerySnapshotTests.mm

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2626
#import "Firestore/Source/API/FIRDocumentChange+Internal.h"
2727
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
28+
#import "Firestore/Source/API/FIRFirestore+Internal.h"
2829
#import "Firestore/Source/API/FIRQuerySnapshot+Internal.h"
2930
#import "Firestore/Source/API/FIRSnapshotMetadata+Internal.h"
3031
#import "Firestore/Source/Model/FSTDocument.h"
@@ -110,16 +111,18 @@ - (void)testIncludeMetadataChanges {
110111
snapshot:std::move(viewSnapshot)
111112
metadata:metadata];
112113

113-
FIRQueryDocumentSnapshot *doc1Snap = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
114-
documentKey:doc1New.key
115-
document:doc1New
116-
fromCache:NO
117-
hasPendingWrites:NO];
118-
FIRQueryDocumentSnapshot *doc2Snap = [FIRQueryDocumentSnapshot snapshotWithFirestore:firestore
119-
documentKey:doc2New.key
120-
document:doc2New
121-
fromCache:NO
122-
hasPendingWrites:NO];
114+
FIRQueryDocumentSnapshot *doc1Snap =
115+
[[FIRQueryDocumentSnapshot alloc] initWithFirestore:firestore.wrapped
116+
documentKey:doc1New.key
117+
document:doc1New
118+
fromCache:false
119+
hasPendingWrites:false];
120+
FIRQueryDocumentSnapshot *doc2Snap =
121+
[[FIRQueryDocumentSnapshot alloc] initWithFirestore:firestore.wrapped
122+
documentKey:doc2New.key
123+
document:doc2New
124+
fromCache:false
125+
hasPendingWrites:false];
123126

124127
NSArray<FIRDocumentChange *> *changesWithoutMetadata = @[
125128
[[FIRDocumentChange alloc] initWithType:FIRDocumentChangeTypeModified

Firestore/Example/Tests/API/FSTAPIHelpers.mm

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@
5555
dispatch_once(&onceToken, ^{
5656
sharedInstance = [[FIRFirestore alloc] initWithProjectID:"abc"
5757
database:"abc"
58-
persistenceKey:@"db123"
59-
credentialsProvider:nil
60-
workerQueue:nil
58+
persistenceKey:"db123"
59+
credentialsProvider:nullptr
60+
workerQueue:nullptr
6161
firebaseApp:nil];
6262
});
6363
#pragma clang diagnostic pop
@@ -73,11 +73,11 @@
7373
data ? FSTTestDoc(path, version, data,
7474
hasMutations ? FSTDocumentStateLocalMutations : FSTDocumentStateSynced)
7575
: nil;
76-
return [FIRDocumentSnapshot snapshotWithFirestore:FSTTestFirestore()
77-
documentKey:testutil::Key(path)
78-
document:doc
79-
fromCache:fromCache
80-
hasPendingWrites:hasMutations];
76+
return [[FIRDocumentSnapshot alloc] initWithFirestore:FSTTestFirestore().wrapped
77+
documentKey:testutil::Key(path)
78+
document:doc
79+
fromCache:fromCache
80+
hasPendingWrites:hasMutations];
8181
}
8282

8383
FIRCollectionReference *FSTTestCollectionRef(const absl::string_view path) {
@@ -86,8 +86,8 @@
8686
}
8787

8888
FIRDocumentReference *FSTTestDocRef(const absl::string_view path) {
89-
return [FIRDocumentReference referenceWithPath:testutil::Resource(path)
90-
firestore:FSTTestFirestore()];
89+
return [[FIRDocumentReference alloc] initWithPath:testutil::Resource(path)
90+
firestore:FSTTestFirestore().wrapped];
9191
}
9292

9393
/** A convenience method for creating a query snapshots for tests. */

Firestore/Example/Tests/Util/FSTIntegrationTestCase.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ - (FIRFirestore *)firestoreWithProjectID:(NSString *)projectID {
193193

194194
FIRFirestore *firestore = [[FIRFirestore alloc] initWithProjectID:util::MakeString(projectID)
195195
database:DatabaseId::kDefault
196-
persistenceKey:persistenceKey
196+
persistenceKey:util::MakeString(persistenceKey)
197197
credentialsProvider:std::move(credentials_provider)
198198
workerQueue:std::move(workerQueue)
199199
firebaseApp:app];

Firestore/Source/API/FIRCollectionReference.mm

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616

1717
#import "FIRCollectionReference.h"
18-
#import "FIRFirestore.h"
18+
19+
#include <utility>
1920

2021
#include "Firestore/core/src/firebase/firestore/util/autoid.h"
2122

2223
#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
24+
#import "Firestore/Source/API/FIRFirestore+Internal.h"
2325
#import "Firestore/Source/API/FIRQuery+Internal.h"
2426
#import "Firestore/Source/API/FIRQuery_Init.h"
2527
#import "Firestore/Source/Core/FSTQuery.h"
@@ -99,7 +101,8 @@ - (FIRDocumentReference *_Nullable)parent {
99101
return nil;
100102
} else {
101103
DocumentKey key{parentPath};
102-
return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
104+
return [[FIRDocumentReference alloc] initWithKey:std::move(key)
105+
firestore:self.firestore.wrapped];
103106
}
104107
}
105108

@@ -112,8 +115,9 @@ - (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
112115
FSTThrowInvalidArgument(@"Document path cannot be nil.");
113116
}
114117
const ResourcePath subPath = ResourcePath::FromString(util::MakeString(documentPath));
115-
const ResourcePath path = self.query.path.Append(subPath);
116-
return [FIRDocumentReference referenceWithPath:path firestore:self.firestore];
118+
ResourcePath path = self.query.path.Append(subPath);
119+
return [[FIRDocumentReference alloc] initWithPath:std::move(path)
120+
firestore:self.firestore.wrapped];
117121
}
118122

119123
- (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
@@ -129,8 +133,8 @@ - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)da
129133
}
130134

131135
- (FIRDocumentReference *)documentWithAutoID {
132-
const DocumentKey key{self.query.path.Append(CreateAutoId())};
133-
return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
136+
DocumentKey key{self.query.path.Append(CreateAutoId())};
137+
return [[FIRDocumentReference alloc] initWithKey:std::move(key) firestore:self.firestore.wrapped];
134138
}
135139

136140
@end

Firestore/Source/API/FIRDocumentChange+Internal.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#import "FIRDocumentChange.h"
1818

19+
#include "Firestore/core/src/firebase/firestore/api/firestore.h"
1920
#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
2021

2122
@class FIRFirestore;
@@ -26,10 +27,10 @@ NS_ASSUME_NONNULL_BEGIN
2627
@interface FIRDocumentChange (Internal)
2728

2829
/** Calculates the array of FIRDocumentChange's based on the given FSTViewSnapshot. */
29-
+ (NSArray<FIRDocumentChange *> *)documentChangesForSnapshot:
30-
(const firebase::firestore::core::ViewSnapshot &)snapshot
31-
includeMetadataChanges:(BOOL)includeMetadataChanges
32-
firestore:(FIRFirestore *)firestore;
30+
+ (NSArray<FIRDocumentChange *> *)
31+
documentChangesForSnapshot:(const firebase::firestore::core::ViewSnapshot &)snapshot
32+
includeMetadataChanges:(bool)includeMetadataChanges
33+
firestore:(firebase::firestore::api::Firestore *)firestore;
3334

3435
@end
3536

Firestore/Source/API/FIRDocumentChange.mm

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
#import "FIRDocumentChange.h"
1818

1919
#import "Firestore/Source/API/FIRDocumentSnapshot+Internal.h"
20+
#import "Firestore/Source/API/FIRFirestore+Internal.h"
2021
#import "Firestore/Source/Core/FSTQuery.h"
2122
#import "Firestore/Source/Model/FSTDocument.h"
2223
#import "Firestore/Source/Model/FSTDocumentSet.h"
2324

2425
#include "Firestore/core/src/firebase/firestore/core/view_snapshot.h"
2526
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
2627

28+
using firebase::firestore::api::Firestore;
2729
using firebase::firestore::core::DocumentViewChange;
2830
using firebase::firestore::core::ViewSnapshot;
2931

@@ -55,21 +57,21 @@ + (FIRDocumentChangeType)documentChangeTypeForChange:(const DocumentViewChange &
5557
}
5658

5759
+ (NSArray<FIRDocumentChange *> *)documentChangesForSnapshot:(const ViewSnapshot &)snapshot
58-
includeMetadataChanges:(BOOL)includeMetadataChanges
59-
firestore:(FIRFirestore *)firestore {
60+
includeMetadataChanges:(bool)includeMetadataChanges
61+
firestore:(Firestore *)firestore {
6062
if (snapshot.old_documents().isEmpty) {
6163
// Special case the first snapshot because index calculation is easy and fast. Also all changes
6264
// on the first snapshot are adds so there are also no metadata-only changes to filter out.
6365
FSTDocument *_Nullable lastDocument = nil;
6466
NSUInteger index = 0;
6567
NSMutableArray<FIRDocumentChange *> *changes = [NSMutableArray array];
6668
for (const DocumentViewChange &change : snapshot.document_changes()) {
67-
FIRQueryDocumentSnapshot *document = [FIRQueryDocumentSnapshot
68-
snapshotWithFirestore:firestore
69-
documentKey:change.document().key
70-
document:change.document()
71-
fromCache:snapshot.from_cache()
72-
hasPendingWrites:snapshot.mutated_keys().contains(change.document().key)];
69+
FIRQueryDocumentSnapshot *document = [[FIRQueryDocumentSnapshot alloc]
70+
initWithFirestore:firestore
71+
documentKey:change.document().key
72+
document:change.document()
73+
fromCache:snapshot.from_cache()
74+
hasPendingWrites:snapshot.mutated_keys().contains(change.document().key)];
7375
HARD_ASSERT(change.type() == DocumentViewChange::Type::kAdded,
7476
"Invalid event type for first snapshot");
7577
HARD_ASSERT(!lastDocument || snapshot.query().comparator(lastDocument, change.document()) ==
@@ -91,12 +93,12 @@ + (FIRDocumentChangeType)documentChangeTypeForChange:(const DocumentViewChange &
9193
continue;
9294
}
9395

94-
FIRQueryDocumentSnapshot *document = [FIRQueryDocumentSnapshot
95-
snapshotWithFirestore:firestore
96-
documentKey:change.document().key
97-
document:change.document()
98-
fromCache:snapshot.from_cache()
99-
hasPendingWrites:snapshot.mutated_keys().contains(change.document().key)];
96+
FIRQueryDocumentSnapshot *document = [[FIRQueryDocumentSnapshot alloc]
97+
initWithFirestore:firestore
98+
documentKey:change.document().key
99+
document:change.document()
100+
fromCache:snapshot.from_cache()
101+
hasPendingWrites:snapshot.mutated_keys().contains(change.document().key)];
100102

101103
NSUInteger oldIndex = NSNotFound;
102104
NSUInteger newIndex = NSNotFound;

Firestore/Source/API/FIRDocumentReference+Internal.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,28 @@
1616

1717
#import "FIRDocumentReference.h"
1818

19+
#include "Firestore/core/src/firebase/firestore/api/document_reference.h"
1920
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2021
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
2122

2223
NS_ASSUME_NONNULL_BEGIN
2324

25+
@interface FIRDocumentReference (/* Init */)
26+
27+
- (instancetype)initWithReference:(firebase::firestore::api::DocumentReference &&)reference
28+
NS_DESIGNATED_INITIALIZER;
29+
30+
- (instancetype)initWithPath:(firebase::firestore::model::ResourcePath)path
31+
firestore:(firebase::firestore::api::Firestore *)firestore;
32+
33+
- (instancetype)initWithKey:(firebase::firestore::model::DocumentKey)key
34+
firestore:(firebase::firestore::api::Firestore *)firestore;
35+
36+
@end
37+
2438
/** Internal FIRDocumentReference API we don't want exposed in our public header files. */
2539
@interface FIRDocumentReference (Internal)
2640

27-
+ (instancetype)referenceWithPath:(const firebase::firestore::model::ResourcePath &)path
28-
firestore:(FIRFirestore *)firestore;
29-
+ (instancetype)referenceWithKey:(firebase::firestore::model::DocumentKey)key
30-
firestore:(FIRFirestore *)firestore;
31-
3241
- (const firebase::firestore::model::DocumentKey &)key;
3342

3443
@end

0 commit comments

Comments
 (0)