-
Notifications
You must be signed in to change notification settings - Fork 1.7k
C++ port: add C++ equivalent of FSTDocumentKey. #762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 47 commits
8004ad0
2080f96
05cf106
6c5a60e
880b115
5076872
0b49e35
f387147
286111e
6979f62
e5a0776
7b480b5
e8fa45f
608bcf0
fa5aa94
42c106c
0d89120
53e5564
e58bb49
7bb768e
60317fa
3a7c97e
62fdfcb
822ef1d
4e8aab4
cf43ba0
7761737
6404f06
ef584ec
068e15c
1a071a4
6c14417
0505370
bb39ef9
355f506
ad7582b
62e45a1
b93f2f4
868254f
dfd73cb
bb8006c
d672a0a
f288779
e106787
2ff6c49
6ae8395
8414bb8
fb710fd
4913889
2f52e1f
03163e5
0bd9898
d507494
6583dcc
12d37a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* | ||
| * 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/model/resource_path.h" | ||
| #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() : path_{std::make_shared<ResourcePath>()} { | ||
| } | ||
|
|
||
| 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_); | ||
| } | ||
|
|
||
| DocumentKey DocumentKey::FromPathString(const absl::string_view path) { | ||
| return DocumentKey{ResourcePath::FromString(path)}; | ||
| } | ||
|
|
||
| DocumentKey DocumentKey::FromSegments(std::initializer_list<std::string> list) { | ||
| return DocumentKey{ResourcePath{list}}; | ||
| } | ||
|
|
||
| const DocumentKey& DocumentKey::Empty() { | ||
| static DocumentKey empty; | ||
| return empty; | ||
| } | ||
|
|
||
| bool DocumentKey::IsDocumentKey(const ResourcePath& path) { | ||
| return path.size() % 2 == 0; | ||
| } | ||
|
|
||
| const ResourcePath& DocumentKey::path() const { | ||
| return path_ ? *path_ : Empty().path(); | ||
| } | ||
|
|
||
| bool operator==(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
| return lhs.path() == rhs.path(); | ||
| } | ||
| bool operator!=(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
| return lhs.path() != rhs.path(); | ||
| } | ||
| bool operator<(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
| return lhs.path() < rhs.path(); | ||
| } | ||
| bool operator<=(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
| return lhs.path() <= rhs.path(); | ||
| } | ||
| bool operator>(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
| return lhs.path() > rhs.path(); | ||
| } | ||
| bool operator>=(const DocumentKey& lhs, const DocumentKey& rhs) { | ||
| return lhs.path() >= rhs.path(); | ||
| } | ||
|
|
||
| } // namespace model | ||
| } // namespace firestore | ||
| } // namespace firebase | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * 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 "absl/strings/string_view.h" | ||
|
|
||
| namespace firebase { | ||
| namespace firestore { | ||
| namespace model { | ||
|
|
||
| class ResourcePath; | ||
|
||
|
|
||
| /** | ||
| * DocumentKey represents the location of a document in the Firestore database. | ||
| */ | ||
| class DocumentKey { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uber-nit: Some of your methods have a blank line after them (eg lines 39, 51, 58, etc) while others don't (eg 34,45, 68, etc). Consider unifying.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to group together more-or-less related methods. So I'm mildly in favor of keeping (provided it's not against the style guide), but not too much against removing as well.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAICT, it's not at all against the style guide. From the examples listed, the style guide usually omits blank lines after methods that are entirely contained on a single line, but adds them when the definition spans lines. (See guide for examples). But I don't think there's any requirement here. I'm fine with it as is.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please port comments: /** DocumentKey represents the location of a document in the Firestore database. */
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| public: | ||
| /** Creates a "blank" document key not associated with any document. */ | ||
| DocumentKey(); | ||
|
|
||
| /** 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(absl::string_view path); | ||
|
|
||
| /** Creates and returns a new document key with the given segments. */ | ||
| static DocumentKey FromSegments(std::initializer_list<std::string> 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); | ||
| /** The path to the document. */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing newline?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
| const ResourcePath& path() const; | ||
|
|
||
| private: | ||
| // This is an optimization to make passing DocumentKey around cheaper (it's | ||
| // copied often). | ||
| std::shared_ptr<const ResourcePath> path_; | ||
| }; | ||
|
|
||
| bool operator==(const DocumentKey& lhs, const DocumentKey& rhs); | ||
| bool operator!=(const DocumentKey& lhs, const DocumentKey& rhs); | ||
| bool operator<(const DocumentKey& lhs, const DocumentKey& rhs); | ||
| bool operator<=(const DocumentKey& lhs, const DocumentKey& rhs); | ||
| bool operator>(const DocumentKey& lhs, const DocumentKey& rhs); | ||
| bool operator>=(const DocumentKey& lhs, const DocumentKey& rhs); | ||
|
|
||
| } // namespace model | ||
| } // namespace firestore | ||
| } // namespace firebase | ||
|
|
||
| #endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,9 +30,6 @@ namespace model { | |
|
|
||
| namespace { | ||
|
|
||
| // TODO(varconst): move to C++ equivalent of FSTDocumentKey.{h,cc} | ||
| const char* const kDocumentKeyPath = "__name__"; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at other SDKs:
So it seemed easiest to just move it to the point of usage. Please let me know if there's a reason to have it inside DocumentKey/make it a global variable/etc.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to make organizational changes but when you notice inconsistencies like these please fix them :-).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be fair, there seem to be reasons for this inconsistencies. E.g., Java can't have a global variable, so the variable is wrapped into a class, whereas Typescript even has a nice syntax for exporting variables. |
||
|
|
||
| /** | ||
| * True if the string could be used as a segment in a field path without | ||
| * escaping. Valid identifies follow the regex [a-zA-Z_][a-zA-Z0-9_]* | ||
|
|
@@ -146,12 +143,12 @@ const FieldPath& FieldPath::EmptyPath() { | |
| } | ||
|
|
||
| const FieldPath& FieldPath::KeyFieldPath() { | ||
| static const FieldPath key_field_path{kDocumentKeyPath}; | ||
| static const FieldPath key_field_path{FieldPath::kDocumentKeyPath}; | ||
| return key_field_path; | ||
| } | ||
|
|
||
| bool FieldPath::IsKeyFieldPath() const { | ||
| return size() == 1 && first_segment() == kDocumentKeyPath; | ||
| return size() == 1 && first_segment() == FieldPath::kDocumentKeyPath; | ||
| } | ||
|
|
||
| std::string FieldPath::CanonicalString() const { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ namespace firebase { | |
| namespace firestore { | ||
| namespace model { | ||
|
|
||
| ResourcePath ResourcePath::Parse(const absl::string_view path) { | ||
| ResourcePath ResourcePath::FromString(const absl::string_view path) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to be more similar to other SDKs. |
||
| // NOTE: The client is ignorant of any path segments containing escape | ||
| // sequences (e.g. __id123__) and just passes them through raw (they exist | ||
| // for legacy reasons and should not be used frequently). | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be
static const DocumentKey empty?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.