[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:
Submodule mapbox-gl-js updated: 5b3af8e151...a5a8dfe5b9
@@ -219,7 +219,7 @@
|
||||
NSArray *expected = @[@"match", @[@"id"], @[@6, @5, @4, @3], @YES, @NO];
|
||||
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"$featureIdentifier IN { 6, 5, 4, 3}"];
|
||||
XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected);
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH(CAST($featureIdentifier, 'NSNumber'), { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH($featureIdentifier, { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
auto forwardFilter = [NSPredicate predicateWithMGLJSONObject:expected].mgl_filter;
|
||||
NSPredicate *forwardPredicateAfter = [NSPredicate mgl_predicateWithFilter:forwardFilter];
|
||||
XCTAssertEqualObjects(predicateAfter, forwardPredicateAfter);
|
||||
@@ -228,7 +228,7 @@
|
||||
NSArray *expected = @[@"!", @[@"match", @[@"get", @"x"], @[@6, @5, @4, @3], @YES, @NO]];
|
||||
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT x IN { 6, 5, 4, 3}"];
|
||||
XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected);
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"NOT MGL_MATCH(CAST(x, 'NSNumber'), { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"NOT MGL_MATCH(x, { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
auto forwardFilter = [NSPredicate predicateWithMGLJSONObject:expected].mgl_filter;
|
||||
NSPredicate *forwardPredicateAfter = [NSPredicate mgl_predicateWithFilter:forwardFilter];
|
||||
XCTAssertEqualObjects(predicateAfter, forwardPredicateAfter);
|
||||
@@ -237,7 +237,7 @@
|
||||
NSArray *expected = @[@"match", @[@"get", @"a"], @[@"b", @"c"], @YES, @NO];
|
||||
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"a IN { 'b', 'c' }"];
|
||||
XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected);
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH(CAST(a, 'NSString'), { 'b', 'c' }, YES, NO) == YES"];
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH(a, { 'b', 'c' }, YES, NO) == YES"];
|
||||
auto forwardFilter = [NSPredicate predicateWithMGLJSONObject:expected].mgl_filter;
|
||||
NSPredicate *forwardPredicateAfter = [NSPredicate mgl_predicateWithFilter:forwardFilter];
|
||||
XCTAssertEqualObjects(predicateAfter, forwardPredicateAfter);
|
||||
@@ -255,7 +255,7 @@
|
||||
NSArray *expected = @[@"match", @[@"get", @"x"], @[@6, @5, @4, @3], @YES, @NO];
|
||||
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"{ 6, 5, 4, 3 } CONTAINS x"];
|
||||
XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected);
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH(CAST(x, 'NSNumber'), { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH(x, { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
auto forwardFilter = [NSPredicate predicateWithMGLJSONObject:expected].mgl_filter;
|
||||
NSPredicate *forwardPredicateAfter = [NSPredicate mgl_predicateWithFilter:forwardFilter];
|
||||
XCTAssertEqualObjects(predicateAfter, forwardPredicateAfter);
|
||||
@@ -273,7 +273,7 @@
|
||||
NSArray *expected = @[@"match", @[@"id"], @[@6, @5, @4, @3], @YES, @NO];
|
||||
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"{ 6, 5, 4, 3} CONTAINS $featureIdentifier"];
|
||||
XCTAssertEqualObjects(predicate.mgl_jsonExpressionObject, expected);
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH(CAST($featureIdentifier, 'NSNumber'), { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
NSPredicate *predicateAfter = [NSPredicate predicateWithFormat:@"MGL_MATCH($featureIdentifier, { 3, 4, 5, 6 }, YES, NO) == YES"];
|
||||
auto forwardFilter = [NSPredicate predicateWithMGLJSONObject:expected].mgl_filter;
|
||||
NSPredicate *forwardPredicateAfter = [NSPredicate mgl_predicateWithFilter:forwardFilter];
|
||||
XCTAssertEqualObjects(predicateAfter, forwardPredicateAfter);
|
||||
|
||||
@@ -42,6 +42,7 @@
|
||||
"render-tests/fill-extrusion-pattern/opacity": "https://github.com/mapbox/mapbox-gl-js/issues/3327",
|
||||
"render-tests/geojson/inline-linestring-fill": "current behavior is arbitrary",
|
||||
"render-tests/geojson/inline-polygon-symbol": "behavior needs reconciliation with gl-js",
|
||||
"render-tests/icon-rotate/with-offset": "https://github.com/mapbox/mapbox-gl-native/issues/11872",
|
||||
"render-tests/line-gradient/gradient": "https://github.com/mapbox/mapbox-gl-native/issues/11718",
|
||||
"render-tests/line-gradient/translucent": "https://github.com/mapbox/mapbox-gl-native/issues/11718",
|
||||
"render-tests/mixed-zoom/z10-z11": "https://github.com/mapbox/mapbox-gl-native/issues/10397",
|
||||
@@ -57,6 +58,7 @@
|
||||
"render-tests/regressions/mapbox-gl-js#5370": "skip - https://github.com/mapbox/mapbox-gl-native/pull/9439",
|
||||
"render-tests/regressions/mapbox-gl-js#5740": "https://github.com/mapbox/mapbox-gl-native/issues/10619",
|
||||
"render-tests/regressions/mapbox-gl-js#5982": "https://github.com/mapbox/mapbox-gl-native/issues/10619",
|
||||
"render-tests/regressions/mapbox-gl-js#6655": "skip - port https://github.com/mapbox/mapbox-gl-js/pull/6263 - needs issue",
|
||||
"render-tests/regressions/mapbox-gl-native#7357": "https://github.com/mapbox/mapbox-gl-native/issues/7357",
|
||||
"render-tests/runtime-styling/image-add-sdf": "https://github.com/mapbox/mapbox-gl-native/issues/9847",
|
||||
"render-tests/runtime-styling/paint-property-fill-flat-to-extrude": "https://github.com/mapbox/mapbox-gl-native/issues/6745",
|
||||
@@ -67,6 +69,7 @@
|
||||
"render-tests/text-pitch-alignment/map-text-rotation-alignment-map": "https://github.com/mapbox/mapbox-gl-native/issues/9732",
|
||||
"render-tests/text-pitch-alignment/viewport-text-rotation-alignment-map": "https://github.com/mapbox/mapbox-gl-native/issues/9732",
|
||||
"render-tests/text-pitch-scaling/line-half": "https://github.com/mapbox/mapbox-gl-native/issues/9732",
|
||||
"render-tests/text-rotate/with-offset": "https://github.com/mapbox/mapbox-gl-native/issues/11872",
|
||||
"render-tests/video/default": "skip - https://github.com/mapbox/mapbox-gl-native/issues/601",
|
||||
"render-tests/background-color/colorSpace-hcl": "needs issue",
|
||||
"render-tests/combinations/background-opaque--heatmap-translucent": "https://github.com/mapbox/mapbox-gl-native/issues/10146",
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user