diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-05-30 12:38:36 +0200 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-05-30 15:58:27 +0200 |
commit | f52a182afe3af442ecd8ecf441124a6489f831a3 (patch) | |
tree | d1bb0c0efaa375ac130b425b6750307721b42bf9 /src | |
parent | 70eed826bc59d924cc3c43ae5f1bc19327493d22 (diff) | |
download | justbuild-f52a182afe3af442ecd8ecf441124a6489f831a3.tar.gz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/buildtool/build_engine/expression/evaluator.cpp | 15 |
1 files changed, 15 insertions, 0 deletions
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}, |