Skip to content

Commit 6474a82

Browse files
authored
Firestore DatabaseId in C++ (firebase#727)
* Implement DataBaseId in C++ * add database_id_test to project * fix project * address changes * fix style
1 parent 05ef5ae commit 6474a82

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

Firestore/Example/Firestore.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
AB99452C1FE3018D00DFC1E6 /* FIRQuerySnapshotTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB99452B1FE3018D00DFC1E6 /* FIRQuerySnapshotTests.m */; };
7979
AB99452E1FE30AC800DFC1E6 /* FIRFieldValueTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AB99452D1FE30AC800DFC1E6 /* FIRFieldValueTests.m */; };
8080
ABAEEF4F1FD5F8B100C966CB /* FIRQueryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = ABAEEF4E1FD5F8B100C966CB /* FIRQueryTests.m */; };
81+
ABE6637A201FA81900ED349A /* database_id_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = AB71064B201FA60300344F18 /* database_id_test.cc */; };
8182
ABF341051FE860CA00C48322 /* FSTAPIHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = ABF341021FE8593500C48322 /* FSTAPIHelpers.m */; };
8283
ABF6506C201131F8005F2C74 /* timestamp_test.cc in Sources */ = {isa = PBXBuildFile; fileRef = ABF6506B201131F8005F2C74 /* timestamp_test.cc */; };
8384
AFE6114F0D4DAECBA7B7C089 /* Pods_Firestore_IntegrationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2FA635DF5D116A67A7441CD /* Pods_Firestore_IntegrationTests.framework */; };
@@ -257,6 +258,7 @@
257258
AB380D03201BC6E400D97691 /* ordered_code_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ordered_code_test.cc; path = ../../core/test/firebase/firestore/util/ordered_code_test.cc; sourceTree = "<group>"; };
258259
AB382F7B1FE02A1F007CA955 /* FIRDocumentReferenceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRDocumentReferenceTests.m; sourceTree = "<group>"; };
259260
AB382F7D1FE03059007CA955 /* FIRFieldPathTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRFieldPathTests.m; sourceTree = "<group>"; };
261+
AB71064B201FA60300344F18 /* database_id_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = database_id_test.cc; sourceTree = "<group>"; };
260262
AB7BAB332012B519001E0872 /* geo_point_test.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = geo_point_test.cc; path = ../../core/test/firebase/firestore/geo_point_test.cc; sourceTree = "<group>"; };
261263
AB9945251FE2D71100DFC1E6 /* FIRCollectionReferenceTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FIRCollectionReferenceTests.m; sourceTree = "<group>"; };
262264
AB9945271FE2DE0C00DFC1E6 /* FIRSnapshotMetadataTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FIRSnapshotMetadataTests.m; sourceTree = "<group>"; };
@@ -553,6 +555,7 @@
553555
children = (
554556
AB356EF6200EA5EB0089B766 /* field_value_test.cc */,
555557
ABF6506B201131F8005F2C74 /* timestamp_test.cc */,
558+
AB71064B201FA60300344F18 /* database_id_test.cc */,
556559
);
557560
name = model;
558561
path = ../../core/test/firebase/firestore/model;
@@ -1222,6 +1225,7 @@
12221225
files = (
12231226
DE2EF0881F3D0B6E003D0CDC /* FSTTreeSortedDictionaryTests.m in Sources */,
12241227
DE51B1FD1F0D492C0013853F /* FSTSpecTests.m in Sources */,
1228+
ABE6637A201FA81900ED349A /* database_id_test.cc in Sources */,
12251229
ABAEEF4F1FD5F8B100C966CB /* FIRQueryTests.m in Sources */,
12261230
ABF341051FE860CA00C48322 /* FSTAPIHelpers.m in Sources */,
12271231
DE51B1CC1F0D48C00013853F /* FIRGeoPointTests.m in Sources */,

Firestore/core/src/firebase/firestore/model/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
cc_library(
1616
firebase_firestore_model
1717
SOURCES
18+
database_id.cc
19+
database_id.h
1820
field_value.cc
21+
field_value.h
1922
timestamp.cc
23+
timestamp.h
2024
DEPENDS
25+
absl_strings
2126
firebase_firestore_util
2227
firebase_firestore_types
2328
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
18+
19+
#include "Firestore/core/src/firebase/firestore/util/firebase_assert.h"
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace model {
24+
25+
constexpr const char* DatabaseId::kDefaultDatabaseId;
26+
27+
DatabaseId::DatabaseId(const absl::string_view project_id,
28+
const absl::string_view database_id)
29+
: project_id_(project_id), database_id_(database_id) {
30+
FIREBASE_ASSERT(!project_id.empty());
31+
FIREBASE_ASSERT(!database_id.empty());
32+
}
33+
34+
bool DatabaseId::IsDefaultDatabase() {
35+
return database_id_ == kDefaultDatabaseId;
36+
}
37+
38+
} // namespace model
39+
} // namespace firestore
40+
} // namespace firebase
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DATABASE_ID_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DATABASE_ID_H_
19+
20+
#include <string>
21+
22+
#include "absl/strings/string_view.h"
23+
24+
namespace firebase {
25+
namespace firestore {
26+
namespace model {
27+
28+
/** A DatabaseId represents a particular database in the Firestore. */
29+
class DatabaseId {
30+
public:
31+
/** The default name for "unset" database ID in resource names. */
32+
static constexpr const char* kDefaultDatabaseId = "(default)";
33+
34+
/**
35+
* Creates and returns a new DatabaseId.
36+
*
37+
* @param project_id The project for the database.
38+
* @param database_id The database in the project to use.
39+
*/
40+
DatabaseId(const absl::string_view project_id,
41+
const absl::string_view database_id);
42+
43+
const std::string& project_id() const {
44+
return project_id_;
45+
}
46+
47+
const std::string& database_id() const {
48+
return database_id_;
49+
}
50+
51+
/** Whether this is the default database of the project. */
52+
bool IsDefaultDatabase();
53+
54+
friend bool operator<(const DatabaseId& lhs, const DatabaseId& rhs);
55+
56+
private:
57+
const std::string project_id_;
58+
const std::string database_id_;
59+
};
60+
61+
/** Compares against another DatabaseId. */
62+
inline bool operator<(const DatabaseId& lhs, const DatabaseId& rhs) {
63+
return lhs.project_id_ < rhs.project_id_ ||
64+
(lhs.project_id_ == rhs.project_id_ &&
65+
lhs.database_id_ < rhs.database_id_);
66+
}
67+
68+
inline bool operator>(const DatabaseId& lhs, const DatabaseId& rhs) {
69+
return rhs < lhs;
70+
}
71+
72+
inline bool operator>=(const DatabaseId& lhs, const DatabaseId& rhs) {
73+
return !(lhs < rhs);
74+
}
75+
76+
inline bool operator<=(const DatabaseId& lhs, const DatabaseId& rhs) {
77+
return !(lhs > rhs);
78+
}
79+
80+
inline bool operator!=(const DatabaseId& lhs, const DatabaseId& rhs) {
81+
return lhs < rhs || lhs > rhs;
82+
}
83+
84+
inline bool operator==(const DatabaseId& lhs, const DatabaseId& rhs) {
85+
return !(lhs != rhs);
86+
}
87+
88+
} // namespace model
89+
} // namespace firestore
90+
} // namespace firebase
91+
92+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DATABASE_ID_H_

Firestore/core/test/firebase/firestore/model/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
cc_test(
1616
firebase_firestore_model_test
1717
SOURCES
18+
database_id_test.cc
1819
field_value_test.cc
1920
timestamp_test.cc
2021
DEPENDS
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2018 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
18+
19+
#include "gtest/gtest.h"
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace model {
24+
25+
TEST(DatabaseId, Constructor) {
26+
DatabaseId id("p", "d");
27+
EXPECT_EQ("p", id.project_id());
28+
EXPECT_EQ("d", id.database_id());
29+
EXPECT_FALSE(id.IsDefaultDatabase());
30+
}
31+
32+
TEST(DatabaseId, DefaultDb) {
33+
DatabaseId id("p", DatabaseId::kDefaultDatabaseId);
34+
EXPECT_EQ("p", id.project_id());
35+
EXPECT_EQ(DatabaseId::kDefaultDatabaseId, id.database_id());
36+
EXPECT_TRUE(id.IsDefaultDatabase());
37+
}
38+
39+
TEST(DatabaseId, Comparison) {
40+
EXPECT_LT(DatabaseId("a", "b"), DatabaseId("b", "a"));
41+
EXPECT_LT(DatabaseId("a", "b"), DatabaseId("a", "c"));
42+
EXPECT_EQ(DatabaseId("a", "b"), DatabaseId("a", "b"));
43+
}
44+
45+
} // namespace model
46+
} // namespace firestore
47+
} // namespace firebase

0 commit comments

Comments
 (0)