| 74 | } // namespace |
| 75 | |
| 76 | absl::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 |