From 5990efbb12ddf7718a668012ba9d0ee549dd8a67 Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Wed, 7 Jun 2023 15:06:57 +0200 Subject: [PATCH] Fix issues in index-of expression (#1121) Co-authored-by: Ovidiu Voda Co-authored-by: Bart Louwers --- .../expression-tests/index-of/with-from-index/test.json | 6 ++++++ src/mbgl/style/expression/index_of.cpp | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/metrics/integration/expression-tests/index-of/with-from-index/test.json b/metrics/integration/expression-tests/index-of/with-from-index/test.json index 52a029e7b..6351cb238 100644 --- a/metrics/integration/expression-tests/index-of/with-from-index/test.json +++ b/metrics/integration/expression-tests/index-of/with-from-index/test.json @@ -11,6 +11,9 @@ [{}, {"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"}}] ], @@ -35,6 +38,9 @@ 4, -1, 1, + 1, + 0, + 2, -1, { "error": "Expected third argument to be of type number, but found string instead." diff --git a/src/mbgl/style/expression/index_of.cpp b/src/mbgl/style/expression/index_of.cpp index dfff3fff9..ecb7e514d 100644 --- a/src/mbgl/style/expression/index_of.cpp +++ b/src/mbgl/style/expression/index_of.cpp @@ -41,7 +41,7 @@ EvaluationResult IndexOf::evaluate(const EvaluationContext ¶ms) const { return EvaluationError{"Expected third argument to be of type number, but found " + toString(typeOf(*evaluatedFromIndex)) + " instead."}; } - fromIndexValue = static_cast(evaluatedFromIndex->get()); + fromIndexValue = static_cast(std::max(evaluatedFromIndex->get(), 0.0)); } return evaluatedInput->match(