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

Function EvalCheckedExpr

codelab/exercise2.cc:79–106  ·  view source on GitHub ↗

Parse a cel expression and evaluate it against the given activation and arena.

Source from the content-addressed store, hash-verified

77// Parse a cel expression and evaluate it against the given activation and
78// arena.
79absl::StatusOr<bool> EvalCheckedExpr(const CheckedExpr& checked_expr,
80 const Activation& activation,
81 google::protobuf::Arena* arena) {
82 // Setup a default environment for building expressions.
83 InterpreterOptions options;
84 std::unique_ptr<CelExpressionBuilder> builder = CreateCelExpressionBuilder(
85 google::protobuf::DescriptorPool::generated_pool(),
86 google::protobuf::MessageFactory::generated_factory(), options);
87 CEL_RETURN_IF_ERROR(
88 RegisterBuiltinFunctions(builder->GetRegistry(), options));
89
90 // Note, the expression_plan below is reusable for different inputs, but we
91 // create one just in time for evaluation here.
92 CEL_ASSIGN_OR_RETURN(std::unique_ptr<CelExpression> expression_plan,
93 builder->CreateExpression(&checked_expr));
94
95 CEL_ASSIGN_OR_RETURN(CelValue result,
96 expression_plan->Evaluate(activation, arena));
97
98 if (bool value; result.GetValue(&value)) {
99 return value;
100 } else if (const CelError * value; result.GetValue(&value)) {
101 return *value;
102 } else {
103 return absl::InvalidArgumentError(absl::StrCat(
104 "expected 'bool' result got '", result.DebugString(), "'"));
105 }
106}
107} // namespace
108
109absl::StatusOr<bool> CompileAndEvaluateWithBoolVar(absl::string_view cel_expr,

Callers 2

Calls 7

RegisterBuiltinFunctionsFunction · 0.85
GetRegistryMethod · 0.80
CEL_ASSIGN_OR_RETURNFunction · 0.50
CreateExpressionMethod · 0.45
GetValueMethod · 0.45
DebugStringMethod · 0.45

Tested by

no test coverage detected