/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 | | |
9 | | #include "cel/expr/checked.pb.h" |
10 | | #include "cel/expr/syntax.pb.h" |
11 | | #include "absl/status/statusor.h" |
12 | | #include "absl/strings/string_view.h" |
13 | | #include "eval/public/base_activation.h" |
14 | | #include "eval/public/cel_function_registry.h" |
15 | | #include "eval/public/cel_type_registry.h" |
16 | | #include "eval/public/cel_value.h" |
17 | | |
18 | | namespace google::api::expr::runtime { |
19 | | |
20 | | // CelEvaluationListener is the callback that is passed to (and called by) |
21 | | // CelExpression::Trace. It gets an expression node ID from the original |
22 | | // expression, its value and the arena object. If an expression node |
23 | | // is evaluated multiple times (e.g. as a part of Comprehension.loop_step) |
24 | | // then the order of the callback invocations is guaranteed to correspond |
25 | | // the order of variable sub-elements (e.g. the order of elements returned |
26 | | // by Comprehension.iter_range). |
27 | | using CelEvaluationListener = std::function<absl::Status( |
28 | | int64_t expr_id, const CelValue&, google::protobuf::Arena*)>; |
29 | | |
30 | | // An opaque state used for evaluation of a CEL expression. |
31 | | class CelEvaluationState { |
32 | | public: |
33 | 9.92k | virtual ~CelEvaluationState() = default; |
34 | | }; |
35 | | |
36 | | // Base interface for expression evaluating objects. |
37 | | class CelExpression { |
38 | | public: |
39 | 9.92k | virtual ~CelExpression() = default; |
40 | | |
41 | | // Initializes the state |
42 | | virtual std::unique_ptr<CelEvaluationState> InitializeState( |
43 | | google::protobuf::Arena* arena) const = 0; |
44 | | |
45 | | // Evaluates expression and returns value. |
46 | | // activation contains bindings from parameter names to values |
47 | | // arena parameter specifies Arena object where output result and |
48 | | // internal data will be allocated. |
49 | | virtual absl::StatusOr<CelValue> Evaluate(const BaseActivation& activation, |
50 | | google::protobuf::Arena* arena) const = 0; |
51 | | |
52 | | // Evaluates expression and returns value. |
53 | | // activation contains bindings from parameter names to values |
54 | | // state must be non-null and created prior to calling Evaluate by |
55 | | // InitializeState. |
56 | | virtual absl::StatusOr<CelValue> Evaluate( |
57 | | const BaseActivation& activation, CelEvaluationState* state) const = 0; |
58 | | |
59 | | // Trace evaluates expression calling the callback on each sub-tree. |
60 | | virtual absl::StatusOr<CelValue> Trace( |
61 | | const BaseActivation& activation, google::protobuf::Arena* arena, |
62 | | CelEvaluationListener callback) const = 0; |
63 | | |
64 | | // Trace evaluates expression calling the callback on each sub-tree. |
65 | | // state must be non-null and created prior to calling Evaluate by |
66 | | // InitializeState. |
67 | | virtual absl::StatusOr<CelValue> Trace( |
68 | | const BaseActivation& activation, CelEvaluationState* state, |
69 | | CelEvaluationListener callback) const = 0; |
70 | | }; |
71 | | |
72 | | // Base class for Expression Builder implementations |
73 | | // Provides user with factory to register extension functions. |
74 | | // ExpressionBuilder MUST NOT be destroyed before CelExpression objects |
75 | | // it built. |
76 | | class CelExpressionBuilder { |
77 | | public: |
78 | 14.5k | CelExpressionBuilder() = default; |
79 | | |
80 | 14.5k | virtual ~CelExpressionBuilder() = default; |
81 | | |
82 | | // Creates CelExpression object from AST tree. |
83 | | // expr specifies root of AST tree. |
84 | | // Method implementation is expected to create copies of expr and source_info, |
85 | | // so that the returned CelExpression is not dependent on the lifetime of |
86 | | // the input arguments. |
87 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
88 | | const cel::expr::Expr* expr, |
89 | | const cel::expr::SourceInfo* source_info) const = 0; |
90 | | |
91 | | // Creates CelExpression object from AST tree. |
92 | | // expr specifies root of AST tree. |
93 | | // non-fatal build warnings are written to warnings if encountered. |
94 | | // Method implementation is expected to create copies of expr and source_info, |
95 | | // so that the returned CelExpression is not dependent on the lifetime of |
96 | | // the input arguments. |
97 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
98 | | const cel::expr::Expr* expr, |
99 | | const cel::expr::SourceInfo* source_info, |
100 | | std::vector<absl::Status>* warnings) const = 0; |
101 | | |
102 | | // Creates CelExpression object from a checked expression. |
103 | | // This includes an AST, source info, type hints and ident hints. |
104 | | // Method implementation is expected to create copy of checked_expr, |
105 | | // so that the returned CelExpression is not dependent on the lifetime of |
106 | | // the input arguments. |
107 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
108 | 0 | const cel::expr::CheckedExpr* checked_expr) const { |
109 | | // Default implementation just passes through the expr and source info. |
110 | 0 | return CreateExpression(&checked_expr->expr(), |
111 | 0 | &checked_expr->source_info()); |
112 | 0 | } |
113 | | |
114 | | // Creates CelExpression object from a checked expression. |
115 | | // This includes an AST, source info, type hints and ident hints. |
116 | | // non-fatal build warnings are written to warnings if encountered. |
117 | | // Method implementation is expected to create copy of checked_expr, |
118 | | // so that the returned CelExpression is not dependent on the lifetime of |
119 | | // the input arguments. |
120 | | virtual absl::StatusOr<std::unique_ptr<CelExpression>> CreateExpression( |
121 | | const cel::expr::CheckedExpr* checked_expr, |
122 | 0 | std::vector<absl::Status>* warnings) const { |
123 | | // Default implementation just passes through the expr and source_info. |
124 | 0 | return CreateExpression(&checked_expr->expr(), &checked_expr->source_info(), |
125 | 0 | warnings); |
126 | 0 | } |
127 | | |
128 | | // CelFunction registry. Extension function should be registered with it |
129 | | // prior to expression creation. |
130 | | virtual CelFunctionRegistry* GetRegistry() const = 0; |
131 | | |
132 | | // CEL Type registry. Provides a means to resolve the CEL built-in types to |
133 | | // CelValue instances, and to extend the set of types and enums known to |
134 | | // expressions by registering them ahead of time. |
135 | | virtual CelTypeRegistry* GetTypeRegistry() const = 0; |
136 | | |
137 | | virtual void set_container(std::string container) = 0; |
138 | | |
139 | | virtual absl::string_view container() const = 0; |
140 | | }; |
141 | | |
142 | | } // namespace google::api::expr::runtime |
143 | | |
144 | | #endif // THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_CEL_EXPRESSION_H_ |