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

Commit

Permalink
Fix review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin committed Feb 11, 2020
1 parent 5b019c2 commit 9cb1f21
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,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
Expand Down
7 changes: 2 additions & 5 deletions include/mbgl/style/expression/in.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_)) {}
Expand All @@ -26,16 +26,13 @@ class In : public Expression {
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
Expand Down
58 changes: 30 additions & 28 deletions src/mbgl/style/expression/in.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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";
Expand All @@ -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);
}
Expand Down Expand Up @@ -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

0 comments on commit 9cb1f21

Please sign in to comment.