Skip to content

Commit 2d9d3a8

Browse files
authored
adding converters between DocumentKey (#900)
* naively remove FSTPath import and source/test files. * port FieldPath, part I * port FieldPath, part II * port ResourcePath, part I * port ResourcePath, part II * the grand commit to fix build errors * use testutil:: helper instead of those from FSTHelpers * fix test and lint * use c_str in errmsg directly * fix * fix * make code clean * fix integration test I missed * fix to avoid naming collision in preprocessor * address changes * address changes * address changes * fix: fieldMask are actually shared with different context. * address changes * add converter function between two DocumentKey implementations * add unit test * address changes * fix lint
1 parent 35de3c5 commit 2d9d3a8

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

Firestore/Example/Tests/Model/FSTDocumentKeyTests.mm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
#import <XCTest/XCTest.h>
2020

21+
#include "Firestore/core/src/firebase/firestore/model/document_key.h"
2122
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
2223

24+
using firebase::firestore::model::DocumentKey;
2325
using firebase::firestore::model::ResourcePath;
2426

2527
NS_ASSUME_NONNULL_BEGIN
@@ -56,6 +58,15 @@ - (void)testComparison {
5658
XCTAssertEqual(NSOrderedDescending, [ab compare:a]);
5759
}
5860

61+
- (void)testConverter {
62+
const ResourcePath path{"rooms", "firestore", "messages", "1"};
63+
FSTDocumentKey *objcKey = [FSTDocumentKey keyWithPath:path];
64+
XCTAssertEqualObjects(objcKey, (FSTDocumentKey *)(DocumentKey{objcKey}));
65+
66+
DocumentKey cpp_key{path};
67+
XCTAssertEqual(cpp_key, DocumentKey{(FSTDocumentKey *)(cpp_key)});
68+
}
69+
5970
@end
6071

6172
NS_ASSUME_NONNULL_END

Firestore/Source/Model/FSTDocumentKey.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ + (BOOL)isDocumentKey:(const ResourcePath &)path {
101101
return path.size() % 2 == 0;
102102
}
103103

104-
- (const firebase::firestore::model::ResourcePath &)path {
104+
- (const ResourcePath &)path {
105105
return _path;
106106
}
107107

Firestore/core/src/firebase/firestore/model/document_key.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <memory>
2222
#include <string>
2323

24+
#if defined(__OBJC__)
25+
#import "Firestore/Source/Model/FSTDocumentKey.h"
26+
#endif // defined(__OBJC__)
27+
2428
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
2529
#include "absl/strings/string_view.h"
2630

@@ -43,6 +47,16 @@ class DocumentKey {
4347
/** Creates a new document key, taking ownership of the given path. */
4448
explicit DocumentKey(ResourcePath&& path);
4549

50+
#if defined(__OBJC__)
51+
DocumentKey(FSTDocumentKey* key) // NOLINT(runtime/explicit)
52+
: path_(std::make_shared<ResourcePath>(key.path)) {
53+
}
54+
55+
operator FSTDocumentKey*() const {
56+
return [FSTDocumentKey keyWithPath:path()];
57+
}
58+
#endif
59+
4660
/**
4761
* Creates and returns a new document key using '/' to split the string into
4862
* segments.
@@ -69,6 +83,11 @@ class DocumentKey {
6983
return path_ ? *path_ : Empty().path();
7084
}
7185

86+
#if defined(__OBJC__)
87+
// Helper function to convert to FSTDocumentKey during the C++ migration.
88+
FSTDocumentKey* ToFSTDocumentKey() const;
89+
#endif // defined(__OBJC__)
90+
7291
private:
7392
// This is an optimization to make passing DocumentKey around cheaper (it's
7493
// copied often).

0 commit comments

Comments
 (0)