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

Function ExpandOptFlatMapMacro

parser/macro.cc:334–364  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

332}
333
334absl::optional<Expr> ExpandOptFlatMapMacro(MacroExprFactory& factory,
335 Expr& target,
336 absl::Span<Expr> args) {
337 if (args.size() != 2) {
338 return factory.ReportError("optFlatMap() requires 2 arguments");
339 }
340 if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
341 return factory.ReportErrorAt(
342 args[0], "optFlatMap() variable name must be a simple identifier");
343 }
344 if (args[0].ident_expr().name() == kAccumulatorVariableName) {
345 return factory.ReportErrorAt(
346 args[1], absl::StrCat("optFlatMap() variable name cannot be ",
347 kAccumulatorVariableName));
348 }
349 auto var_name = args[0].ident_expr().name();
350
351 auto target_copy = factory.Copy(target);
352 std::vector<Expr> call_args;
353 call_args.reserve(3);
354 call_args.push_back(factory.NewMemberCall("hasValue", std::move(target)));
355 auto iter_range = factory.NewList();
356 auto accu_init = factory.NewMemberCall("value", std::move(target_copy));
357 auto condition = factory.NewBoolConst(false);
358 call_args.push_back(factory.NewComprehension(
359 "#unused", std::move(iter_range), std::move(var_name),
360 std::move(accu_init), std::move(condition), std::move(args[0]),
361 std::move(args[1])));
362 call_args.push_back(factory.NewCall("optional.none"));
363 return factory.NewCall(CelOperator::CONDITIONAL, std::move(call_args));
364}
365
366Macro MakeOptFlatMapMacro() {
367 auto status_or_macro =

Callers

nothing calls this directly

Calls 12

has_ident_exprMethod · 0.80
CopyMethod · 0.80
sizeMethod · 0.45
ReportErrorMethod · 0.45
emptyMethod · 0.45
nameMethod · 0.45
ReportErrorAtMethod · 0.45
NewMemberCallMethod · 0.45
NewListMethod · 0.45
NewBoolConstMethod · 0.45
NewComprehensionMethod · 0.45
NewCallMethod · 0.45

Tested by

no test coverage detected