From f52a182afe3af442ecd8ecf441124a6489f831a3 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Mon, 30 May 2022 12:38:36 +0200 Subject: Built-in expressions: add enumerate Add a function transforming a list into a map. In this way, artifacts collected positionally in a list can easily be realized as a stage used for input to an action or output of a target. --- doc/concepts/expressions.org | 5 +++++ src/buildtool/build_engine/expression/evaluator.cpp | 15 +++++++++++++++ .../build_engine/expression/expression.test.cpp | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/doc/concepts/expressions.org b/doc/concepts/expressions.org index 50737413..895b5e0a 100644 --- a/doc/concepts/expressions.org +++ b/doc/concepts/expressions.org @@ -211,6 +211,11 @@ those) argument(s) to obtain the final result. first non-negative integers. For example, ~{"type": "range", "$1": "3"}~ evaluates to ~["0", "1", "2"]~. +- ~"enumerate"~ The argument has to be a list. The result is a map + containing one entry for each element of the list; the key is the + decimal representation of the position in the list (starting from + ~0~) and the value is the element. + - ~"++"~ 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 cfd0be31..76ad3076 100644 --- a/src/buildtool/build_engine/expression/evaluator.cpp +++ b/src/buildtool/build_engine/expression/evaluator.cpp @@ -139,6 +139,20 @@ auto Values(ExpressionPtr const& d) -> ExpressionPtr { return ExpressionPtr{d->Map().Values()}; } +auto Enumerate(ExpressionPtr const& expr) -> ExpressionPtr { + if (not expr->IsList()) { + throw Evaluator::EvaluationError{fmt::format( + "enumerate expects list but instead got: {}.", expr->ToString())}; + } + auto result = Expression::map_t::underlying_map_t{}; + size_t count = 0; + for (auto const& entry : expr->List()) { + result[fmt::format("{}", count)] = entry; + count++; + } + return ExpressionPtr{Expression::map_t{result}}; +} + auto NubRight(ExpressionPtr const& expr) -> ExpressionPtr { if (not expr->IsList()) { throw Evaluator::EvaluationError{fmt::format( @@ -849,6 +863,7 @@ auto const kBuiltInFunctions = {"json_encode", JsonEncodeExpr}, {"escape_chars", EscapeCharsExpr}, {"keys", UnaryExpr(Keys)}, + {"enumerate", UnaryExpr(Enumerate)}, {"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 cbc8c7bf..11ff7a43 100644 --- a/test/buildtool/build_engine/expression/expression.test.cpp +++ b/test/buildtool/build_engine/expression/expression.test.cpp @@ -890,6 +890,24 @@ TEST_CASE("Expression Evaluation", "[expression]") { // NOLINT CHECK(result == Expression::FromJson(R"("esXcXape me XX")"_json)); } + SECTION("enumerate expression") { + auto expr = Expression::FromJson(R"( + { "type": "enumerate" + , "$1": ["foo", "bar", "baz"] + } + )"_json); + REQUIRE(expr); + + auto result = expr.Evaluate(env, fcts); + REQUIRE(result); + CHECK(result == Expression::FromJson(R"( + { "0": "foo" + , "1": "bar" + , "2": "baz" + } + )"_json)); + } + SECTION("keys expression") { auto expr = Expression::FromJson(R"( { "type": "keys" -- cgit v1.2.3