diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-22 10:48:46 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-24 12:30:09 +0200 |
commit | 14b93071eca21c69b635bd77eb68057b2906ddab (patch) | |
tree | 324d82d99e9015239f548acf17e1e9373af1f65a | |
parent | 199487b5ba387653bbec36d6dcea85819c90994c (diff) | |
download | justbuild-14b93071eca21c69b635bd77eb68057b2906ddab.tar.gz |
expressions: add "length" function
Lists are somtimes used in configurations as replacement for tuples.
Providing length gives an easy way to detect usage errors.
-rw-r--r-- | doc/concepts/expressions.md | 3 | ||||
-rw-r--r-- | src/buildtool/build_engine/expression/evaluator.cpp | 10 | ||||
-rw-r--r-- | test/buildtool/build_engine/expression/expression.test.cpp | 13 |
3 files changed, 26 insertions, 0 deletions
diff --git a/doc/concepts/expressions.md b/doc/concepts/expressions.md index 80378191..a14cd60f 100644 --- a/doc/concepts/expressions.md +++ b/doc/concepts/expressions.md @@ -256,6 +256,9 @@ those) argument(s) to obtain the final result. - `"reverse"` The argument has to be a list. The result is a new list with the entries in reverse order. +- `"length"` The argument has to be a list. The result is the length + of the list. + - `"++"` 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 f91e5b13..11299bb5 100644 --- a/src/buildtool/build_engine/expression/evaluator.cpp +++ b/src/buildtool/build_engine/expression/evaluator.cpp @@ -232,6 +232,15 @@ auto Reverse(ExpressionPtr const& expr) -> ExpressionPtr { return ExpressionPtr{reverse_result}; } +auto Length(ExpressionPtr const& expr) -> ExpressionPtr { + if (not expr->IsList()) { + throw Evaluator::EvaluationError{fmt::format( + "length expects list but instead got: {}.", expr->ToString())}; + } + return ExpressionPtr{ + static_cast<Expression::number_t>(expr->List().size())}; +} + auto NubRight(ExpressionPtr const& expr) -> ExpressionPtr { if (not expr->IsList()) { throw Evaluator::EvaluationError{fmt::format( @@ -1098,6 +1107,7 @@ auto const kBuiltInFunctions = {"enumerate", UnaryExpr(Enumerate)}, {"set", UnaryExpr(Set)}, {"reverse", UnaryExpr(Reverse)}, + {"length", UnaryExpr(Length)}, {"values", UnaryExpr(Values)}, {"lookup", LookupExpr}, {"[]", ArrayAccessExpr}, diff --git a/test/buildtool/build_engine/expression/expression.test.cpp b/test/buildtool/build_engine/expression/expression.test.cpp index 18189baf..83550ebd 100644 --- a/test/buildtool/build_engine/expression/expression.test.cpp +++ b/test/buildtool/build_engine/expression/expression.test.cpp @@ -1010,6 +1010,19 @@ TEST_CASE("Expression Evaluation", "[expression]") { // NOLINT )"_json)); } + SECTION("length expression") { + auto expr = Expression::FromJson(R"( + { "type": "length" + , "$1": ["foo", "bar", "baz"] + } + )"_json); + REQUIRE(expr); + + auto result = expr.Evaluate(env, fcts); + REQUIRE(result); + CHECK(result == Expression::FromJson("3"_json)); + } + SECTION("keys expression") { auto expr = Expression::FromJson(R"( { "type": "keys" |