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

Function CompileAndEvaluateExercise10

codelab/exercise10.cc:76–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

TESTFunction · 0.70

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 1

TESTFunction · 0.56