-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anand Thakker
committed
Aug 28, 2017
1 parent
aa451de
commit 3ce92e1
Showing
11 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// @flow | ||
|
||
const { parseExpression } = require('../expression'); | ||
const { | ||
array, | ||
BooleanType, | ||
ValueType | ||
} = require('../types'); | ||
|
||
import type { Expression, ParsingContext } from '../expression'; | ||
import type { Type, ArrayType } from '../types'; | ||
|
||
class Contains implements Expression { | ||
key: string; | ||
type: Type; | ||
value: Expression; | ||
array: Expression; | ||
|
||
constructor(key: string, value: Expression, array: Expression) { | ||
this.key = key; | ||
this.type = BooleanType; | ||
this.value = value; | ||
this.array = array; | ||
} | ||
|
||
static parse(args: Array<mixed>, context: ParsingContext) { | ||
if (args.length !== 3) | ||
return context.error(`Expected 2 arguments, but found ${args.length - 1} instead.`); | ||
|
||
const arrayExpr = parseExpression(args[1], context.concat(1, array(ValueType))); | ||
if (!arrayExpr) return null; | ||
|
||
const t: ArrayType = (arrayExpr.type: any); | ||
const value = parseExpression(args[2], context.concat(2, t.itemType)); | ||
if (!value) return null; | ||
|
||
const itemType = value.type.kind; | ||
if (itemType === 'Object' || itemType === 'Array' || itemType === 'Color') { | ||
return context.error(`"contains" does not support values of type ${itemType}.`); | ||
} | ||
|
||
return new Contains(context.key, value, arrayExpr); | ||
} | ||
|
||
compile() { | ||
return `this.contains(${this.array.compile()}, ${this.value.compile()})`; | ||
} | ||
|
||
serialize() { | ||
return [ 'contains', this.array.serialize(), this.value.serialize() ]; | ||
} | ||
|
||
accept(visitor: Visitor<Expression>) { | ||
visitor.visit(this); | ||
this.array.accept(visitor); | ||
this.value.accept(visitor); | ||
} | ||
} | ||
|
||
module.exports = Contains; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
test/integration/expression-tests/contains/array/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"expectExpressionType": null, | ||
"expression": ["contains", ["array", ["get", "arr"]], ["literal", []]], | ||
"inputs": [], | ||
"expected": { | ||
"compiled": { | ||
"result": "error", | ||
"errors": [ | ||
{ | ||
"key": "", | ||
"error": "\"contains\" does not support values of type Array." | ||
} | ||
] | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/integration/expression-tests/contains/boolean/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"expectExpressionType": null, | ||
"expression": [ | ||
"contains", | ||
["literal", [false, false]], | ||
["boolean", ["get", "item"]] | ||
], | ||
"inputs": [ | ||
[{}, {"properties": {"item": true}}], | ||
[{}, {"properties": {"item": false}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "Boolean" | ||
}, | ||
"outputs": [false, true] | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/integration/expression-tests/contains/color/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"expectExpressionType": null, | ||
"expression": ["contains", ["array", ["get", "arr"]], ["parse-color", "red"]], | ||
"inputs": [], | ||
"expected": { | ||
"compiled": { | ||
"result": "error", | ||
"errors": [ | ||
{ | ||
"key": "", | ||
"error": "\"contains\" does not support values of type Color." | ||
} | ||
] | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
test/integration/expression-tests/contains/number/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"expectExpressionType": null, | ||
"expression": [ | ||
"contains", | ||
["literal", [1, 2, 3, 4]], | ||
["number", ["get", "item"]] | ||
], | ||
"inputs": [ | ||
[{}, {"properties": {"item": 3}}], | ||
[{}, {"properties": {"item": 5}}], | ||
[{}, {"properties": {"item": "3"}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "Boolean" | ||
}, | ||
"outputs": [ | ||
true, | ||
false, | ||
{ | ||
"error": "Expected value to be of type Number, but found String instead." | ||
} | ||
] | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/integration/expression-tests/contains/object/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"expectExpressionType": null, | ||
"expression": ["contains", ["array", ["get", "arr"]], ["literal", {}]], | ||
"inputs": [], | ||
"expected": { | ||
"compiled": { | ||
"result": "error", | ||
"errors": [ | ||
{ | ||
"key": "", | ||
"error": "\"contains\" does not support values of type Object." | ||
} | ||
] | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/integration/expression-tests/contains/string/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"expectExpressionType": null, | ||
"expression": [ | ||
"contains", | ||
["literal", ["a", "b", "c"]], | ||
["string", ["get", "item"]] | ||
], | ||
"inputs": [ | ||
[{}, {"properties": {"item": "a"}}], | ||
[{}, {"properties": {"item": "d"}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "Boolean" | ||
}, | ||
"outputs": [true, false] | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
test/integration/expression-tests/contains/value/test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"expectExpressionType": null, | ||
"expression": ["contains", ["array", ["get", "arr"]], ["get", "item"]], | ||
"inputs": [ | ||
[{}, {"properties": {"item": 3, "arr": [1, 2, 3, 4]}}], | ||
[{}, {"properties": {"item": 5, "arr": [1, 2, 3, 4]}}], | ||
[{}, {"properties": {"item": "3", "arr": [1, 2, 3, 4]}}], | ||
[{}, {"properties": {"item": "a", "arr": ["a", "b", "c"]}}], | ||
[{}, {"properties": {"item": "d", "arr": ["a", "b", "c"]}}], | ||
[{}, {"properties": {"item": true, "arr": [true, true]}}], | ||
[{}, {"properties": {"item": false, "arr": [true, true]}}], | ||
[ | ||
{}, | ||
{ | ||
"properties": { | ||
"item": ["nested", "array"], | ||
"arr": [["nested", "array"]] | ||
} | ||
} | ||
] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "Boolean" | ||
}, | ||
"outputs": [ | ||
true, | ||
false, | ||
false, | ||
true, | ||
false, | ||
true, | ||
false, | ||
{"error": "\"contains\" does not support values of type Array"} | ||
] | ||
} | ||
} |