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

Function CompileAndEvaluateExercise10

codelab/solutions/exercise10.cc:86–134  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

84} // namespace
85
86absl::StatusOr<bool> CompileAndEvaluateExercise10(absl::string_view expression,
87 absl::string_view ip) {
88 absl::StatusOr<std::unique_ptr<cel::Compiler>> compiler = ConfigureCompiler();
89 if (!compiler.ok()) {
90 return std::move(compiler).status();
91 }
92
93 absl::StatusOr<std::unique_ptr<cel::Runtime>> runtime = ConfigureRuntime();
94 if (!runtime.ok()) {
95 return std::move(runtime).status();
96 }
97
98 absl::StatusOr<cel::ValidationResult> checked =
99 (*compiler)->Compile(expression);
100 if (!checked.ok()) {
101 return std::move(checked).status();
102 }
103
104 if (!checked->IsValid() || checked->GetAst() == nullptr) {
105 return absl::InvalidArgumentError(checked->FormatError());
106 }
107
108 absl::StatusOr<std::unique_ptr<cel::Program>> program =
109 (*runtime)->CreateProgram(checked->ReleaseAst().value());
110
111 if (!program.ok()) {
112 return std::move(program).status();
113 }
114
115 cel::Activation activation;
116 google::protobuf::Arena arena;
117 activation.InsertOrAssignValue("ip", cel::StringValue::From(ip, &arena));
118 absl::StatusOr<cel::Value> result = (*program)->Evaluate(&arena, activation);
119
120 if (!result.ok()) {
121 return std::move(result).status();
122 }
123
124 if (result->IsBool()) {
125 return result->GetBool();
126 }
127
128 if (result->IsError()) {
129 return result->GetError().ToStatus();
130 }
131
132 return absl::InvalidArgumentError(
133 absl::StrCat("unexpected result type: ", result->DebugString()));
134}
135
136} // namespace cel_codelab

Callers

nothing calls this directly

Calls 15

IsValidMethod · 0.80
GetAstMethod · 0.80
FormatErrorMethod · 0.80
ReleaseAstMethod · 0.80
InsertOrAssignValueMethod · 0.80
ConfigureCompilerFunction · 0.70
ConfigureRuntimeFunction · 0.70
okMethod · 0.45
CompileMethod · 0.45
CreateProgramMethod · 0.45
valueMethod · 0.45
EvaluateMethod · 0.45

Tested by

no test coverage detected