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

Commit

Permalink
Implement in expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin committed Feb 6, 2020
1 parent 1688130 commit f15ba74
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/mbgl/style/expression/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ enum class Kind : int32_t {
FormatExpression,
FormatSectionOverride,
NumberFormat,
ImageExpression
ImageExpression,
In
};

class Expression {
Expand Down
43 changes: 43 additions & 0 deletions include/mbgl/style/expression/in.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <mbgl/style/conversion.hpp>
#include <mbgl/style/expression/expression.hpp>
#include <memory>

namespace mbgl {
namespace style {
namespace expression {

class In : 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_)) {}

static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);

EvaluationResult evaluate(const EvaluationContext& params) const override;
void eachChild(const std::function<void(const Expression&)>&) const override;

bool operator==(const Expression& e) const override {
if (e.getKind() == Kind::In) {
auto rhs = static_cast<const In*>(&e);
return *needle == *(rhs->needle) && *haystack == *(rhs->haystack);
}
return false;
}

std::vector<optional<Value>> possibleOutputs() const override { return {nullopt}; }

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
} // namespace style
} // namespace mbgl
6 changes: 0 additions & 6 deletions metrics/ignores/platform-all.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
{
"expression-tests/collator/accent-equals-de": "Locale-specific behavior changes based on platform.",
"expression-tests/in/assert-array": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
"expression-tests/in/assert-string": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
"expression-tests/in/basic-array": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
"expression-tests/in/basic-string": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
"expression-tests/in/invalid-haystack": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
"expression-tests/in/invalid-needle": "https://github.com/mapbox/mapbox-gl-native/issues/15893",
"expression-tests/interpolate-hcl/linear": "https://github.com/mapbox/mapbox-gl-native/issues/8720",
"expression-tests/interpolate-lab/linear": "https://github.com/mapbox/mapbox-gl-native/issues/8720",
"expression-tests/is-supported-script/default": "This tests RTL text plugin behavior specific to GL JS",
Expand Down
2 changes: 2 additions & 0 deletions next/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ add_library(
${MBGL_ROOT}/include/mbgl/style/conversion_impl.hpp
${MBGL_ROOT}/include/mbgl/style/expression/assertion.hpp
${MBGL_ROOT}/include/mbgl/style/expression/at.hpp
${MBGL_ROOT}/include/mbgl/style/expression/in.hpp
${MBGL_ROOT}/include/mbgl/style/expression/boolean_operator.hpp
${MBGL_ROOT}/include/mbgl/style/expression/case.hpp
${MBGL_ROOT}/include/mbgl/style/expression/check_subtype.hpp
Expand Down Expand Up @@ -630,6 +631,7 @@ add_library(
${MBGL_ROOT}/src/mbgl/style/custom_tile_loader.hpp
${MBGL_ROOT}/src/mbgl/style/expression/assertion.cpp
${MBGL_ROOT}/src/mbgl/style/expression/at.cpp
${MBGL_ROOT}/src/mbgl/style/expression/in.cpp
${MBGL_ROOT}/src/mbgl/style/expression/boolean_operator.cpp
${MBGL_ROOT}/src/mbgl/style/expression/case.cpp
${MBGL_ROOT}/src/mbgl/style/expression/check_subtype.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/core-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@
"src/mbgl/style/expression/get_covering_stops.cpp",
"src/mbgl/style/expression/image.cpp",
"src/mbgl/style/expression/image_expression.cpp",
"src/mbgl/style/expression/in.cpp",
"src/mbgl/style/expression/interpolate.cpp",
"src/mbgl/style/expression/is_constant.cpp",
"src/mbgl/style/expression/is_expression.cpp",
Expand Down Expand Up @@ -424,6 +425,7 @@
"mbgl/style/expression/get_covering_stops.hpp": "include/mbgl/style/expression/get_covering_stops.hpp",
"mbgl/style/expression/image.hpp": "include/mbgl/style/expression/image.hpp",
"mbgl/style/expression/image_expression.hpp": "include/mbgl/style/expression/image_expression.hpp",
"mbgl/style/expression/in.hpp": "include/mbgl/style/expression/in.hpp",
"mbgl/style/expression/interpolate.hpp": "include/mbgl/style/expression/interpolate.hpp",
"mbgl/style/expression/interpolator.hpp": "include/mbgl/style/expression/interpolator.hpp",
"mbgl/style/expression/is_constant.hpp": "include/mbgl/style/expression/is_constant.hpp",
Expand Down
116 changes: 116 additions & 0 deletions src/mbgl/style/expression/in.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/expression/in.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/util/string.hpp>
#include <string.h>

namespace mbgl {
namespace style {
namespace expression {

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();
}

type::Type evaluatedNeedleType = typeOf(*evaluatedNeedle);
if (!isComparableRuntimeValue(evaluatedNeedleType)) {
return EvaluationError{"Expected first argument to be of type boolean, string or number, but found " +
toString(evaluatedNeedleType) + " instead."};
}

type::Type evaluatedHeystackType = typeOf(*evaluatedHeystack);
if (!isSearchableRuntimeValue(evaluatedHeystackType)) {
return EvaluationError{"Expected second argument to be of type array or string, but found " +
toString(evaluatedHeystackType) + " instead."};
}

if (evaluatedNeedleType == type::Null || evaluatedHeystackType == type::Null) {
return EvaluationResult(false);
}

if (evaluatedHeystackType == type::String) {
const auto heystackString = evaluatedHeystack->get<std::string>();
std::string needleValue = "";
if (evaluatedNeedleType == type::Boolean) {
needleValue = evaluatedNeedle->get<bool>() ? "true" : " false";
} else if(evaluatedNeedleType == type::String) {
needleValue = evaluatedNeedle->get<std::string>();
} else if(evaluatedNeedleType == type::Number) {
needleValue = std::to_string(evaluatedNeedle->get<double>());
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);
} else {
const auto heystackArray = evaluatedHeystack->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();
} else if(evaluatedNeedleType == type::String) {
auto needleValue = evaluatedNeedle->get<std::string>();
result = find(heystackArray.begin(), heystackArray.end(), needleValue) != heystackArray.end();
} else if(evaluatedNeedleType == type::Number) {
auto needleValue = evaluatedNeedle->get<double>();
result = find(heystackArray.begin(), heystackArray.end(), needleValue) != heystackArray.end();
}
return EvaluationResult(result);
}
}

void In::eachChild(const std::function<void(const Expression&)>& visit) const {
visit(*needle);
visit(*haystack);
}

using namespace mbgl::style::conversion;
ParseResult In::parse(const Convertible& value, ParsingContext& ctx) {
assert(isArray(value));

std::size_t length = arrayLength(value);
if (length != 3) {
ctx.error("Expected 2 arguments, but found " + util::toString(length - 1) + " instead.");
return ParseResult();
}

ParseResult needle = ctx.parse(arrayMember(value, 1), 1, {type::Value});
ParseResult haystack = ctx.parse(arrayMember(value, 2), 2, {type::Value});

if (!needle || !haystack) return ParseResult();

type::Type needleType = (*needle)->getType();;
type::Type haystackType = (*haystack)->getType();;

if (!isComparableType(needleType)) {
ctx.error("Expected first argument to be of type boolean, string or number, but found " +
toString(needleType) + " instead.");
return ParseResult();
}

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
2 changes: 2 additions & 0 deletions src/mbgl/style/expression/parsing_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/expression/format_expression.hpp>
#include <mbgl/style/expression/image_expression.hpp>
#include <mbgl/style/expression/in.hpp>
#include <mbgl/style/expression/interpolate.hpp>
#include <mbgl/style/expression/length.hpp>
#include <mbgl/style/expression/let.hpp>
Expand Down Expand Up @@ -113,6 +114,7 @@ MAPBOX_ETERNAL_CONSTEXPR const auto expressionRegistry =
{"any", Any::parse},
{"array", Assertion::parse},
{"at", At::parse},
{"in", In::parse},
{"boolean", Assertion::parse},
{"case", Case::parse},
{"coalesce", Coalesce::parse},
Expand Down
3 changes: 1 addition & 2 deletions test/style/expression/expression.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ TEST(Expression, IsExpression) {

// TODO: "interpolate-hcl": https://github.com/mapbox/mapbox-gl-native/issues/8720
// TODO: "interpolate-lab": https://github.com/mapbox/mapbox-gl-native/issues/8720
// TODO: "in": https://github.com/mapbox/mapbox-gl-native/issues/15893
if (name == "interpolate-hcl" || name == "interpolate-lab" || name == "in") {
if (name == "interpolate-hcl" || name == "interpolate-lab") {
if (expression::isExpression(conversion::Convertible(expression))) {
ASSERT_TRUE(false) << "Expression name" << name << "is implemented - please update Expression.IsExpression test.";
}
Expand Down

0 comments on commit f15ba74

Please sign in to comment.