MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / ExpandTransformList4Macro

Function ExpandTransformList4Macro

extensions/comprehensions_v2_macros.cc:236–278  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

234}
235
236absl::optional<Expr> ExpandTransformList4Macro(MacroExprFactory& factory,
237 Expr& target,
238 absl::Span<Expr> args) {
239 if (args.size() != 4) {
240 return factory.ReportError("transformList() requires 4 arguments");
241 }
242 if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
243 return factory.ReportErrorAt(
244 args[0],
245 "transformList() first variable name must be a simple identifier");
246 }
247 if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
248 return factory.ReportErrorAt(
249 args[1],
250 "transformList() second variable name must be a simple identifier");
251 }
252 if (args[0].ident_expr().name() == args[1].ident_expr().name()) {
253 return factory.ReportErrorAt(args[0],
254 "transformList() second variable must be "
255 "different from the first variable");
256 }
257 if (args[0].ident_expr().name() == kAccumulatorVariableName) {
258 return factory.ReportErrorAt(
259 args[0], absl::StrCat("transformList() first variable name cannot be ",
260 kAccumulatorVariableName));
261 }
262 if (args[1].ident_expr().name() == kAccumulatorVariableName) {
263 return factory.ReportErrorAt(
264 args[1], absl::StrCat("transformList() second variable name cannot be ",
265 kAccumulatorVariableName));
266 }
267 std::string iter_var = args[0].ident_expr().name();
268 std::string iter_var2 = args[1].ident_expr().name();
269 Expr step = factory.NewCall(
270 CelOperator::ADD, factory.NewAccuIdent(),
271 factory.NewList(factory.NewListElement(std::move(args[3]))));
272 step = factory.NewCall(CelOperator::CONDITIONAL, std::move(args[2]),
273 std::move(step), factory.NewAccuIdent());
274 return factory.NewComprehension(std::move(iter_var), std::move(iter_var2),
275 std::move(target), factory.AccuVarName(),
276 factory.NewList(), factory.NewBoolConst(true),
277 std::move(step), factory.NewAccuIdent());
278}
279
280Macro MakeTransformList4Macro() {
281 auto status_or_macro =

Callers

nothing calls this directly

Calls 13

has_ident_exprMethod · 0.80
NewListElementMethod · 0.80
sizeMethod · 0.45
ReportErrorMethod · 0.45
emptyMethod · 0.45
nameMethod · 0.45
ReportErrorAtMethod · 0.45
NewCallMethod · 0.45
NewAccuIdentMethod · 0.45
NewListMethod · 0.45
NewComprehensionMethod · 0.45
AccuVarNameMethod · 0.45

Tested by

no test coverage detected