forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise2.cc
More file actions
143 lines (122 loc) · 5.48 KB
/
Copy pathexercise2.cc
File metadata and controls
143 lines (122 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "codelab/exercise2.h"
#include <memory>
#include "cel/expr/checked.pb.h"
#include "cel/expr/syntax.pb.h"
#include "google/rpc/context/attribute_context.pb.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "codelab/cel_compiler.h"
#include "compiler/compiler.h"
#include "compiler/compiler_factory.h"
#include "compiler/standard_library.h"
#include "eval/public/activation.h"
#include "eval/public/builtin_func_registrar.h"
#include "eval/public/cel_expr_builder_factory.h"
#include "eval/public/cel_expression.h"
#include "eval/public/cel_options.h"
#include "eval/public/cel_value.h"
#include "internal/status_macros.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel_codelab {
namespace {
using ::cel::expr::CheckedExpr;
using ::google::api::expr::runtime::Activation;
using ::google::api::expr::runtime::CelError;
using ::google::api::expr::runtime::CelExpression;
using ::google::api::expr::runtime::CelExpressionBuilder;
using ::google::api::expr::runtime::CelValue;
using ::google::api::expr::runtime::CreateCelExpressionBuilder;
using ::google::api::expr::runtime::InterpreterOptions;
using ::google::api::expr::runtime::RegisterBuiltinFunctions;
using ::google::rpc::context::AttributeContext;
absl::StatusOr<std::unique_ptr<cel::Compiler>> MakeCelCompiler() {
// Note: we are using the generated descriptor pool here for simplicity, but
// it has the drawback of including all message types that are linked into the
// binary instead of just the ones expected for the CEL environment.
google::protobuf::LinkMessageReflection<AttributeContext>();
CEL_ASSIGN_OR_RETURN(
std::unique_ptr<cel::CompilerBuilder> builder,
cel::NewCompilerBuilder(google::protobuf::DescriptorPool::generated_pool()));
CEL_RETURN_IF_ERROR(builder->AddLibrary(cel::StandardCompilerLibrary()));
// === Start Codelab ===
// Add 'AttributeContext' as a context message to the type checker and a
// boolean variable 'bool_var'. Relevant functions are on the
// TypeCheckerBuilder class (see CompilerBuilder::GetCheckerBuilder).
//
// We're reusing the same compiler for both evaluation paths here for brevity,
// but it's likely a better fit to configure a separate compiler per use case.
// === End Codelab ===
return builder->Build();
}
// Parse a cel expression and evaluate it against the given activation and
// arena.
absl::StatusOr<bool> EvalCheckedExpr(const CheckedExpr& checked_expr,
const Activation& activation,
google::protobuf::Arena* arena) {
// Setup a default environment for building expressions.
InterpreterOptions options;
std::unique_ptr<CelExpressionBuilder> builder = CreateCelExpressionBuilder(
google::protobuf::DescriptorPool::generated_pool(),
google::protobuf::MessageFactory::generated_factory(), options);
CEL_RETURN_IF_ERROR(
RegisterBuiltinFunctions(builder->GetRegistry(), options));
// Note, the expression_plan below is reusable for different inputs, but we
// create one just in time for evaluation here.
CEL_ASSIGN_OR_RETURN(std::unique_ptr<CelExpression> expression_plan,
builder->CreateExpression(&checked_expr));
CEL_ASSIGN_OR_RETURN(CelValue result,
expression_plan->Evaluate(activation, arena));
if (bool value; result.GetValue(&value)) {
return value;
} else if (const CelError * value; result.GetValue(&value)) {
return *value;
} else {
return absl::InvalidArgumentError(absl::StrCat(
"expected 'bool' result got '", result.DebugString(), "'"));
}
}
} // namespace
absl::StatusOr<bool> CompileAndEvaluateWithBoolVar(absl::string_view cel_expr,
bool bool_var) {
CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Compiler> compiler,
MakeCelCompiler());
CEL_ASSIGN_OR_RETURN(CheckedExpr checked_expr,
CompileToCheckedExpr(*compiler, cel_expr));
Activation activation;
google::protobuf::Arena arena;
// === Start Codelab ===
// Update the activation to bind the bool argument to 'bool_var'
// === End Codelab ===
return EvalCheckedExpr(checked_expr, activation, &arena);
}
absl::StatusOr<bool> CompileAndEvaluateWithContext(
absl::string_view cel_expr, const AttributeContext& context) {
CEL_ASSIGN_OR_RETURN(std::unique_ptr<cel::Compiler> compiler,
MakeCelCompiler());
CEL_ASSIGN_OR_RETURN(CheckedExpr checked_expr,
CompileToCheckedExpr(*compiler, cel_expr));
Activation activation;
google::protobuf::Arena arena;
// === Start Codelab ===
// Update the activation to bind the AttributeContext.
// === End Codelab ===
return EvalCheckedExpr(checked_expr, activation, &arena);
}
} // namespace cel_codelab