summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2023-08-14 11:43:28 +0200
committerKlaus Aehlig <klaus.aehlig@huawei.com>2023-08-14 12:09:57 +0200
commit726e70cb9bf544ee3164e32405e4dd4f750c0403 (patch)
tree7e15485967af5f24852e30d54059b62a40e8928d
parent755360ab129879c8981c84262ef1f1d3e697a5e0 (diff)
downloadjustbuild-726e70cb9bf544ee3164e32405e4dd4f750c0403.tar.gz
expression: add new built in "set"
... to obtain from a list of strings a map with those entries as keys and true as value. In this way, repeated membership tests in lists can be implemented more efficiently.
-rw-r--r--doc/concepts/expressions.md4
-rw-r--r--src/buildtool/build_engine/expression/evaluator.cpp19
-rw-r--r--test/buildtool/build_engine/expression/expression.test.cpp18
3 files changed, 41 insertions, 0 deletions
diff --git a/doc/concepts/expressions.md b/doc/concepts/expressions.md
index b6aa0945..c66de8bc 100644
--- a/doc/concepts/expressions.md
+++ b/doc/concepts/expressions.md
@@ -249,6 +249,10 @@ those) argument(s) to obtain the final result.
same iteration order as the list for all lists indexable by
32-bit integers.
+- `"set"` The argument has to be a list of strings. The result is
+ a map with the members of the list as keys, and all values being
+ `true`.
+
- `"++"` The argument has to be a list of lists. The result is
the concatenation of those lists.
diff --git a/src/buildtool/build_engine/expression/evaluator.cpp b/src/buildtool/build_engine/expression/evaluator.cpp
index 6aeea591..431792e8 100644
--- a/src/buildtool/build_engine/expression/evaluator.cpp
+++ b/src/buildtool/build_engine/expression/evaluator.cpp
@@ -167,6 +167,24 @@ auto Enumerate(ExpressionPtr const& expr) -> ExpressionPtr {
return ExpressionPtr{Expression::map_t{result}};
}
+auto Set(ExpressionPtr const& expr) -> ExpressionPtr {
+ if (not expr->IsList()) {
+ throw Evaluator::EvaluationError{
+ fmt::format("set expects list of strings but instead got: {}.",
+ expr->ToString())};
+ }
+ auto result = Expression::map_t::underlying_map_t{};
+ for (auto const& entry : expr->List()) {
+ if (not entry->IsString()) {
+ throw Evaluator::EvaluationError{
+ fmt::format("set expects list of strings found entry: {}.",
+ entry->ToString())};
+ }
+ result[entry->String()] = Expression::kTrue;
+ }
+ return ExpressionPtr{Expression::map_t{result}};
+}
+
auto NubRight(ExpressionPtr const& expr) -> ExpressionPtr {
if (not expr->IsList()) {
throw Evaluator::EvaluationError{fmt::format(
@@ -938,6 +956,7 @@ auto const kBuiltInFunctions =
{"escape_chars", EscapeCharsExpr},
{"keys", UnaryExpr(Keys)},
{"enumerate", UnaryExpr(Enumerate)},
+ {"set", UnaryExpr(Set)},
{"values", UnaryExpr(Values)},
{"lookup", LookupExpr},
{"empty_map", EmptyMapExpr},
diff --git a/test/buildtool/build_engine/expression/expression.test.cpp b/test/buildtool/build_engine/expression/expression.test.cpp
index 10edbcbd..7f3df7c5 100644
--- a/test/buildtool/build_engine/expression/expression.test.cpp
+++ b/test/buildtool/build_engine/expression/expression.test.cpp
@@ -924,6 +924,24 @@ TEST_CASE("Expression Evaluation", "[expression]") { // NOLINT
)"_json));
}
+ SECTION("set expression") {
+ auto expr = Expression::FromJson(R"(
+ { "type": "set"
+ , "$1": ["foo", "bar", "baz"]
+ }
+ )"_json);
+ REQUIRE(expr);
+
+ auto result = expr.Evaluate(env, fcts);
+ REQUIRE(result);
+ CHECK(result == Expression::FromJson(R"(
+ { "foo": true
+ , "bar": true
+ , "baz": true
+ }
+ )"_json));
+ }
+
SECTION("keys expression") {
auto expr = Expression::FromJson(R"(
{ "type": "keys"