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

Function ListSortByMacro

extensions/lists_functions.cc:432–478  ·  view source on GitHub ↗

This macro transforms an expression like: mylistExpr.sortBy(e, -math.abs(e)) into something equivalent to: cel.bind( @__sortBy_input__, myListExpr, @__sortBy_input__.@sortByAssociatedKeys( @__sortBy_input__.map(e, -math.abs(e) ) )

Source from the content-addressed store, hash-verified

430// )
431// )
432Macro ListSortByMacro() {
433 absl::StatusOr<Macro> sortby_macro = Macro::Receiver(
434 "sortBy", 2,
435 [](MacroExprFactory& factory, Expr& target,
436 absl::Span<Expr> args) -> absl::optional<Expr> {
437 if (!target.has_ident_expr() && !target.has_select_expr() &&
438 !target.has_list_expr() && !target.has_comprehension_expr() &&
439 !target.has_call_expr()) {
440 return factory.ReportErrorAt(
441 target,
442 "sortBy can only be applied to a list, identifier, "
443 "comprehension, call or select expression");
444 }
445
446 auto sortby_input_ident = factory.NewIdent("@__sortBy_input__");
447 auto sortby_input_expr = std::move(target);
448 auto key_ident = std::move(args[0]);
449 auto key_expr = std::move(args[1]);
450
451 // Build the map expression:
452 // map_compr := @__sortBy_input__.map(key_ident, key_expr)
453 auto map_compr =
454 MakeMapComprehension(factory, factory.Copy(sortby_input_ident),
455 std::move(key_ident), std::move(key_expr));
456 if (!map_compr.has_value()) {
457 return absl::nullopt;
458 }
459
460 // Build the call expression:
461 // call_expr := @__sortBy_input__.@sortByAssociatedKeys(map_compr)
462 std::vector<Expr> call_args;
463 call_args.push_back(std::move(*map_compr));
464 auto call_expr = factory.NewMemberCall("@sortByAssociatedKeys",
465 std::move(sortby_input_ident),
466 absl::MakeSpan(call_args));
467
468 // Build the returned bind expression:
469 // cel.bind(@__sortBy_input__, target, call_expr)
470 auto var_ident = factory.NewIdent("@__sortBy_input__");
471 auto var_expr = std::move(sortby_input_expr);
472 auto bind_compr =
473 MakeBindComprehension(factory, std::move(var_ident),
474 std::move(var_expr), std::move(call_expr));
475 return bind_compr;
476 });
477 return *sortby_macro;
478}
479
480absl::StatusOr<Value> ListSort(
481 const ListValue& list,

Callers 1

lists_macrosFunction · 0.85

Calls 12

MakeMapComprehensionFunction · 0.85
MakeBindComprehensionFunction · 0.85
has_ident_exprMethod · 0.80
has_select_exprMethod · 0.80
has_list_exprMethod · 0.80
has_call_exprMethod · 0.80
CopyMethod · 0.80
ReportErrorAtMethod · 0.45
NewIdentMethod · 0.45
has_valueMethod · 0.45
NewMemberCallMethod · 0.45

Tested by

no test coverage detected