Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align match behavior with case/== #6684

Merged
merged 2 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/style-spec/expression/definitions/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import assert from 'assert';

import { typeOf } from '../values';
import { ValueType, type Type } from '../types';

import type { Expression } from '../expression';
import type ParsingContext from '../parsing_context';
import type EvaluationContext from '../evaluation_context';
import type { Type } from '../types';

// Map input label values to output expression index
type Cases = {[number | string]: number};
Expand Down Expand Up @@ -84,19 +84,25 @@ class Match implements Expression {
outputs.push(result);
}

const input = context.parse(args[1], 1, inputType);
const input = context.parse(args[1], 1, ValueType);
if (!input) return null;

const otherwise = context.parse(args[args.length - 1], args.length - 1, outputType);
if (!otherwise) return null;

assert(inputType && outputType);

if (input.type.kind !== 'value' && context.concat(1).checkSubtype((inputType: any), input.type)) {
return null;
}

return new Match((inputType: any), (outputType: any), input, cases, outputs, otherwise);
}

evaluate(ctx: EvaluationContext) {
const input = (this.input.evaluate(ctx): any);
return (this.outputs[this.cases[input]] || this.otherwise).evaluate(ctx);
const output = (typeOf(input) === this.inputType && this.outputs[this.cases[input]]) || this.otherwise;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad we can't just use Map

return output.evaluate(ctx);
}

eachChild(fn: (Expression) => void) {
Expand Down Expand Up @@ -134,7 +140,7 @@ class Match implements Expression {
}
}

const coerceLabel = (label) => this.input.type.kind === 'number' ? Number(label) : label;
const coerceLabel = (label) => this.inputType.kind === 'number' ? Number(label) : label;

for (const [outputIndex, labels] of groupedByOutput) {
if (labels.length === 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/style-spec/reference/v8.json
Original file line number Diff line number Diff line change
Expand Up @@ -2321,7 +2321,7 @@
}
},
"match": {
"doc": "Selects the output whose label value matches the input value, or the fallback value if no match is found. The `input` can be any string or number expression (e.g. `[\"get\", \"building_type\"]`). Each label can either be a single literal value or an array of values.",
"doc": "Selects the output whose label value matches the input value, or the fallback value if no match is found. The input can be any expression (e.g. `[\"get\", \"building_type\"]`). Each label must either be a single literal value or an array of literal values (e.g. `\"a\"` or `[\"c\", \"b\"]`), and those values must be all strings or all numbers. (The values `\"1\"` and `1` cannot both be labels in the same match expression.) If the input type does not match the type of the labels, the result will be the fallback value.",
"group": "Decision",
"sdk-support": {
"basic functionality": {
Expand Down
10 changes: 5 additions & 5 deletions test/integration/expression-tests/match/basic/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[{}, {"properties": {"x": "a"}}],
[{}, {"properties": {"x": "b"}}],
[{}, {"properties": {"x": "c"}}],
[{}, {"properties": {"x": 0}}]
[{}, {"properties": {"x": 0}}],
[{}, {"properties": {}}]
],
"expected": {
"compiled": {
Expand All @@ -17,13 +18,12 @@
"Apple",
"Banana",
"Kumquat",
{
"error": "Expected value to be of type string, but found number instead."
}
"Kumquat",
"Kumquat"
],
"serialized": [
"match",
["string", ["get", "x"]],
["get", "x"],
"a",
"Apple",
"b",
Expand Down
14 changes: 5 additions & 9 deletions test/integration/expression-tests/match/label-number/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@
"match",
"otherwise",
"otherwise",
{
"error": "Expected value to be of type number, but found string instead."
},
{
"error": "Expected value to be of type number, but found boolean instead."
},
{"error": "Expected value to be of type number, but found null instead."},
{"error": "Expected value to be of type number, but found null instead."}
"otherwise",
"otherwise",
"otherwise",
"otherwise"
],
"serialized": ["match", ["number", ["get", "x"]], 0, "match", "otherwise"]
"serialized": ["match", ["get", "x"], 0, "match", "otherwise"]
}
}