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

Enable implicit type assertions for array types #5738

Merged
merged 1 commit into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions src/style-spec/expression/parsing_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const Scope = require('./scope');
const {checkSubtype} = require('./types');
const ParsingError = require('./parsing_error');
const Literal = require('./definitions/literal');
const Assertion = require('./definitions/assertion');
const ArrayAssertion = require('./definitions/array');
const Coercion = require('./definitions/coercion');

import type {Expression} from './expression';
import type {Type} from './types';
Expand Down Expand Up @@ -77,10 +80,10 @@ class ParsingContext {
expected.kind === 'boolean';

if (canAssert && actual.kind === 'value') {
const Assertion = require('./definitions/assertion');
parsed = new Assertion(expected, [parsed]);
} else if (expected.kind === 'array' && actual.kind === 'value') {
parsed = new ArrayAssertion(expected, parsed);
} else if (expected.kind === 'color' && (actual.kind === 'value' || actual.kind === 'string')) {
const Coercion = require('./definitions/coercion');
parsed = new Coercion(expected, [parsed]);
}

Expand Down
21 changes: 21 additions & 0 deletions test/integration/expression-tests/array/implicit-1/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"expression": ["at", 0, ["get", "array"]],
"inputs": [
[{}, {"properties": {"array": [0, 1, 2]}}],
[{}, {"properties": {"array": "not"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "value"
},
"outputs": [
0,
{
"error": "Expected value to be of type array, but found string instead."
}
]
}
}
26 changes: 26 additions & 0 deletions test/integration/expression-tests/array/implicit-2/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"propertySpec": {"type": "array", "value": "string"},
"expression": ["get", "array"],
"inputs": [
[{}, {"properties": {"array": ["a", "b"]}}],
[{}, {"properties": {"array": [1, 2]}}],
[{}, {"properties": {"array": "not"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "array<string>"
},
"outputs": [
["a", "b"],
{
"error": "Expected value to be of type array<string>, but found array<number, 2> instead."
},
{
"error": "Expected value to be of type array<string>, but found string instead."
}
]
}
}
26 changes: 26 additions & 0 deletions test/integration/expression-tests/array/implicit-3/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"propertySpec": {"type": "array", "value": "number", "length": 2},
"expression": ["get", "array"],
"inputs": [
[{}, {"properties": {"array": [1, 2]}}],
[{}, {"properties": {"array": [1, 2, 3]}}],
[{}, {"properties": {"array": "not"}}]
],
"expected": {
"compiled": {
"result": "success",
"isFeatureConstant": false,
"isZoomConstant": true,
"type": "array<number, 2>"
},
"outputs": [
[1, 2],
{
"error": "Expected value to be of type array<number, 2>, but found array<number, 3> instead."
},
{
"error": "Expected value to be of type array<number, 2>, but found string instead."
}
]
}
}