-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opengl-2] cherry pick slice and index-of expression (#2023)
Co-authored-by: Siarhei Fedartsou <siarhei.fedartsou@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Bart Louwers <bart@emeel.net> Co-authored-by: Ovidiu Voda <ovi.voda@gmail.com>
- Loading branch information
1 parent
0849edc
commit 7744591
Showing
22 changed files
with
875 additions
and
37 deletions.
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
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,41 @@ | ||
#pragma once | ||
|
||
#include <mbgl/style/expression/expression.hpp> | ||
#include <mbgl/style/conversion.hpp> | ||
#include <memory> | ||
|
||
namespace mbgl { | ||
namespace style { | ||
namespace expression { | ||
|
||
class IndexOf : public Expression { | ||
public: | ||
IndexOf(std::unique_ptr<Expression> keyword_, | ||
std::unique_ptr<Expression> input_, | ||
std::unique_ptr<Expression> fromIndex_); | ||
|
||
static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx); | ||
|
||
EvaluationResult evaluate(const EvaluationContext& params) const override; | ||
void eachChild(const std::function<void(const Expression&)>&) const override; | ||
|
||
bool operator==(const Expression& e) const override; | ||
|
||
std::vector<std::optional<Value>> possibleOutputs() const override; | ||
std::string getOperator() const override; | ||
|
||
private: | ||
EvaluationResult evaluateForArrayInput(const std::vector<Value>& array, | ||
const Value& keyword, | ||
size_t fromIndex) const; | ||
EvaluationResult evaluateForStringInput(const std::string& string, const Value& keyword, size_t fromIndex) const; | ||
|
||
private: | ||
std::unique_ptr<Expression> keyword; | ||
std::unique_ptr<Expression> input; | ||
std::unique_ptr<Expression> fromIndex; | ||
}; | ||
|
||
} // namespace expression | ||
} // namespace style | ||
} // namespace mbgl |
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,39 @@ | ||
#pragma once | ||
|
||
#include <mbgl/style/expression/expression.hpp> | ||
#include <mbgl/style/conversion.hpp> | ||
#include <memory> | ||
|
||
namespace mbgl { | ||
namespace style { | ||
namespace expression { | ||
|
||
class Slice : public Expression { | ||
public: | ||
Slice(std::unique_ptr<Expression> input_, | ||
std::unique_ptr<Expression> fromIndex_, | ||
std::unique_ptr<Expression> toIndex_); | ||
|
||
static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx); | ||
|
||
EvaluationResult evaluate(const EvaluationContext& params) const override; | ||
void eachChild(const std::function<void(const Expression&)>&) const override; | ||
|
||
bool operator==(const Expression& e) const override; | ||
|
||
std::vector<std::optional<Value>> possibleOutputs() const override; | ||
std::string getOperator() const override; | ||
|
||
private: | ||
EvaluationResult evaluateForStringInput(const std::string& input, int fromIndexValue, int toIndexValue) const; | ||
EvaluationResult evaluateForArrayInput(const std::vector<Value>& input, int fromIndexValue, int toIndexValue) const; | ||
|
||
private: | ||
std::unique_ptr<Expression> input; | ||
std::unique_ptr<Expression> fromIndex; | ||
std::unique_ptr<Expression> toIndex; | ||
}; | ||
|
||
} // namespace expression | ||
} // namespace style | ||
} // namespace mbgl |
26 changes: 26 additions & 0 deletions
26
metrics/integration/expression-tests/index-of/assert-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,26 @@ | ||
{ | ||
"expression": ["index-of", ["get", "i"], ["array", ["get", "arr"]]], | ||
"inputs": [ | ||
[{}, {"properties": {"i": null, "arr": [9, 8, 7]}}], | ||
[{}, {"properties": {"i": null, "arr": [9, 8, 7, null]}}], | ||
[{}, {"properties": {"i": 1, "arr": [9, 8, 7]}}], | ||
[{}, {"properties": {"i": 9, "arr": [9, 8, 7, 9]}}], | ||
[{}, {"properties": {"i": 1, "arr": null}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"serialized": ["index-of", ["get", "i"], ["array", ["get", "arr"]]], | ||
"outputs": [ | ||
-1, | ||
3, | ||
-1, | ||
0, | ||
{"error": "Expected value to be of type array, but found null instead."} | ||
] | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
metrics/integration/expression-tests/index-of/assert-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,24 @@ | ||
{ | ||
"expression": ["index-of", ["get", "substr"], ["string", ["get", "str"]]], | ||
"inputs": [ | ||
[{}, {"properties": {"substr": null, "str": "helloworld"}}], | ||
[{}, {"properties": {"substr": "foo", "str": "helloworld"}}], | ||
[{}, {"properties": {"substr": "low", "str": "helloworld"}}], | ||
[{}, {"properties": {"substr": "low", "str": null}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"serialized": ["index-of", ["get", "substr"], ["string", ["get", "str"]]], | ||
"outputs": [ | ||
-1, | ||
-1, | ||
3, | ||
{"error": "Expected value to be of type string, but found null instead."} | ||
] | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
metrics/integration/expression-tests/index-of/basic-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,46 @@ | ||
{ | ||
"expression": ["index-of", ["get", "i"], ["get", "arr"]], | ||
"inputs": [ | ||
[{}, {"properties": {"i": null, "arr": [9, 8, 7]}}], | ||
[{}, {"properties": {"i": 1, "arr": [9, 8, 7]}}], | ||
[{}, {"properties": {"i": 9, "arr": [9, 8, 7]}}], | ||
[ | ||
{}, | ||
{ | ||
"properties": { | ||
"i": "foo", | ||
"arr": ["baz", "bar", "hello", "foo", "world"] | ||
} | ||
} | ||
], | ||
[ | ||
{}, | ||
{ | ||
"properties": { | ||
"i": true, | ||
"arr": ["foo", 123, null, 456, false, {}, true] | ||
} | ||
} | ||
], | ||
[{}, {"properties": {"i": 1, "arr": null}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"serialized": ["index-of", ["get", "i"], ["get", "arr"]], | ||
"outputs": [ | ||
-1, | ||
-1, | ||
0, | ||
3, | ||
6, | ||
{ | ||
"error": "Expected second argument to be of type array or string, but found null instead." | ||
} | ||
] | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
metrics/integration/expression-tests/index-of/basic-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,32 @@ | ||
{ | ||
"expression": ["index-of", ["get", "substr"], ["get", "str"]], | ||
"inputs": [ | ||
[{}, {"properties": {"substr": null, "str": "helloworld"}}], | ||
[{}, {"properties": {"substr": "foo", "str": "helloworld"}}], | ||
[{}, {"properties": {"substr": "low", "str": "helloworld"}}], | ||
[{}, {"properties": {"substr": true, "str": "falsetrue"}}], | ||
[{}, {"properties": {"substr": false, "str": "falsetrue"}}], | ||
[{}, {"properties": {"substr": 123, "str": "hello123world"}}], | ||
[{}, {"properties": {"substr": "low", "str": null}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"serialized": ["index-of", ["get", "substr"], ["get", "str"]], | ||
"outputs": [ | ||
-1, | ||
-1, | ||
3, | ||
5, | ||
0, | ||
5, | ||
{ | ||
"error": "Expected second argument to be of type array or string, but found null instead." | ||
} | ||
] | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
metrics/integration/expression-tests/index-of/invalid-haystack/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 @@ | ||
{ | ||
"expression": ["index-of", ["get", "needle"], ["get", "haystack"]], | ||
"inputs": [ | ||
[{}, {"properties": {"needle": 1, "haystack": 123}}], | ||
[{}, {"properties": {"needle": "foo", "haystack": {}}}], | ||
[{}, {"properties": {"needle": "foo", "haystack": null}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"serialized": ["index-of", ["get", "needle"], ["get", "haystack"]], | ||
"outputs": [ | ||
{ | ||
"error": "Expected second argument to be of type array or string, but found number instead." | ||
}, | ||
{ | ||
"error": "Expected second argument to be of type array or string, but found object instead." | ||
}, | ||
{ | ||
"error": "Expected second argument to be of type array or string, but found null instead." | ||
} | ||
] | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
metrics/integration/expression-tests/index-of/invalid-needle/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,25 @@ | ||
{ | ||
"expression": ["index-of", ["get", "needle"], ["get", "haystack"]], | ||
"inputs": [ | ||
[{}, {"properties": {"needle": {}, "haystack": [9, 8, 7]}}], | ||
[{}, {"properties": {"needle": {}, "haystack": "helloworld"}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"serialized": ["index-of", ["get", "needle"], ["get", "haystack"]], | ||
"outputs": [ | ||
{ | ||
"error": "Expected first argument to be of type boolean, string, number or null, but found object instead." | ||
}, | ||
{ | ||
"error": "Expected first argument to be of type boolean, string, number or null, but found object instead." | ||
} | ||
] | ||
} | ||
} | ||
|
51 changes: 51 additions & 0 deletions
51
metrics/integration/expression-tests/index-of/with-from-index/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,51 @@ | ||
{ | ||
"expression": ["index-of", ["get", "needle"], ["get", "hay"], ["get", "i"]], | ||
"inputs": [ | ||
[{}, {"properties": {"needle": null, "hay": "helloworld", "i": 0}}], | ||
[{}, {"properties": {"needle": "foo", "hay": "helloworld", "i": 0}}], | ||
[{}, {"properties": {"needle": "low", "hay": "helloworldlow", "i": 4}}], | ||
[{}, {"properties": {"needle": true, "hay": "falsetruetrue", "i": 6}}], | ||
[{}, {"properties": {"needle": false, "hay": "falsetrue", "i": 0}}], | ||
[{}, {"properties": {"needle": 123, "hay": "hello123world", "i": 6}}], | ||
[{}, {"properties": {"needle": "low", "hay": null, "i": 0}}], | ||
[{}, {"properties": {"needle": 7, "hay": [9, 8, 7, 8, 7, 7], "i": 3}}], | ||
[{}, {"properties": {"needle": 9, "hay": [9, 8, 7, 8, 7, 7], "i": 1}}], | ||
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": 1}}], | ||
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": -1}}], | ||
[{}, {"properties": {"needle": "foo", "hay": ["foo", "foo"], "i": -100}}], | ||
[{}, {"properties": {"needle": "foo", "hay": "__foo__foo", "i": -100}}], | ||
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": 10000}}], | ||
[{}, {"properties": {"needle": 8, "hay": [9, 8, 7, 8, 7, 7], "i": "wrong"}}] | ||
], | ||
"expected": { | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "number" | ||
}, | ||
"serialized": ["index-of", ["get", "needle"], ["get", "hay"], ["get", "i"]], | ||
"outputs": [ | ||
-1, | ||
-1, | ||
10, | ||
9, | ||
0, | ||
-1, | ||
{ | ||
"error": "Expected second argument to be of type array or string, but found null instead." | ||
}, | ||
4, | ||
-1, | ||
1, | ||
1, | ||
0, | ||
2, | ||
-1, | ||
{ | ||
"error": "Expected third argument to be of type number, but found string instead." | ||
} | ||
] | ||
} | ||
} | ||
|
19 changes: 19 additions & 0 deletions
19
metrics/integration/expression-tests/slice/array-one-index/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,19 @@ | ||
{ | ||
"expression": ["slice", ["array", ["get", "val"]], ["get", "index"]], | ||
"inputs": [ | ||
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": 2}}], | ||
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": 0}}], | ||
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": 99}}], | ||
[{}, {"properties": {"val": [1, 2, 3, 4, 5], "index": -2}}] | ||
], | ||
"expected": { | ||
"serialized": ["slice", ["array", ["get", "val"]], ["get", "index"]], | ||
"compiled": { | ||
"result": "success", | ||
"isFeatureConstant": false, | ||
"isZoomConstant": true, | ||
"type": "array" | ||
}, | ||
"outputs": [[3, 4, 5], [1, 2, 3, 4, 5], [], [4, 5]] | ||
} | ||
} |
Oops, something went wrong.