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

Function ExpandOptMapMacro

parser/macro.cc:296–326  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

294}
295
296absl::optional<Expr> ExpandOptMapMacro(MacroExprFactory& factory, Expr& target,
297 absl::Span<Expr> args) {
298 if (args.size() != 2) {
299 return factory.ReportError("optMap() requires 2 arguments");
300 }
301 if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
302 return factory.ReportErrorAt(
303 args[0], "optMap() variable name must be a simple identifier");
304 }
305 if (args[0].ident_expr().name() == kAccumulatorVariableName) {
306 return factory.ReportErrorAt(
307 args[1], absl::StrCat("optMap() variable name cannot be ",
308 kAccumulatorVariableName));
309 }
310 auto var_name = args[0].ident_expr().name();
311
312 auto target_copy = factory.Copy(target);
313 std::vector<Expr> call_args;
314 call_args.reserve(3);
315 call_args.push_back(factory.NewMemberCall("hasValue", std::move(target)));
316 auto iter_range = factory.NewList();
317 auto accu_init = factory.NewMemberCall("value", std::move(target_copy));
318 auto condition = factory.NewBoolConst(false);
319 auto fold = factory.NewComprehension(
320 "#unused", std::move(iter_range), std::move(var_name),
321 std::move(accu_init), std::move(condition), std::move(args[0]),
322 std::move(args[1]));
323 call_args.push_back(factory.NewCall("optional.of", std::move(fold)));
324 call_args.push_back(factory.NewCall("optional.none"));
325 return factory.NewCall(CelOperator::CONDITIONAL, std::move(call_args));
326}
327
328Macro MakeOptMapMacro() {
329 auto status_or_macro = Macro::Receiver("optMap", 2, ExpandOptMapMacro);

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