Skip to content

Commit 7203abe

Browse files
authored
add converters and port paths to FSTQuery (firebase#869)
* add converters and fix FSTQuery.{h,m} only * address changes * a change forget to address * add a dummy function to make inline-only-library buildable
1 parent 3dd5801 commit 7203abe

24 files changed

+485
-195
lines changed

Firestore/Example/Tests/Core/FSTQueryTests.mm

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,16 @@
2626
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
2727

2828
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
29+
#include "Firestore/core/src/firebase/firestore/model/field_path.h"
30+
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
2931
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
32+
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
3033

34+
namespace testutil = firebase::firestore::testutil;
3135
namespace util = firebase::firestore::util;
3236
using firebase::firestore::model::DatabaseId;
37+
using firebase::firestore::model::FieldPath;
38+
using firebase::firestore::model::ResourcePath;
3339

3440
NS_ASSUME_NONNULL_BEGIN
3541

@@ -41,8 +47,10 @@ - (FSTQuery *)queryByAddingSortBy:(NSString *)key ascending:(BOOL)ascending;
4147
@implementation FSTQuery (Tests)
4248

4349
- (FSTQuery *)queryByAddingSortBy:(NSString *)key ascending:(BOOL)ascending {
44-
return [self queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(key)
45-
ascending:ascending]];
50+
return [self
51+
queryByAddingSortOrder:[FSTSortOrder
52+
sortOrderWithFieldPath:testutil::Field(util::MakeStringView(key))
53+
ascending:ascending]];
4654
}
4755

4856
@end
@@ -55,11 +63,11 @@ @implementation FSTQueryTests
5563
- (void)testConstructor {
5664
FSTResourcePath *path =
5765
[FSTResourcePath pathWithSegments:@[ @"rooms", @"Firestore", @"messages", @"0001" ]];
58-
FSTQuery *query = [FSTQuery queryWithPath:path];
66+
FSTQuery *query = [FSTQuery queryWithPath:[path toCPPResourcePath]];
5967
XCTAssertNotNil(query);
6068

6169
XCTAssertEqual(query.sortOrders.count, 1);
62-
XCTAssertEqualObjects(query.sortOrders[0].field.canonicalString, kDocumentKeyPath);
70+
XCTAssertEqual(query.sortOrders[0].field.CanonicalString(), FieldPath::kDocumentKeyPath);
6371
XCTAssertEqual(query.sortOrders[0].ascending, YES);
6472

6573
XCTAssertEqual(query.explicitSortOrders.count, 0);
@@ -68,17 +76,17 @@ - (void)testConstructor {
6876
- (void)testOrderBy {
6977
FSTQuery *query = FSTTestQuery(@"rooms/Firestore/messages");
7078
query =
71-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"length")
79+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("length")
7280
ascending:NO]];
7381

7482
XCTAssertEqual(query.sortOrders.count, 2);
75-
XCTAssertEqualObjects(query.sortOrders[0].field.canonicalString, @"length");
83+
XCTAssertEqual(query.sortOrders[0].field.CanonicalString(), "length");
7684
XCTAssertEqual(query.sortOrders[0].ascending, NO);
77-
XCTAssertEqualObjects(query.sortOrders[1].field.canonicalString, kDocumentKeyPath);
85+
XCTAssertEqual(query.sortOrders[1].field.CanonicalString(), FieldPath::kDocumentKeyPath);
7886
XCTAssertEqual(query.sortOrders[1].ascending, NO);
7987

8088
XCTAssertEqual(query.explicitSortOrders.count, 1);
81-
XCTAssertEqualObjects(query.explicitSortOrders[0].field.canonicalString, @"length");
89+
XCTAssertEqual(query.explicitSortOrders[0].field.CanonicalString(), "length");
8290
XCTAssertEqual(query.explicitSortOrders[0].ascending, NO);
8391
}
8492

@@ -211,7 +219,7 @@ - (void)testDoesNotMatchComplexObjectsForFilters {
211219

212220
- (void)testDoesntRemoveComplexObjectsWithOrderBy {
213221
FSTQuery *query1 = [FSTTestQuery(@"collection")
214-
queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"sort")
222+
queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort")
215223
ascending:YES]];
216224

217225
FSTDocument *doc1 = FSTTestDoc(@"collection/1", 0, @{ @"sort" : @2 }, NO);
@@ -305,9 +313,8 @@ - (void)assertCorrectComparisonsWithArray:(NSArray *)array comparator:(NSCompara
305313

306314
- (void)testSortsDocumentsInTheCorrectOrder {
307315
FSTQuery *query = FSTTestQuery(@"collection");
308-
query =
309-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"sort")
310-
ascending:YES]];
316+
query = [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort")
317+
ascending:YES]];
311318

312319
// clang-format off
313320
NSArray<FSTDocument *> *docs = @[
@@ -335,10 +342,10 @@ - (void)testSortsDocumentsInTheCorrectOrder {
335342
- (void)testSortsDocumentsUsingMultipleFields {
336343
FSTQuery *query = FSTTestQuery(@"collection");
337344
query =
338-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"sort1")
345+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort1")
339346
ascending:YES]];
340347
query =
341-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"sort2")
348+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort2")
342349
ascending:YES]];
343350

344351
// clang-format off
@@ -362,10 +369,10 @@ - (void)testSortsDocumentsUsingMultipleFields {
362369
- (void)testSortsDocumentsWithDescendingToo {
363370
FSTQuery *query = FSTTestQuery(@"collection");
364371
query =
365-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"sort1")
372+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort1")
366373
ascending:NO]];
367374
query =
368-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"sort2")
375+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("sort2")
369376
ascending:NO]];
370377

371378
// clang-format off

Firestore/Example/Tests/Core/FSTViewTests.mm

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030

3131
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
3232

33+
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
34+
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
35+
36+
namespace testutil = firebase::firestore::testutil;
37+
using firebase::firestore::model::ResourcePath;
38+
3339
NS_ASSUME_NONNULL_BEGIN
3440

3541
@interface FSTViewTests : XCTestCase
@@ -39,8 +45,7 @@ @implementation FSTViewTests
3945

4046
/** Returns a new empty query to use for testing. */
4147
- (FSTQuery *)queryForMessages {
42-
return [FSTQuery
43-
queryWithPath:[FSTResourcePath pathWithSegments:@[ @"rooms", @"eros", @"messages" ]]];
48+
return [FSTQuery queryWithPath:ResourcePath{"rooms", "eros", "messages"}];
4449
}
4550

4651
- (void)testAddsDocumentsBasedOnQuery {
@@ -128,7 +133,7 @@ - (void)testDoesNotReturnNilForFirstChanges {
128133
- (void)testFiltersDocumentsBasedOnQueryWithFilter {
129134
FSTQuery *query = [self queryForMessages];
130135
FSTRelationFilter *filter =
131-
[FSTRelationFilter filterWithField:FSTTestFieldPath(@"sort")
136+
[FSTRelationFilter filterWithField:testutil::Field("sort")
132137
filterOperator:FSTRelationFilterOperatorLessThanOrEqual
133138
value:[FSTDoubleValue doubleValue:2]];
134139
query = [query queryByAddingFilter:filter];
@@ -160,7 +165,7 @@ - (void)testFiltersDocumentsBasedOnQueryWithFilter {
160165
- (void)testUpdatesDocumentsBasedOnQueryWithFilter {
161166
FSTQuery *query = [self queryForMessages];
162167
FSTRelationFilter *filter =
163-
[FSTRelationFilter filterWithField:FSTTestFieldPath(@"sort")
168+
[FSTRelationFilter filterWithField:testutil::Field("sort")
164169
filterOperator:FSTRelationFilterOperatorLessThanOrEqual
165170
value:[FSTDoubleValue doubleValue:2]];
166171
query = [query queryByAddingFilter:filter];
@@ -232,9 +237,8 @@ - (void)testRemovesDocumentsForQueryWithLimit {
232237

233238
- (void)testDoesntReportChangesForDocumentBeyondLimitOfQuery {
234239
FSTQuery *query = [self queryForMessages];
235-
query =
236-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"num")
237-
ascending:YES]];
240+
query = [query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("num")
241+
ascending:YES]];
238242
query = [query queryBySettingLimit:2];
239243
FSTView *view = [[FSTView alloc] initWithQuery:query remoteDocuments:[FSTDocumentKeySet keySet]];
240244

@@ -385,7 +389,7 @@ - (void)testReturnsNeedsRefillOnDeleteInLimitQuery {
385389
- (void)testReturnsNeedsRefillOnReorderInLimitQuery {
386390
FSTQuery *query = [self queryForMessages];
387391
query =
388-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"order")
392+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("order")
389393
ascending:YES]];
390394
query = [query queryBySettingLimit:2];
391395
FSTDocument *doc1 = FSTTestDoc(@"rooms/eros/messages/0", 0, @{ @"order" : @1 }, NO);
@@ -419,7 +423,7 @@ - (void)testReturnsNeedsRefillOnReorderInLimitQuery {
419423
- (void)testDoesntNeedRefillOnReorderWithinLimit {
420424
FSTQuery *query = [self queryForMessages];
421425
query =
422-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"order")
426+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("order")
423427
ascending:YES]];
424428
query = [query queryBySettingLimit:3];
425429
FSTDocument *doc1 = FSTTestDoc(@"rooms/eros/messages/0", 0, @{ @"order" : @1 }, NO);
@@ -449,7 +453,7 @@ - (void)testDoesntNeedRefillOnReorderWithinLimit {
449453
- (void)testDoesntNeedRefillOnReorderAfterLimitQuery {
450454
FSTQuery *query = [self queryForMessages];
451455
query =
452-
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"order")
456+
[query queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("order")
453457
ascending:YES]];
454458
query = [query queryBySettingLimit:3];
455459
FSTDocument *doc1 = FSTTestDoc(@"rooms/eros/messages/0", 0, @{ @"order" : @1 }, NO);

Firestore/Example/Tests/Model/FSTPathTests.mm

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ - (void)testCanonicalStringOfSubstring {
195195
XCTAssertEqualObjects([[pathHead pathByRemovingFirstSegment] canonicalString], @"bar");
196196
}
197197

198+
- (void)testRoundTrip {
199+
FSTFieldPath *path = [FSTFieldPath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]];
200+
XCTAssertEqualObjects(path, [FSTFieldPath fieldPathWithCPPFieldPath:[path toCPPFieldPath]]);
201+
202+
const firebase::firestore::model::FieldPath cppPath{"rooms", "Eros", "messages"};
203+
XCTAssertEqual(cppPath, [[FSTFieldPath fieldPathWithCPPFieldPath:cppPath] toCPPFieldPath]);
204+
}
205+
206+
@end
207+
208+
@interface FSTResourcePathTests : XCTestCase
198209
@end
199210

211+
@implementation FSTResourcePathTests
212+
213+
- (void)testRoundTrip {
214+
FSTResourcePath *path = [FSTResourcePath pathWithSegments:@[ @"rooms", @"Eros", @"messages" ]];
215+
XCTAssertEqualObjects(path,
216+
[FSTResourcePath resourcePathWithCPPResourcePath:[path toCPPResourcePath]]);
217+
218+
const firebase::firestore::model::ResourcePath cppPath{"rooms", "Eros", "messages"};
219+
XCTAssertEqual(cppPath,
220+
[[FSTResourcePath resourcePathWithCPPResourcePath:cppPath] toCPPResourcePath]);
221+
}
222+
223+
@end
200224
NS_ASSUME_NONNULL_END

Firestore/Example/Tests/Remote/FSTSerializerBetaTests.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848

4949
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
5050
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
51+
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
5152

53+
namespace testutil = firebase::firestore::testutil;
5254
namespace util = firebase::firestore::util;
5355
using firebase::firestore::model::DatabaseId;
5456

@@ -581,7 +583,7 @@ - (void)unaryFilterTestWithValue:(id)value
581583

582584
- (void)testEncodesSortOrders {
583585
FSTQuery *q = [FSTTestQuery(@"docs")
584-
queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"prop")
586+
queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("prop")
585587
ascending:YES]];
586588
FSTQueryData *model = [self queryDataForQuery:q];
587589

@@ -601,7 +603,7 @@ - (void)testEncodesSortOrders {
601603

602604
- (void)testEncodesSortOrdersDescending {
603605
FSTQuery *q = [FSTTestQuery(@"rooms/1/messages/10/attachments")
604-
queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(@"prop")
606+
queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("prop")
605607
ascending:NO]];
606608
FSTQueryData *model = [self queryDataForQuery:q];
607609

Firestore/Example/Tests/Util/FSTHelpers.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
}
184184

185185
FSTQuery *FSTTestQuery(NSString *path) {
186-
return [FSTQuery queryWithPath:FSTTestPath(path)];
186+
return [FSTQuery queryWithPath:[FSTTestPath(path) toCPPResourcePath]];
187187
}
188188

189189
id<FSTFilter> FSTTestFilter(NSString *field, NSString *opString, id value) {
@@ -206,12 +206,12 @@
206206
FSTFieldValue *data = FSTTestFieldValue(value);
207207
if ([data isEqual:[FSTDoubleValue nanValue]]) {
208208
FSTCAssert(op == FSTRelationFilterOperatorEqual, @"Must use == with NAN.");
209-
return [[FSTNanFilter alloc] initWithField:path];
209+
return [[FSTNanFilter alloc] initWithField:[path toCPPFieldPath]];
210210
} else if ([data isEqual:[FSTNullValue nullValue]]) {
211211
FSTCAssert(op == FSTRelationFilterOperatorEqual, @"Must use == with Null.");
212-
return [[FSTNullFilter alloc] initWithField:path];
212+
return [[FSTNullFilter alloc] initWithField:[path toCPPFieldPath]];
213213
} else {
214-
return [FSTRelationFilter filterWithField:path filterOperator:op value:data];
214+
return [FSTRelationFilter filterWithField:[path toCPPFieldPath] filterOperator:op value:data];
215215
}
216216
}
217217

@@ -225,13 +225,14 @@
225225
} else {
226226
FSTCFail(@"Unsupported direction: %@", direction);
227227
}
228-
return [FSTSortOrder sortOrderWithFieldPath:path ascending:ascending];
228+
return [FSTSortOrder sortOrderWithFieldPath:[path toCPPFieldPath] ascending:ascending];
229229
}
230230

231231
NSComparator FSTTestDocComparator(NSString *fieldPath) {
232232
FSTQuery *query = [FSTTestQuery(@"docs")
233-
queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:FSTTestFieldPath(fieldPath)
234-
ascending:YES]];
233+
queryByAddingSortOrder:[FSTSortOrder
234+
sortOrderWithFieldPath:[FSTTestFieldPath(fieldPath) toCPPFieldPath]
235+
ascending:YES]];
235236
return [query comparator];
236237
}
237238

Firestore/Source/API/FIRCollectionReference.mm

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
#import "Firestore/Source/Util/FSTAssert.h"
2929
#import "Firestore/Source/Util/FSTUsageValidation.h"
3030

31+
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
32+
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
33+
34+
namespace util = firebase::firestore::util;
35+
using firebase::firestore::model::ResourcePath;
3136
using firebase::firestore::util::CreateAutoId;
3237

3338
NS_ASSUME_NONNULL_BEGIN
@@ -57,7 +62,8 @@ - (instancetype)initWithPath:(FSTResourcePath *)path firestore:(FIRFirestore *)f
5762
"number of segments, but %@ has %d",
5863
path.canonicalString, path.length);
5964
}
60-
self = [super initWithQuery:[FSTQuery queryWithPath:path] firestore:firestore];
65+
self =
66+
[super initWithQuery:[FSTQuery queryWithPath:[path toCPPResourcePath]] firestore:firestore];
6167
return self;
6268
}
6369

@@ -87,30 +93,33 @@ - (NSUInteger)hash {
8793
}
8894

8995
- (NSString *)collectionID {
90-
return [self.query.path lastSegment];
96+
return util::WrapNSString(self.query.path.last_segment());
9197
}
9298

9399
- (FIRDocumentReference *_Nullable)parent {
94-
FSTResourcePath *parentPath = [self.query.path pathByRemovingLastSegment];
95-
if (parentPath.isEmpty) {
100+
const ResourcePath parentPath = self.query.path.PopLast();
101+
if (parentPath.empty()) {
96102
return nil;
97103
} else {
98-
FSTDocumentKey *key = [FSTDocumentKey keyWithPath:parentPath];
104+
FSTDocumentKey *key =
105+
[FSTDocumentKey keyWithPath:[FSTResourcePath resourcePathWithCPPResourcePath:parentPath]];
99106
return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
100107
}
101108
}
102109

103110
- (NSString *)path {
104-
return [self.query.path canonicalString];
111+
return util::WrapNSString(self.query.path.CanonicalString());
105112
}
106113

107114
- (FIRDocumentReference *)documentWithPath:(NSString *)documentPath {
108115
if (!documentPath) {
109116
FSTThrowInvalidArgument(@"Document path cannot be nil.");
110117
}
111-
FSTResourcePath *subPath = [FSTResourcePath pathWithString:documentPath];
112-
FSTResourcePath *path = [self.query.path pathByAppendingPath:subPath];
113-
return [FIRDocumentReference referenceWithPath:path firestore:self.firestore];
118+
const ResourcePath subPath = ResourcePath::FromString(util::MakeStringView(documentPath));
119+
const ResourcePath path = self.query.path.Append(subPath);
120+
return
121+
[FIRDocumentReference referenceWithPath:[FSTResourcePath resourcePathWithCPPResourcePath:path]
122+
firestore:self.firestore];
114123
}
115124

116125
- (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)data {
@@ -126,9 +135,9 @@ - (FIRDocumentReference *)addDocumentWithData:(NSDictionary<NSString *, id> *)da
126135
}
127136

128137
- (FIRDocumentReference *)documentWithAutoID {
129-
NSString *autoID = [NSString stringWithUTF8String:CreateAutoId().c_str()];
138+
const ResourcePath path = self.query.path.Append(CreateAutoId());
130139
FSTDocumentKey *key =
131-
[FSTDocumentKey keyWithPath:[self.query.path pathByAppendingSegment:autoID]];
140+
[FSTDocumentKey keyWithPath:[FSTResourcePath resourcePathWithCPPResourcePath:path]];
132141
return [FIRDocumentReference referenceWithKey:key firestore:self.firestore];
133142
}
134143

Firestore/Source/API/FIRDocumentReference.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ - (void)getDocumentWithCompletion:(void (^)(FIRDocumentSnapshot *_Nullable docum
266266
addSnapshotListenerInternalWithOptions:(FSTListenOptions *)internalOptions
267267
listener:(FIRDocumentSnapshotBlock)listener {
268268
FIRFirestore *firestore = self.firestore;
269-
FSTQuery *query = [FSTQuery queryWithPath:self.key.path];
269+
FSTQuery *query = [FSTQuery queryWithPath:[self.key.path toCPPResourcePath]];
270270
FSTDocumentKey *key = self.key;
271271

272272
FSTViewSnapshotHandler snapshotHandler = ^(FSTViewSnapshot *snapshot, NSError *error) {

0 commit comments

Comments
 (0)