From b2fabe5eefc81cc38866a4856d6db37f4471d6ae Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 17 May 2018 09:55:15 -0700 Subject: [core] Align match behavior with case/== Makes `["match", ["get", k], label, match, otherwise]` equivalent to `["case", ["==", ["get", k], label], match, otherwise]`. This changes the behavior of match expressions where the runtime type of the input does not match the type of the labels: previously such expressions produced a runtime type error and then fell back to the property default value; now they produce the fallback value from the match expression. --- src/mbgl/style/expression/match.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'src/mbgl/style/expression') diff --git a/src/mbgl/style/expression/match.cpp b/src/mbgl/style/expression/match.cpp index 3d41f0bdd3..59123c9812 100644 --- a/src/mbgl/style/expression/match.cpp +++ b/src/mbgl/style/expression/match.cpp @@ -83,6 +83,10 @@ template<> EvaluationResult Match::evaluate(const EvaluationContext return inputValue.error(); } + if (!inputValue->is()) { + return otherwise->evaluate(params); + } + auto it = branches.find(inputValue->get()); if (it != branches.end()) { return (*it).second->evaluate(params); @@ -96,7 +100,11 @@ template<> EvaluationResult Match::evaluate(const EvaluationContext& pa if (!inputValue) { return inputValue.error(); } - + + if (!inputValue->is()) { + return otherwise->evaluate(params); + } + const auto numeric = inputValue->get(); int64_t rounded = std::floor(numeric); if (numeric == rounded) { @@ -280,7 +288,7 @@ ParseResult parseMatch(const Convertible& value, ParsingContext& ctx) { branches.push_back(std::make_pair(std::move(labels), std::move(*output))); } - auto input = ctx.parse(arrayMember(value, 1), 1, inputType); + auto input = ctx.parse(arrayMember(value, 1), 1, {type::Value}); if (!input) { return ParseResult(); } @@ -292,6 +300,12 @@ ParseResult parseMatch(const Convertible& value, ParsingContext& ctx) { assert(inputType && outputType); + optional err; + if ((*input)->getType() != type::Value && (err = type::checkSubtype(*inputType, (*input)->getType()))) { + ctx.error(*err, 1); + return ParseResult(); + } + return inputType->match( [&](const type::NumberType&) { return create(*outputType, std::move(*input), std::move(branches), std::move(*otherwise), ctx); -- cgit v1.2.1