From 2d9d3a868c996d38b4a8bf6d3ce55ee76af43d75 Mon Sep 17 00:00:00 2001 From: zxu Date: Thu, 15 Mar 2018 11:31:16 -0400 Subject: 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 --- Firestore/Example/Tests/Model/FSTDocumentKeyTests.mm | 11 +++++++++++ Firestore/Source/Model/FSTDocumentKey.mm | 2 +- .../core/src/firebase/firestore/model/document_key.h | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Firestore/Example/Tests/Model/FSTDocumentKeyTests.mm b/Firestore/Example/Tests/Model/FSTDocumentKeyTests.mm index 5992b42..5e465f7 100644 --- a/Firestore/Example/Tests/Model/FSTDocumentKeyTests.mm +++ b/Firestore/Example/Tests/Model/FSTDocumentKeyTests.mm @@ -18,8 +18,10 @@ #import +#include "Firestore/core/src/firebase/firestore/model/document_key.h" #include "Firestore/core/src/firebase/firestore/model/resource_path.h" +using firebase::firestore::model::DocumentKey; using firebase::firestore::model::ResourcePath; NS_ASSUME_NONNULL_BEGIN @@ -56,6 +58,15 @@ NS_ASSUME_NONNULL_BEGIN XCTAssertEqual(NSOrderedDescending, [ab compare:a]); } +- (void)testConverter { + const ResourcePath path{"rooms", "firestore", "messages", "1"}; + FSTDocumentKey *objcKey = [FSTDocumentKey keyWithPath:path]; + XCTAssertEqualObjects(objcKey, (FSTDocumentKey *)(DocumentKey{objcKey})); + + DocumentKey cpp_key{path}; + XCTAssertEqual(cpp_key, DocumentKey{(FSTDocumentKey *)(cpp_key)}); +} + @end NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Model/FSTDocumentKey.mm b/Firestore/Source/Model/FSTDocumentKey.mm index 269748f..7425812 100644 --- a/Firestore/Source/Model/FSTDocumentKey.mm +++ b/Firestore/Source/Model/FSTDocumentKey.mm @@ -101,7 +101,7 @@ NS_ASSUME_NONNULL_BEGIN return path.size() % 2 == 0; } -- (const firebase::firestore::model::ResourcePath &)path { +- (const ResourcePath &)path { return _path; } diff --git a/Firestore/core/src/firebase/firestore/model/document_key.h b/Firestore/core/src/firebase/firestore/model/document_key.h index 6eb1e18..8e590d8 100644 --- a/Firestore/core/src/firebase/firestore/model/document_key.h +++ b/Firestore/core/src/firebase/firestore/model/document_key.h @@ -21,6 +21,10 @@ #include #include +#if defined(__OBJC__) +#import "Firestore/Source/Model/FSTDocumentKey.h" +#endif // defined(__OBJC__) + #include "Firestore/core/src/firebase/firestore/model/resource_path.h" #include "absl/strings/string_view.h" @@ -43,6 +47,16 @@ class DocumentKey { /** Creates a new document key, taking ownership of the given path. */ explicit DocumentKey(ResourcePath&& path); +#if defined(__OBJC__) + DocumentKey(FSTDocumentKey* key) // NOLINT(runtime/explicit) + : path_(std::make_shared(key.path)) { + } + + operator FSTDocumentKey*() const { + return [FSTDocumentKey keyWithPath:path()]; + } +#endif + /** * Creates and returns a new document key using '/' to split the string into * segments. @@ -69,6 +83,11 @@ class DocumentKey { return path_ ? *path_ : Empty().path(); } +#if defined(__OBJC__) + // Helper function to convert to FSTDocumentKey during the C++ migration. + FSTDocumentKey* ToFSTDocumentKey() const; +#endif // defined(__OBJC__) + private: // This is an optimization to make passing DocumentKey around cheaper (it's // copied often). -- cgit v1.2.3