summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkevin <kevin.li@mapbox.com>2020-02-11 17:02:08 +0800
committerkevin <kevin.li@mapbox.com>2020-02-15 08:52:23 +0800
commit447b694e25997db7b5133b9376d94a4aa2f9ac5f (patch)
treee93d0c84584d09e32072855ac56b77ead2e64aec
parent2e2bba8dc33665df952e80beb381741894a5331d (diff)
downloadqtlocation-mapboxgl-447b694e25997db7b5133b9376d94a4aa2f9ac5f.tar.gz
Fix review comments.
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/mbgl/style/expression/in.hpp7
-rw-r--r--src/mbgl/style/expression/in.cpp58
3 files changed, 33 insertions, 33 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 485ced04aa..4428b38b53 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -586,6 +586,7 @@ add_library(
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/get_covering_stops.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/image.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/image_expression.cpp
+ ${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/in.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/interpolate.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/is_constant.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/expression/is_expression.cpp
diff --git a/include/mbgl/style/expression/in.hpp b/include/mbgl/style/expression/in.hpp
index ab8e2e9ff2..6bd57e2833 100644
--- a/include/mbgl/style/expression/in.hpp
+++ b/include/mbgl/style/expression/in.hpp
@@ -8,7 +8,7 @@ namespace mbgl {
namespace style {
namespace expression {
-class In : public Expression {
+class In final : public Expression {
public:
In(std::unique_ptr<Expression> needle_, std::unique_ptr<Expression> haystack_)
: Expression(Kind::In, type::Boolean), needle(std::move(needle_)), haystack(std::move(haystack_)) {}
@@ -26,16 +26,13 @@ public:
return false;
}
- std::vector<optional<Value>> possibleOutputs() const override { return {nullopt}; }
+ std::vector<optional<Value>> possibleOutputs() const override { return {{true}, {false}}; }
std::string getOperator() const override { return "in"; }
private:
std::unique_ptr<Expression> needle;
std::unique_ptr<Expression> haystack;
- static bool isComparableType(type::Type type);
- static bool isComparableRuntimeValue(type::Type type);
- static bool isSearchableRuntimeValue(type::Type type);
};
} // namespace expression
diff --git a/src/mbgl/style/expression/in.cpp b/src/mbgl/style/expression/in.cpp
index 05336c7d92..28658b4a31 100644
--- a/src/mbgl/style/expression/in.cpp
+++ b/src/mbgl/style/expression/in.cpp
@@ -8,15 +8,30 @@ namespace mbgl {
namespace style {
namespace expression {
+namespace {
+bool isComparableType(type::Type type) {
+ return type == type::Boolean || type == type::String || type == type::Number || type == type::Null ||
+ type == type::Value;
+}
+
+bool isComparableRuntimeValue(type::Type type) {
+ return type == type::Boolean || type == type::String || type == type::Number || type == type::Null;
+}
+
+bool isSearchableRuntimeValue(type::Type type) {
+ return type == type::String || type.is<type::Array>() || type == type::Null;
+}
+}
+
EvaluationResult In::evaluate(const EvaluationContext& params) const {
const EvaluationResult evaluatedNeedle = needle->evaluate(params);
- const EvaluationResult evaluatedHeystack = haystack->evaluate(params);
-
if (!evaluatedNeedle) {
return evaluatedNeedle.error();
}
- if (!evaluatedHeystack) {
- return evaluatedHeystack.error();
+
+ const EvaluationResult evaluatedHaystack = haystack->evaluate(params);
+ if (!evaluatedHaystack) {
+ return evaluatedHaystack.error();
}
type::Type evaluatedNeedleType = typeOf(*evaluatedNeedle);
@@ -25,18 +40,18 @@ EvaluationResult In::evaluate(const EvaluationContext& params) const {
toString(evaluatedNeedleType) + " instead."};
}
- type::Type evaluatedHeystackType = typeOf(*evaluatedHeystack);
- if (!isSearchableRuntimeValue(evaluatedHeystackType)) {
+ type::Type evaluatedHaystackType = typeOf(*evaluatedHaystack);
+ if (!isSearchableRuntimeValue(evaluatedHaystackType)) {
return EvaluationError{"Expected second argument to be of type array or string, but found " +
- toString(evaluatedHeystackType) + " instead."};
+ toString(evaluatedHaystackType) + " instead."};
}
- if (evaluatedNeedleType == type::Null || evaluatedHeystackType == type::Null) {
+ if (evaluatedNeedleType == type::Null || evaluatedHaystackType == type::Null) {
return EvaluationResult(false);
}
- if (evaluatedHeystackType == type::String) {
- const auto heystackString = evaluatedHeystack->get<std::string>();
+ if (evaluatedHaystackType == type::String) {
+ const auto haystackString = evaluatedHaystack->get<std::string>();
std::string needleValue = "";
if (evaluatedNeedleType == type::Boolean) {
needleValue = evaluatedNeedle->get<bool>() ? "true" : " false";
@@ -47,20 +62,20 @@ EvaluationResult In::evaluate(const EvaluationContext& params) const {
needleValue.erase(needleValue.find_last_not_of('0') + 1, std::string::npos);
needleValue.erase(needleValue.find_last_not_of('.') + 1, std::string::npos);
}
- return EvaluationResult(heystackString.find(needleValue) != std::string::npos);
+ return EvaluationResult(haystackString.find(needleValue) != std::string::npos);
} else {
- const auto heystackArray = evaluatedHeystack->get<std::vector<Value>>();
+ const auto haystackArray = evaluatedHaystack->get<std::vector<Value>>();
bool result = false;
if (evaluatedNeedleType == type::Boolean) {
auto needleValue = evaluatedNeedle->get<bool>();
- result = find(heystackArray.begin(), heystackArray.end(), needleValue) != heystackArray.end();
+ result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end();
} else if (evaluatedNeedleType == type::String) {
auto needleValue = evaluatedNeedle->get<std::string>();
- result = find(heystackArray.begin(), heystackArray.end(), needleValue) != heystackArray.end();
+ result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end();
} else if (evaluatedNeedleType == type::Number) {
auto needleValue = evaluatedNeedle->get<double>();
- result = find(heystackArray.begin(), heystackArray.end(), needleValue) != heystackArray.end();
+ result = find(haystackArray.begin(), haystackArray.end(), needleValue) != haystackArray.end();
}
return EvaluationResult(result);
}
@@ -98,19 +113,6 @@ ParseResult In::parse(const Convertible& value, ParsingContext& ctx) {
return ParseResult(std::make_unique<In>(std::move(*needle), std::move(*haystack)));
}
-bool In::isComparableType(type::Type type) {
- return type == type::Boolean || type == type::String || type == type::Number || type == type::Null ||
- type == type::Value;
-}
-
-bool In::isComparableRuntimeValue(type::Type type) {
- return type == type::Boolean || type == type::String || type == type::Number || type == type::Null;
-}
-
-bool In::isSearchableRuntimeValue(type::Type type) {
- return type == type::String || type.is<type::Array>() || type == type::Null;
-}
-
} // namespace expression
} // namespace style
} // namespace mbgl