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

Function EvalCheckedExpr

codelab/solutions/exercise2.cc:83–110  ·  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

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