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

Function CompileAndEvalExpr

env/env_test.cc:105–141  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

103};
104
105absl::StatusOr<cel::Value> CompileAndEvalExpr(
106 Env& env, absl::string_view expr,
107 const Activation& activation = Activation()) {
108 CEL_ASSIGN_OR_RETURN(std::unique_ptr<Compiler> compiler, env.NewCompiler());
109 if (compiler == nullptr) {
110 return absl::InternalError("Failed to create compiler");
111 }
112 CEL_ASSIGN_OR_RETURN(ValidationResult result, compiler->Compile(expr));
113 if (!result.GetIssues().empty()) {
114 return absl::InvalidArgumentError(result.FormatError());
115 }
116
117 cel::RuntimeOptions opts;
118 CEL_ASSIGN_OR_RETURN(
119 cel::RuntimeBuilder rt_builder,
120 cel::CreateStandardRuntimeBuilder(env.GetDescriptorPool(), opts));
121 CEL_RETURN_IF_ERROR(cel::EnableReferenceResolver(
122 rt_builder, cel::ReferenceResolverEnabled::kAlways));
123 CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Runtime> runtime,
124 std::move(rt_builder).Build());
125 if (runtime == nullptr) {
126 return absl::InternalError("Failed to create runtime");
127 }
128
129 CEL_ASSIGN_OR_RETURN(std::unique_ptr<Ast> ast, result.ReleaseAst());
130 if (ast == nullptr) {
131 return absl::InternalError("Failed to create AST");
132 }
133 google::protobuf::Arena arena;
134 CEL_ASSIGN_OR_RETURN(std::unique_ptr<Program> program,
135 runtime->CreateProgram(std::move(ast)));
136 if (program == nullptr) {
137 return absl::InternalError("Failed to create program");
138 }
139 CEL_ASSIGN_OR_RETURN(Value value, program->Evaluate(&arena, activation));
140 return value;
141}
142
143absl::StatusOr<bool> CompileAndEvalBooleanExpr(
144 Env& env, absl::string_view expr,

Callers

nothing calls this directly

Calls 10

EnableReferenceResolverFunction · 0.85
NewCompilerMethod · 0.80
GetIssuesMethod · 0.80
FormatErrorMethod · 0.80
ReleaseAstMethod · 0.80
ActivationClass · 0.50
CEL_ASSIGN_OR_RETURNFunction · 0.50
emptyMethod · 0.45
BuildMethod · 0.45
CreateProgramMethod · 0.45

Tested by

no test coverage detected