Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 02274e7

Browse files
author
Kevin Li
authored
[core] Implement 'in' expression. (#16162)
* Implement in.cpp * Fix review comments. * Add expression_equality test for 'in' * Fix review comments. * [core] Update changelog. * [core] Update mapbox-gl-js * [core] Ignore render-tests/debug/padding * [core] Update baseline.
1 parent 59294aa commit 02274e7

File tree

12 files changed

+225
-14
lines changed

12 files changed

+225
-14
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222

2323
The `within expression` enables checking whether a feature is inside a pre-defined geometry set/boundary or not. This `within expression` returns a boolean value, `true` indicates that the feature being evaluated is inside the geometry set. The returned value can be then consumed as input by another expression or used directly by a paint/layer property.
2424

25-
Support for using `within expression` with layout propery will be implemented separately.
25+
Support for using `within expression` with layout property will be implemented separately.
2626

27-
- [core] Add support for using `within expression` with layout propery. ([#16194](https://github.com/mapbox/mapbox-gl-native/pull/16194))
27+
- [core] Add support for using `within expression` with layout property. ([#16194](https://github.com/mapbox/mapbox-gl-native/pull/16194))
28+
29+
- [core] Add support for `in expression`. ([#16162](https://github.com/mapbox/mapbox-gl-native/pull/16162))
30+
31+
The `in expression` enables checking whether a Number/String/Boolean type item is in a String/Array and returns a boolean value.
2832

2933
### 🐞 Bug fixes
3034

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ add_library(
195195
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/get_covering_stops.hpp
196196
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/image.hpp
197197
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/image_expression.hpp
198+
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/in.hpp
198199
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/interpolate.hpp
199200
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/interpolator.hpp
200201
${PROJECT_SOURCE_DIR}/include/mbgl/style/expression/is_constant.hpp
@@ -586,6 +587,7 @@ add_library(
586587
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/get_covering_stops.cpp
587588
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/image.cpp
588589
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/image_expression.cpp
590+
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/in.cpp
589591
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/interpolate.cpp
590592
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/is_constant.cpp
591593
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/is_expression.cpp

include/mbgl/style/expression/expression.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ enum class Kind : int32_t {
168168
FormatSectionOverride,
169169
NumberFormat,
170170
ImageExpression,
171+
In,
171172
Within
172173
};
173174

include/mbgl/style/expression/in.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <mbgl/style/conversion.hpp>
4+
#include <mbgl/style/expression/expression.hpp>
5+
#include <memory>
6+
7+
namespace mbgl {
8+
namespace style {
9+
namespace expression {
10+
11+
class In final : public Expression {
12+
public:
13+
In(std::unique_ptr<Expression> needle_, std::unique_ptr<Expression> haystack_);
14+
15+
static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);
16+
17+
EvaluationResult evaluate(const EvaluationContext& params) const override;
18+
void eachChild(const std::function<void(const Expression&)>&) const override;
19+
20+
bool operator==(const Expression& e) const override;
21+
22+
std::vector<optional<Value>> possibleOutputs() const override;
23+
24+
std::string getOperator() const override;
25+
26+
private:
27+
std::unique_ptr<Expression> needle;
28+
std::unique_ptr<Expression> haystack;
29+
};
30+
31+
} // namespace expression
32+
} // namespace style
33+
} // namespace mbgl

mapbox-gl-js

Submodule mapbox-gl-js updated 41 files

metrics/binary-size/macos-xcode11/metrics.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
[
44
"mbgl-glfw",
55
"/tmp/attach/install/macos-xcode11-release/bin/mbgl-glfw",
6-
5616056
6+
5677792
77
],
88
[
99
"mbgl-offline",
1010
"/tmp/attach/install/macos-xcode11-release/bin/mbgl-offline",
11-
5484604
11+
5542252
1212
],
1313
[
1414
"mbgl-render",
1515
"/tmp/attach/install/macos-xcode11-release/bin/mbgl-render",
16-
5530720
16+
5588368
1717
]
1818
]
1919
}

metrics/ignores/platform-all.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
{
22
"expression-tests/collator/accent-equals-de": "Locale-specific behavior changes based on platform.",
3-
"expression-tests/in/assert-array": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
4-
"expression-tests/in/assert-string": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
5-
"expression-tests/in/basic-array": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
6-
"expression-tests/in/basic-string": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
7-
"expression-tests/in/invalid-haystack": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
8-
"expression-tests/in/invalid-needle": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
93
"expression-tests/interpolate-hcl/linear": "https://github.com/mapbox/mapbox-gl-native/issues/8720",
104
"expression-tests/interpolate-lab/linear": "https://github.com/mapbox/mapbox-gl-native/issues/8720",
115
"expression-tests/is-supported-script/default": "This tests RTL text plugin behavior specific to GL JS",
@@ -68,6 +62,12 @@
6862
"render-tests/custom-layer-js/tent-3d": "skip - js specific",
6963
"render-tests/debug/collision": "https://github.com/mapbox/mapbox-gl-native/issues/3841",
7064
"render-tests/debug/overdraw": "https://github.com/mapbox/mapbox-gl-native/issues/15638",
65+
"render-tests/debug/padding/ease-to-btm-distort": "https://github.com/mapbox/mapbox-gl-native/issues/16212",
66+
"render-tests/debug/padding/ease-to-left-distort": "https://github.com/mapbox/mapbox-gl-native/issues/16212",
67+
"render-tests/debug/padding/ease-to-no-distort": "https://github.com/mapbox/mapbox-gl-native/issues/16212",
68+
"render-tests/debug/padding/ease-to-right-distort": "https://github.com/mapbox/mapbox-gl-native/issues/16212",
69+
"render-tests/debug/padding/ease-to-top-distort": "https://github.com/mapbox/mapbox-gl-native/issues/16212",
70+
"render-tests/debug/padding/set-padding": "https://github.com/mapbox/mapbox-gl-native/issues/16212",
7171
"render-tests/debug/raster": "https://github.com/mapbox/mapbox-gl-native/issues/15510",
7272
"render-tests/debug/tile": "https://github.com/mapbox/mapbox-gl-native/issues/3841",
7373
"render-tests/debug/tile-overscaled": "https://github.com/mapbox/mapbox-gl-native/issues/3841",

src/mbgl/style/expression/in.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#include <string.h>
2+
#include <mbgl/style/conversion_impl.hpp>
3+
#include <mbgl/style/expression/in.hpp>
4+
#include <mbgl/style/expression/type.hpp>
5+
#include <mbgl/util/string.hpp>
6+
7+
namespace mbgl {
8+
namespace style {
9+
namespace expression {
10+
11+
namespace {
12+
bool isComparableType(type::Type type) {
13+
return type == type::Boolean || type == type::String || type == type::Number || type == type::Null ||
14+
type == type::Value;
15+
}
16+
17+
bool isComparableRuntimeType(type::Type type) {
18+
return type == type::Boolean || type == type::String || type == type::Number || type == type::Null;
19+
}
20+
21+
bool isSearchableType(type::Type type) {
22+
return type == type::String || type.is<type::Array>() || type == type::Null || type == type::Value;
23+
}
24+
25+
bool isSearchableRuntimeType(type::Type type) {
26+
return type == type::String || type.is<type::Array>() || type == type::Null;
27+
}
28+
} // namespace
29+
30+
In::In(std::unique_ptr<Expression> needle_, std::unique_ptr<Expression> haystack_)
31+
: Expression(Kind::In, type::Boolean), needle(std::move(needle_)), haystack(std::move(haystack_)) {
32+
assert(isComparableType(needle->getType()));
33+
assert(isSearchableType(haystack->getType()));
34+
}
35+
36+
EvaluationResult In::evaluate(const EvaluationContext& params) const {
37+
const EvaluationResult evaluatedHaystack = haystack->evaluate(params);
38+
if (!evaluatedHaystack) {
39+
return evaluatedHaystack.error();
40+
}
41+
42+
const EvaluationResult evaluatedNeedle = needle->evaluate(params);
43+
if (!evaluatedNeedle) {
44+
return evaluatedNeedle.error();
45+
}
46+
47+
type::Type evaluatedNeedleType = typeOf(*evaluatedNeedle);
48+
if (!isComparableRuntimeType(evaluatedNeedleType)) {
49+
return EvaluationError{"Expected first argument to be of type boolean, string or number, but found " +
50+
toString(evaluatedNeedleType) + " instead."};
51+
}
52+
53+
type::Type evaluatedHaystackType = typeOf(*evaluatedHaystack);
54+
if (!isSearchableRuntimeType(evaluatedHaystackType)) {
55+
return EvaluationError{"Expected second argument to be of type array or string, but found " +
56+
toString(evaluatedHaystackType) + " instead."};
57+
}
58+
59+
if (evaluatedNeedleType == type::Null || evaluatedHaystackType == type::Null) {
60+
return EvaluationResult(false);
61+
}
62+
63+
if (evaluatedHaystackType == type::String) {
64+
const auto haystackString = evaluatedHaystack->get<std::string>();
65+
const auto needleString = toString(*evaluatedNeedle);
66+
return EvaluationResult(haystackString.find(needleString) != std::string::npos);
67+
} else {
68+
const auto haystackArray = evaluatedHaystack->get<std::vector<Value>>();
69+
return EvaluationResult(std::find(haystackArray.begin(), haystackArray.end(), *evaluatedNeedle) !=
70+
haystackArray.end());
71+
}
72+
}
73+
74+
void In::eachChild(const std::function<void(const Expression&)>& visit) const {
75+
visit(*needle);
76+
visit(*haystack);
77+
}
78+
79+
using namespace mbgl::style::conversion;
80+
ParseResult In::parse(const Convertible& value, ParsingContext& ctx) {
81+
assert(isArray(value));
82+
83+
std::size_t length = arrayLength(value);
84+
if (length != 3) {
85+
ctx.error("Expected 2 arguments, but found " + util::toString(length - 1) + " instead.");
86+
return ParseResult();
87+
}
88+
89+
ParseResult needle = ctx.parse(arrayMember(value, 1), 1, {type::Value});
90+
if (!needle) return ParseResult();
91+
92+
ParseResult haystack = ctx.parse(arrayMember(value, 2), 2, {type::Value});
93+
if (!haystack) return ParseResult();
94+
95+
type::Type needleType = (*needle)->getType();
96+
type::Type haystackType = (*haystack)->getType();
97+
98+
if (!isComparableType(needleType)) {
99+
ctx.error("Expected first argument to be of type boolean, string or number, but found " + toString(needleType) +
100+
" instead.");
101+
return ParseResult();
102+
}
103+
104+
if (!isSearchableType(haystackType)) {
105+
ctx.error("Expected second argument to be of type array or string, but found " + toString(haystackType) +
106+
" instead.");
107+
return ParseResult();
108+
}
109+
return ParseResult(std::make_unique<In>(std::move(*needle), std::move(*haystack)));
110+
}
111+
112+
bool In::operator==(const Expression& e) const {
113+
if (e.getKind() == Kind::In) {
114+
auto rhs = static_cast<const In*>(&e);
115+
return *needle == *(rhs->needle) && *haystack == *(rhs->haystack);
116+
}
117+
return false;
118+
}
119+
120+
std::vector<optional<Value>> In::possibleOutputs() const {
121+
return {{true}, {false}};
122+
}
123+
124+
std::string In::getOperator() const {
125+
return "in";
126+
}
127+
128+
} // namespace expression
129+
} // namespace style
130+
} // namespace mbgl

src/mbgl/style/expression/parsing_context.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <mbgl/style/expression/expression.hpp>
1616
#include <mbgl/style/expression/format_expression.hpp>
1717
#include <mbgl/style/expression/image_expression.hpp>
18+
#include <mbgl/style/expression/in.hpp>
1819
#include <mbgl/style/expression/interpolate.hpp>
1920
#include <mbgl/style/expression/length.hpp>
2021
#include <mbgl/style/expression/let.hpp>
@@ -114,6 +115,7 @@ MAPBOX_ETERNAL_CONSTEXPR const auto expressionRegistry =
114115
{"any", Any::parse},
115116
{"array", Assertion::parse},
116117
{"at", At::parse},
118+
{"in", In::parse},
117119
{"boolean", Assertion::parse},
118120
{"case", Case::parse},
119121
{"coalesce", Coalesce::parse},
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
"number",
3+
[
4+
"in",
5+
[
6+
"number",
7+
[
8+
"get",
9+
"i"
10+
]
11+
],
12+
[
13+
"array",
14+
[
15+
"get",
16+
"arr"
17+
]
18+
]
19+
]
20+
]

0 commit comments

Comments
 (0)