| 332 | } |
| 333 | |
| 334 | absl::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 | |
| 366 | Macro MakeOptFlatMapMacro() { |
| 367 | auto status_or_macro = |
nothing calls this directly
no test coverage detected