| 84 | } // namespace |
| 85 | |
| 86 | absl::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 |
nothing calls this directly
no test coverage detected