feat: implement split and join expressions (#4463)
Co-authored-by: Bart Louwers <bart@emeel.net>
This commit is contained in:
@@ -155,6 +155,9 @@ public:
|
||||
EvaluationResult(const std::array<double, 4>& arr)
|
||||
: Result(toExpressionValue(arr)) {}
|
||||
|
||||
EvaluationResult(const std::vector<std::string>& values)
|
||||
: Result(toExpressionValue(values)) {}
|
||||
|
||||
// used only for the special (private) "error" expression
|
||||
EvaluationResult(const type::ErrorType&) noexcept { assert(false); }
|
||||
};
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"expression": ["join", ["literal", []], ","],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": true,
|
||||
"isZoomConstant": true,
|
||||
"type": "string"
|
||||
},
|
||||
"outputs": [""],
|
||||
"serialized": ""
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"expression": ["join", ["literal", ["", "latitude", "", "", "longitude", ""]], ","],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": true,
|
||||
"isZoomConstant": true,
|
||||
"type": "string"
|
||||
},
|
||||
"outputs": [",latitude,,,longitude,"],
|
||||
"serialized": ",latitude,,,longitude,"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"expression": ["join", ["literal", ["latitude", "longitude"]], ""],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": true,
|
||||
"isZoomConstant": true,
|
||||
"type": "string"
|
||||
},
|
||||
"outputs": ["latitudelongitude"],
|
||||
"serialized": "latitudelongitude"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"expression": ["join", "1+2+3", "+"],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "error",
|
||||
"errors": [
|
||||
{
|
||||
"key": "",
|
||||
"error": "Expected arguments of type (array<string>, string), but found (string, string) instead."
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
15
metrics/integration/expression-tests/join/numbers/test.json
Normal file
15
metrics/integration/expression-tests/join/numbers/test.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"expression": ["join", ["literal", [1, 2, 3]], "+"],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "error",
|
||||
"errors": [
|
||||
{
|
||||
"key": "",
|
||||
"error": "Expected arguments of type (array<string>, string), but found (array<number, 3>, string) instead."
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
23
metrics/integration/expression-tests/join/property/test.json
Normal file
23
metrics/integration/expression-tests/join/property/test.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"expression": ["join", ["get", "haystack"], "+"],
|
||||
"inputs": [
|
||||
[
|
||||
{},
|
||||
{
|
||||
"properties": {
|
||||
"haystack": ["1", "2", "3"]
|
||||
}
|
||||
}
|
||||
]
|
||||
],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": false,
|
||||
"isZoomConstant": true,
|
||||
"type": "string"
|
||||
},
|
||||
"outputs": ["1+2+3"],
|
||||
"serialized": ["join", ["array", "string", ["get", "haystack"]], "+"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"expression": ["split", "string", ""],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": true,
|
||||
"isZoomConstant": true,
|
||||
"type": "array<string>"
|
||||
},
|
||||
"outputs": [["s", "t", "r", "i", "n", "g"]],
|
||||
"serialized": ["literal", ["s", "t", "r", "i", "n", "g"]]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"expression": ["split", "1+2+3+4", "+"],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": true,
|
||||
"isZoomConstant": true,
|
||||
"type": "array<string>"
|
||||
},
|
||||
"outputs": [["1", "2", "3", "4"]],
|
||||
"serialized": ["literal", ["1", "2", "3", "4"]]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"expression": ["split", "needle", "e"],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": true,
|
||||
"isZoomConstant": true,
|
||||
"type": "array<string>"
|
||||
},
|
||||
"outputs": [["n", "", "dl", ""]],
|
||||
"serialized": ["literal", ["n", "", "dl", ""]]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"expression": ["split", "needle", "haystack"],
|
||||
"inputs": [[{}, {}]],
|
||||
"expected": {
|
||||
"compiled": {
|
||||
"result": "success",
|
||||
"isFeatureConstant": true,
|
||||
"isZoomConstant": true,
|
||||
"type": "array<string>"
|
||||
},
|
||||
"outputs": [["needle"]],
|
||||
"serialized": ["literal", ["needle"]]
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <mbgl/style/expression/compound_expression.hpp>
|
||||
#include <mbgl/style/expression/check_subtype.hpp>
|
||||
#include <mbgl/style/expression/util.hpp>
|
||||
#include <mbgl/style/expression/utf8_op_helpers.hpp>
|
||||
#include <mbgl/style/conversion_impl.hpp>
|
||||
#include <mbgl/tile/geometry_tile_data.hpp>
|
||||
#include <mbgl/math/log2.hpp>
|
||||
@@ -717,6 +718,45 @@ const auto& concatCompoundExpression() {
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& splitCompoundExpression() {
|
||||
static auto signature = detail::makeSignature(
|
||||
"split", [](const std::string& input, const std::string& delimiter) -> Result<std::vector<std::string>> {
|
||||
std::vector<std::string> result;
|
||||
|
||||
if (delimiter.empty()) {
|
||||
std::string_view remaining(input);
|
||||
result.reserve(unicodeLengthOnValidatedUtf8(remaining));
|
||||
while (!remaining.empty()) {
|
||||
const auto characterLength = getUnicodeCharacterOffsetOnValidatedUtf8(remaining, 1);
|
||||
result.emplace_back(remaining.substr(0, characterLength));
|
||||
remaining.remove_prefix(characterLength);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::size_t start = 0;
|
||||
while (true) {
|
||||
const auto position = input.find(delimiter, start);
|
||||
if (position == std::string::npos) {
|
||||
result.emplace_back(input.substr(start));
|
||||
break;
|
||||
}
|
||||
result.emplace_back(input.substr(start, position - start));
|
||||
start = position + delimiter.size();
|
||||
}
|
||||
return result;
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& joinCompoundExpression() {
|
||||
static auto signature = detail::makeSignature(
|
||||
"join", [](const std::vector<std::string>& input, const std::string& delimiter) -> Result<std::string> {
|
||||
return boost::algorithm::join(input, delimiter);
|
||||
});
|
||||
return signature;
|
||||
}
|
||||
|
||||
const auto& resolvedLocaleCompoundExpression() {
|
||||
static auto signature = detail::makeSignature(
|
||||
"resolved-locale", [](const Collator& collator) -> Result<std::string> { return collator.resolvedLocale(); });
|
||||
@@ -1065,6 +1105,8 @@ constexpr const auto compoundExpressionRegistry =
|
||||
{"upcase", upcaseCompoundExpression},
|
||||
{"downcase", downcaseCompoundExpression},
|
||||
{"concat", concatCompoundExpression},
|
||||
{"split", splitCompoundExpression},
|
||||
{"join", joinCompoundExpression},
|
||||
{"resolved-locale", resolvedLocaleCompoundExpression},
|
||||
{"error", errorCompoundExpression},
|
||||
{"feature-state", featureStateCompoundExpression},
|
||||
|
||||
Reference in New Issue
Block a user