Skip to content

Commit dcd1fea

Browse files
authored
operator== and operator!= for SetOptions. (#606)
* operator== and operator!= for SetOptions. * Address comments. * Return a reference. * Fix lint warning.
1 parent f331295 commit dcd1fea

File tree

8 files changed

+96
-12
lines changed

8 files changed

+96
-12
lines changed

firestore/integration_test_internal/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ set(FIREBASE_INTEGRATION_TEST_PORTABLE_TEST_SRCS
9595
src/query_test.cc
9696
src/sanity_test.cc
9797
src/server_timestamp_test.cc
98+
src/set_options_test.cc
9899
src/settings_test.cc
99100
src/smoke_test.cc
100101
src/source_test.cc
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 Google LLC
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 "firebase/firestore.h"
18+
19+
#include "gtest/gtest.h"
20+
21+
namespace firebase {
22+
namespace firestore {
23+
namespace {
24+
25+
TEST(SetOptionsTest, Equality) {
26+
SetOptions opt1 = SetOptions();
27+
SetOptions opt2 = SetOptions();
28+
SetOptions opt3 = SetOptions::Merge();
29+
SetOptions opt4 = SetOptions::Merge();
30+
SetOptions opt5 = SetOptions::MergeFields({"a", "a", "b", "c.d"});
31+
SetOptions opt6 = SetOptions::MergeFields({"c.d", "b", "a", "b"});
32+
SetOptions opt7 = SetOptions::MergeFields({"b"});
33+
34+
EXPECT_TRUE(opt1 == opt1);
35+
EXPECT_TRUE(opt1 == opt2);
36+
EXPECT_TRUE(opt1 != opt3);
37+
EXPECT_TRUE(opt1 != opt4);
38+
EXPECT_TRUE(opt1 != opt5);
39+
EXPECT_TRUE(opt1 != opt6);
40+
EXPECT_TRUE(opt1 != opt7);
41+
EXPECT_TRUE(opt3 == opt4);
42+
EXPECT_TRUE(opt3 != opt5);
43+
EXPECT_TRUE(opt3 != opt6);
44+
EXPECT_TRUE(opt3 != opt7);
45+
EXPECT_TRUE(opt5 == opt6);
46+
EXPECT_TRUE(opt5 != opt7);
47+
48+
EXPECT_FALSE(opt1 != opt1);
49+
EXPECT_FALSE(opt1 != opt2);
50+
EXPECT_FALSE(opt1 == opt3);
51+
EXPECT_FALSE(opt1 == opt4);
52+
EXPECT_FALSE(opt1 == opt5);
53+
EXPECT_FALSE(opt1 == opt6);
54+
EXPECT_FALSE(opt1 == opt7);
55+
EXPECT_FALSE(opt3 != opt4);
56+
EXPECT_FALSE(opt3 == opt5);
57+
EXPECT_FALSE(opt3 == opt6);
58+
EXPECT_FALSE(opt3 == opt7);
59+
EXPECT_FALSE(opt5 != opt6);
60+
EXPECT_FALSE(opt5 == opt7);
61+
}
62+
63+
} // namespace
64+
} // namespace firestore
65+
} // namespace firebase

firestore/src/common/set_options.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,30 @@
2121
namespace firebase {
2222
namespace firestore {
2323

24-
SetOptions::SetOptions(Type type, std::vector<FieldPath> fields)
24+
SetOptions::SetOptions(Type type, std::unordered_set<FieldPath> fields)
2525
: type_(type), fields_(firebase::Move(fields)) {}
2626

2727
SetOptions::~SetOptions() {}
2828

2929
/* static */
3030
SetOptions SetOptions::Merge() {
31-
return SetOptions{Type::kMergeAll, std::vector<FieldPath>{}};
31+
return SetOptions{Type::kMergeAll, std::unordered_set<FieldPath>{}};
3232
}
3333

3434
/* static */
3535
SetOptions SetOptions::MergeFields(const std::vector<std::string>& fields) {
36-
std::vector<FieldPath> field_paths;
36+
std::unordered_set<FieldPath> field_paths;
3737
field_paths.reserve(fields.size());
3838
for (const std::string& field : fields) {
39-
field_paths.push_back(FieldPath::FromDotSeparatedString(field));
39+
field_paths.insert(FieldPath::FromDotSeparatedString(field));
4040
}
4141
return SetOptions{Type::kMergeSpecific, firebase::Move(field_paths)};
4242
}
4343

4444
/* static */
4545
SetOptions SetOptions::MergeFieldPaths(const std::vector<FieldPath>& fields) {
46-
return SetOptions{Type::kMergeSpecific, fields};
46+
return SetOptions{Type::kMergeSpecific, std::unordered_set<FieldPath>(
47+
fields.begin(), fields.end())};
4748
}
4849

4950
} // namespace firestore

firestore/src/include/firebase/firestore/set_options.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define FIREBASE_FIRESTORE_SRC_INCLUDE_FIREBASE_FIRESTORE_SET_OPTIONS_H_
1919

2020
#include <string>
21+
#include <unordered_set>
2122
#include <vector>
2223

2324
#include "firebase/firestore/field_path.h"
@@ -135,14 +136,25 @@ class SetOptions final {
135136
static SetOptions MergeFieldPaths(const std::vector<FieldPath>& fields);
136137

137138
private:
139+
friend bool operator==(const SetOptions& lhs, const SetOptions& rhs);
138140
friend class SetOptionsInternal;
139141

140-
SetOptions(Type type, std::vector<FieldPath> fields);
142+
SetOptions(Type type, std::unordered_set<FieldPath> fields);
141143

142144
Type type_ = Type::kOverwrite;
143-
std::vector<FieldPath> fields_;
145+
std::unordered_set<FieldPath> fields_;
144146
};
145147

148+
/** Checks `lhs` and `rhs` for equality. */
149+
inline bool operator==(const SetOptions& lhs, const SetOptions& rhs) {
150+
return lhs.type_ == rhs.type_ && lhs.fields_ == rhs.fields_;
151+
}
152+
153+
/** Checks `lhs` and `rhs` for inequality. */
154+
inline bool operator!=(const SetOptions& lhs, const SetOptions& rhs) {
155+
return !(lhs == rhs);
156+
}
157+
146158
} // namespace firestore
147159
} // namespace firebase
148160

firestore/src/main/set_options_main.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef FIREBASE_FIRESTORE_SRC_MAIN_SET_OPTIONS_MAIN_H_
1818
#define FIREBASE_FIRESTORE_SRC_MAIN_SET_OPTIONS_MAIN_H_
1919

20+
#include <unordered_set>
2021
#include <utility>
2122

2223
#include "firestore/src/include/firebase/firestore/set_options.h"
@@ -41,7 +42,9 @@ class SetOptionsInternal {
4142
: options_{std::move(options)} {}
4243

4344
Type type() const { return options_.type_; }
44-
const std::vector<FieldPath>& field_mask() const { return options_.fields_; }
45+
const std::unordered_set<FieldPath>& field_mask() const {
46+
return options_.fields_;
47+
}
4548

4649
private:
4750
SetOptions options_;

firestore/src/main/user_data_converter_main.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void ParseNumericIncrement(const FieldValue& value, ParseContext&& context) {
157157
}
158158

159159
FieldMask CreateFieldMask(const ParseAccumulator& accumulator,
160-
const std::vector<FieldPath>& field_paths) {
160+
const std::unordered_set<FieldPath>& field_paths) {
161161
std::set<model::FieldPath> validated;
162162

163163
for (const FieldPath& public_path : field_paths) {
@@ -240,7 +240,8 @@ ParsedSetData UserDataConverter::ParseSetData(
240240

241241
ParsedSetData UserDataConverter::ParseMergeData(
242242
const MapFieldValue& input,
243-
const absl::optional<std::vector<FieldPath>>& maybe_field_mask) const {
243+
const absl::optional<std::unordered_set<FieldPath>>& maybe_field_mask)
244+
const {
244245
ParseAccumulator accumulator{UserDataSource::MergeSet};
245246

246247
auto update_data = ParseMap(input, accumulator.RootContext());

firestore/src/main/user_data_converter_main.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef FIREBASE_FIRESTORE_SRC_MAIN_USER_DATA_CONVERTER_MAIN_H_
1818
#define FIREBASE_FIRESTORE_SRC_MAIN_USER_DATA_CONVERTER_MAIN_H_
1919

20+
#include <unordered_set>
2021
#include <utility>
2122
#include <vector>
2223

@@ -78,7 +79,7 @@ class UserDataConverter {
7879
/** Parse document data from a merge `SetData` call. */
7980
core::ParsedSetData ParseMergeData(
8081
const MapFieldValue& input,
81-
const absl::optional<std::vector<FieldPath>>& field_mask =
82+
const absl::optional<std::unordered_set<FieldPath>>& field_mask =
8283
absl::nullopt) const;
8384

8485
/**

release_build_files/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ code.
571571
- Changes
572572
- General: Updating Android and iOS dependencies to the latest.
573573
- Firestore: Added `operator==` and `operator!=` for `SnapshotMetadata`,
574-
`Settings`, `QuerySnapshot`, and `DocumentSnapshot`.
574+
`Settings`, `QuerySnapshot`, `DocumentSnapshot`, and `SetOptions`.
575575

576576
### 8.3.0
577577
- Changes

0 commit comments

Comments
 (0)