Skip to content

Commit 84b0e7d

Browse files
committed
Add ArrayContainsFilter
1 parent 3ada38a commit 84b0e7d

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
cc_library(
1616
firebase_firestore_core
1717
SOURCES
18+
array_contains_filter.cc
19+
array_contains_filter.h
1820
database_info.cc
1921
database_info.h
2022
direction.cc
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 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/core/array_contains_filter.h"
18+
19+
#include <utility>
20+
21+
#include "absl/algorithm/container.h"
22+
23+
namespace firebase {
24+
namespace firestore {
25+
namespace core {
26+
27+
using model::Document;
28+
using model::FieldPath;
29+
using model::FieldValue;
30+
31+
using Operator = Filter::Operator;
32+
33+
ArrayContainsFilter::ArrayContainsFilter(FieldPath field, FieldValue value)
34+
: FieldFilter(std::move(field), Operator::ArrayContains, std::move(value)) {
35+
}
36+
37+
bool ArrayContainsFilter::Matches(const Document& doc) const {
38+
absl::optional<FieldValue> maybe_lhs = doc.field(field());
39+
if (!maybe_lhs) return false;
40+
41+
const FieldValue& lhs = *maybe_lhs;
42+
if (lhs.type() != FieldValue::Type::Array) return false;
43+
44+
const FieldValue::Array& contents = lhs.array_value();
45+
return absl::c_linear_search(contents, value());
46+
}
47+
48+
} // namespace core
49+
} // namespace firestore
50+
} // namespace firebase
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019 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_CORE_ARRAY_CONTAINS_FILTER_H_
18+
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_CORE_ARRAY_CONTAINS_FILTER_H_
19+
20+
#include <string>
21+
22+
#include "Firestore/core/src/firebase/firestore/core/field_filter.h"
23+
#include "Firestore/core/src/firebase/firestore/model/field_path.h"
24+
25+
namespace firebase {
26+
namespace firestore {
27+
namespace core {
28+
29+
/**
30+
* A Filter that implements the array-contains operator.
31+
*/
32+
class ArrayContainsFilter : public FieldFilter {
33+
public:
34+
ArrayContainsFilter() = default;
35+
36+
ArrayContainsFilter(model::FieldPath field, model::FieldValue value);
37+
38+
bool Matches(const model::Document& doc) const override;
39+
};
40+
41+
} // namespace core
42+
} // namespace firestore
43+
} // namespace firebase
44+
45+
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_CORE_ARRAY_CONTAINS_FILTER_H_

0 commit comments

Comments
 (0)