/proc/self/cwd/eval/public/cel_expression.h
Line | Count | Source |
1 | | #ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_EXPRESSION_H_ |
2 | | #define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_EXPRESSION_H_ |
3 | | |
4 | | #include <cstdint> |
5 | | #include <functional> |
6 | | #include <memory> |
7 | | #include <string> |
8 | | #include <utility> |
9 | | |
10 | | #include "cel/expr/checked.pb.h" |
11 | | #include "cel/expr/syntax.pb.h" |
12 | | #include "absl/base/attributes.h" |
13 | | #include "absl/status/statusor.h" |
14 | | #include "absl/strings/string_view.h" |
15 | | #include "eval/public/base_activation.h" |
16 | | #include "eval/public/cel_function_registry.h" |
17 | | #include "eval/public/cel_type_registry.h" |
18 | | #include "eval/public/cel_value.h" |
19 | | #include "google/protobuf/arena.h" |
20 | | |
21 | | namespace google::api::expr::runtime { |
22 | | |
23 | | // CelEvaluationListener is the callback that is passed to (and called by) |
24 | | // CelExpression::Trace. It gets an expression node ID from the original |
25 | | // expression, its value and the arena object. If an expression node |
26 | | // is evaluated multiple times (e.g. as a part of Comprehension.loop_step) |
27 | | // then the order of the callback invocations is guaranteed to correspond |
28 | | // the order of variable sub-elements (e.g. the order of elements returned |
29 | | // by Comprehension.iter_range). |
30 | | // |
31 | | // Expression IDs outside of the range [0, INT32_MAX] are not supported and |
32 | | // will not invoke the listener. While the AST allows any int64, supported |
33 | | // parser implementations should use a dense range starting at 1. In practice, |
34 | | // no AST should contain more than ~ 1e9 nodes. |
35 | | // |
36 | | // ID 0 should not be considered valid, but is supported for legacy reasons. |
37 | | using CelEvaluationListener = std::function<absl::Status( |
38 | | int64_t expr_id, const CelValue&, google::protobuf::Arena*)>; |
39 | | |
40 | | // An opaque state used for evaluation of a CEL expression. |
41 | | class CelEvaluationState { |
42 | | public: |
43 | 17.3k | virtual ~CelEvaluationState() = default; |
44 | | }; |
45 | | |
46 | | // Base interface for expression evaluating objects. |
47 | | class CelExpression { |
48 | | public: |
49 | 17.3k | virtual ~CelExpression() = default; |
50 | | |
51 | | // Initializes the state |
52 | | ABSL_DEPRECATED("Use CreateState and pass arena on each evaluation") |
53 | | virtual std::unique_ptr<CelEvaluationState> InitializeState( |
54 | | google::protobuf::Arena* arena) const = 0; |
55 | | |
56 | | // Initializes the state |
57 | | virtual std::unique_ptr<CelEvaluationState> CreateState() const = 0; |
58 | | |
59 | | // Evaluates expression and returns value. |
60 | | // activation contains bindings from parameter names to values |
61 | | // arena parameter specifies Arena object where output result and |
62 | | // internal data will be allocated. |
63 | | virtual absl::StatusOr<CelValue> Evaluate(const BaseActivation& activation, |
64 | 17.3k | google::protobuf::Arena* arena) const { |
65 | 17.3k | return Evaluate(activation, arena, nullptr); |
66 | 17.3k | } |
67 | | |
68 | | // Evaluates expression and returns value. |
69 | | // activation contains bindings from parameter names to values |
70 | | // state must be non-null and created prior to calling Evaluate by |
71 | | // InitializeState. |
72 | | ABSL_DEPRECATED( |
73 | | "Use CreateState and the overloads which take both an arena and state") |
74 | | virtual absl::StatusOr<CelValue> Evaluate(const BaseActivation& activation, |
75 | 0 | CelEvaluationState* state) const { |
76 | 0 | return Evaluate(activation, nullptr, state); |
77 | 0 | } |
78 | | virtual absl::StatusOr<CelValue> Evaluate(const BaseActivation& activation, |
79 | | google::protobuf::Arena* arena, |
80 | 17.3k | CelEvaluationState* state) const { |
81 | 17.3k | return Trace(activation, arena, nullptr, state); |
82 | 17.3k | } |
83 | | |
84 | | // Trace evaluates expression calling the callback on each sub-tree. |
85 | | virtual absl::StatusOr<CelValue> Trace(const BaseActivation& activation, |
86 | | google::protobuf::Arena* arena, |
87 | 0 | CelEvaluationListener callback) const { |
88 | 0 | return Trace(activation, arena, std::move(callback), nullptr); |
89 | 0 | } |
90 | | |
91 | | // Trace evaluates expression calling the callback on each sub-tree. |
92 | | // state must be non-null and created prior to calling Evaluate by |
93 | | // InitializeState. |
94 | | ABSL_DEPRECATED( |
95 | | "Use CreateState and the overloads which take both an arena and state") |
96 | | virtual absl::StatusOr<CelValue> Trace(const BaseActivation& activation, |
97 | | CelEvaluationState* state, |
98 | 0 | CelEvaluationListener callback) const { |
99 | 0 | return Trace(activation, nullptr, std::move(callback), state); |
100 | 0 | } |
101 | | virtual absl::StatusOr<CelValue> Trace(const BaseActivation& activation, |
102 | | google::protobuf::Arena* arena, |
103 | | CelEvaluationListener callback, |
104 | | CelEvaluationState* state) const = 0; |
105 | | }; |
106 | | |
107 | | // Base class for Expression Builder implementations |
108 | | // Provides user with factory to register extension functions. |
109 | | // ExpressionBuilder MUST NOT be destroyed before CelExpression objects |
110 | | // it built. |
111 | | class CelExpressionBuilder { |
112 | | public: |
113 | 22.7k | CelExpressionBuilder() = default; |
114 | | |
115 | 22.7k | virtual ~CelExpressionBuilder() = default; |
116 | | |
117 | | // Creates CelExpression object from AST tree. |
118 | | // expr specifies root of AST tree. |
119 | | // Method implementation is expected to create copies of expr and source_info, |
120 | | // so that the returned CelExpression is not dependent on the lifetime of |
121 | | // the input arguments. |
122 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
123 | | const cel::expr::Expr* expr, |
124 | | const cel::expr::SourceInfo* source_info) const = 0; |
125 | | |
126 | | // Creates CelExpression object from AST tree. |
127 | | // expr specifies root of AST tree. |
128 | | // non-fatal build warnings are written to warnings if encountered. |
129 | | // Method implementation is expected to create copies of expr and source_info, |
130 | | // so that the returned CelExpression is not dependent on the lifetime of |
131 | | // the input arguments. |
132 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
133 | | const cel::expr::Expr* expr, |
134 | | const cel::expr::SourceInfo* source_info, |
135 | | std::vector<absl::Status>* warnings) const = 0; |
136 | | |
137 | | // Creates CelExpression object from a checked expression. |
138 | | // This includes an AST, source info, type hints and ident hints. |
139 | | // Method implementation is expected to create copy of checked_expr, |
140 | | // so that the returned CelExpression is not dependent on the lifetime of |
141 | | // the input arguments. |
142 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
143 | 0 | const cel::expr::CheckedExpr* checked_expr) const { |
144 | | // Default implementation just passes through the expr and source info. |
145 | 0 | return CreateExpression(&checked_expr->expr(), |
146 | 0 | &checked_expr->source_info()); |
147 | 0 | } |
148 | | |
149 | | // Creates CelExpression object from a checked expression. |
150 | | // This includes an AST, source info, type hints and ident hints. |
151 | | // non-fatal build warnings are written to warnings if encountered. |
152 | | // Method implementation is expected to create copy of checked_expr, |
153 | | // so that the returned CelExpression is not dependent on the lifetime of |
154 | | // the input arguments. |
155 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
156 | | const cel::expr::CheckedExpr* checked_expr, |
157 | 0 | std::vector<absl::Status>* warnings) const { |
158 | | // Default implementation just passes through the expr and source_info. |
159 | 0 | return CreateExpression(&checked_expr->expr(), &checked_expr->source_info(), |
160 | 0 | warnings); |
161 | 0 | } |
162 | | |
163 | | // CelFunction registry. Extension function should be registered with it |
164 | | // prior to expression creation. |
165 | | virtual CelFunctionRegistry* GetRegistry() const = 0; |
166 | | |
167 | | // CEL Type registry. Provides a means to resolve the CEL built-in types to |
168 | | // CelValue instances, and to extend the set of types and enums known to |
169 | | // expressions by registering them ahead of time. |
170 | | virtual CelTypeRegistry* GetTypeRegistry() const = 0; |
171 | | |
172 | | virtual void set_container(std::string container) = 0; |
173 | | |
174 | | virtual absl::string_view container() const = 0; |
175 | | }; |
176 | | |
177 | | } // namespace google::api::expr::runtime |
178 | | |
179 | | #endif // THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_EXPRESSION_H_ |