forked from firebase/firebase-ios-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++ port: add C++ equivalent of FSTDocumentKey. (firebase#762)
Also move kDocumentKeyPath to the only point of usage - make it a static member variable of FieldPath.
- Loading branch information
Showing
11 changed files
with
326 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Firestore/core/src/firebase/firestore/model/document_key.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/document_key.h" | ||
|
||
#include <utility> | ||
|
||
#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
namespace { | ||
|
||
void AssertValidPath(const ResourcePath& path) { | ||
FIREBASE_ASSERT_MESSAGE(DocumentKey::IsDocumentKey(path), | ||
"invalid document key path: %s", | ||
path.CanonicalString().c_str()); | ||
} | ||
|
||
} // namespace | ||
|
||
DocumentKey::DocumentKey(const ResourcePath& path) | ||
: path_{std::make_shared<ResourcePath>(path)} { | ||
AssertValidPath(*path_); | ||
} | ||
|
||
DocumentKey::DocumentKey(ResourcePath&& path) | ||
: path_{std::make_shared<ResourcePath>(std::move(path))} { | ||
AssertValidPath(*path_); | ||
} | ||
|
||
const DocumentKey& DocumentKey::Empty() { | ||
static const DocumentKey empty; | ||
return empty; | ||
} | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase |
101 changes: 101 additions & 0 deletions
101
Firestore/core/src/firebase/firestore/model/document_key.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2018 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_ | ||
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_ | ||
|
||
#include <initializer_list> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include "Firestore/core/src/firebase/firestore/model/resource_path.h" | ||
#include "absl/strings/string_view.h" | ||
|
||
namespace firebase { | ||
namespace firestore { | ||
namespace model { | ||
|
||
/** | ||
* DocumentKey represents the location of a document in the Firestore database. | ||
*/ | ||
class DocumentKey { | ||
public: | ||
/** Creates a "blank" document key not associated with any document. */ | ||
DocumentKey() : path_{std::make_shared<ResourcePath>()} { | ||
} | ||
|
||
/** Creates a new document key containing a copy of the given path. */ | ||
explicit DocumentKey(const ResourcePath& path); | ||
|
||
/** Creates a new document key, taking ownership of the given path. */ | ||
explicit DocumentKey(ResourcePath&& path); | ||
|
||
/** | ||
* Creates and returns a new document key using '/' to split the string into | ||
* segments. | ||
*/ | ||
static DocumentKey FromPathString(const absl::string_view path) { | ||
return DocumentKey{ResourcePath::FromString(path)}; | ||
} | ||
|
||
/** Creates and returns a new document key with the given segments. */ | ||
static DocumentKey FromSegments(std::initializer_list<std::string> list) { | ||
return DocumentKey{ResourcePath{list}}; | ||
} | ||
|
||
/** Returns a shared instance of an empty document key. */ | ||
static const DocumentKey& Empty(); | ||
|
||
/** Returns true iff the given path is a path to a document. */ | ||
static bool IsDocumentKey(const ResourcePath& path) { | ||
return path.size() % 2 == 0; | ||
} | ||
|
||
/** The path to the document. */ | ||
const ResourcePath& path() const { | ||
return path_ ? *path_ : Empty().path(); | ||
} | ||
|
||
private: | ||
// This is an optimization to make passing DocumentKey around cheaper (it's | ||
// copied often). | ||
std::shared_ptr<const ResourcePath> path_; | ||
}; | ||
|
||
inline bool operator==(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
return lhs.path() == rhs.path(); | ||
} | ||
inline bool operator!=(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
return lhs.path() != rhs.path(); | ||
} | ||
inline bool operator<(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
return lhs.path() < rhs.path(); | ||
} | ||
inline bool operator<=(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
return lhs.path() <= rhs.path(); | ||
} | ||
inline bool operator>(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
return lhs.path() > rhs.path(); | ||
} | ||
inline bool operator>=(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
return lhs.path() >= rhs.path(); | ||
} | ||
|
||
} // namespace model | ||
} // namespace firestore | ||
} // namespace firebase | ||
|
||
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.