| 294 | } |
| 295 | |
| 296 | absl::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 | |
| 328 | Macro MakeOptMapMacro() { |
| 329 | auto status_or_macro = Macro::Receiver("optMap", 2, ExpandOptMapMacro); |
nothing calls this directly
no test coverage detected