|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "codelab/exercise10.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <utility> |
| 19 | + |
| 20 | +#include "absl/status/status.h" |
| 21 | +#include "absl/status/statusor.h" |
| 22 | +#include "absl/strings/str_cat.h" |
| 23 | +#include "absl/strings/string_view.h" |
| 24 | +#include "checker/validation_result.h" |
| 25 | +#include "codelab/network_functions.h" |
| 26 | +#include "common/decl.h" |
| 27 | +#include "common/minimal_descriptor_pool.h" |
| 28 | +#include "common/type.h" |
| 29 | +#include "common/value.h" |
| 30 | +#include "compiler/compiler.h" |
| 31 | +#include "compiler/compiler_factory.h" |
| 32 | +#include "compiler/standard_library.h" |
| 33 | +#include "runtime/activation.h" |
| 34 | +#include "runtime/runtime.h" |
| 35 | +#include "runtime/runtime_builder.h" |
| 36 | +#include "runtime/runtime_options.h" |
| 37 | +#include "runtime/standard_runtime_builder_factory.h" |
| 38 | +#include "google/protobuf/arena.h" |
| 39 | + |
| 40 | +namespace cel_codelab { |
| 41 | + |
| 42 | +namespace { |
| 43 | + |
| 44 | +absl::StatusOr<std::unique_ptr<cel::Compiler>> ConfigureCompiler() { |
| 45 | + absl::StatusOr<std::unique_ptr<cel::CompilerBuilder>> compiler_builder = |
| 46 | + cel::NewCompilerBuilder(cel::GetMinimalDescriptorPool()); |
| 47 | + if (!compiler_builder.ok()) { |
| 48 | + return std::move(compiler_builder).status(); |
| 49 | + } |
| 50 | + absl::Status s = |
| 51 | + (*compiler_builder)->AddLibrary(cel::StandardCompilerLibrary()); |
| 52 | + // =========================================================================== |
| 53 | + // Codelab: Update compiler builder with functions from network_functions.h |
| 54 | + // and add a varible for the input IP. |
| 55 | + // =========================================================================== |
| 56 | + if (!s.ok()) return s; |
| 57 | + |
| 58 | + return (*compiler_builder)->Build(); |
| 59 | +} |
| 60 | + |
| 61 | +absl::StatusOr<std::unique_ptr<cel::Runtime>> ConfigureRuntime() { |
| 62 | + cel::RuntimeOptions runtime_options; |
| 63 | + // Note: this is needed to resolve net.Address as a `type` constant. |
| 64 | + runtime_options.enable_qualified_type_identifiers = true; |
| 65 | + absl::StatusOr<cel::RuntimeBuilder> runtime_builder = |
| 66 | + cel::CreateStandardRuntimeBuilder(cel::GetMinimalDescriptorPool(), |
| 67 | + runtime_options); |
| 68 | + // =========================================================================== |
| 69 | + // Codelab: Update runtime builder with functions from network_functions.h |
| 70 | + // =========================================================================== |
| 71 | + return std::move(runtime_builder).value().Build(); |
| 72 | +} |
| 73 | + |
| 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 |
0 commit comments