[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.
This commit is contained in:
John Firebaugh
2018-05-17 09:55:15 -07:00
parent be6e40eb68
commit b2fabe5eef
4 changed files with 25 additions and 8 deletions

View File

@@ -83,6 +83,10 @@ template<> EvaluationResult Match<std::string>::evaluate(const EvaluationContext
return inputValue.error();
}
if (!inputValue->is<std::string>()) {
return otherwise->evaluate(params);
}
auto it = branches.find(inputValue->get<std::string>());
if (it != branches.end()) {
return (*it).second->evaluate(params);
@@ -96,7 +100,11 @@ template<> EvaluationResult Match<int64_t>::evaluate(const EvaluationContext& pa
if (!inputValue) {
return inputValue.error();
}
if (!inputValue->is<double>()) {
return otherwise->evaluate(params);
}
const auto numeric = inputValue->get<double>();
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<std::string> 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<int64_t>(*outputType, std::move(*input), std::move(branches), std::move(*otherwise), ctx);