/proc/self/cwd/eval/eval/trace_step.h
Line | Count | Source |
1 | | // Copyright 2024 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 | | #ifndef THIRD_PARTY_CEL_CPP_EVAL_EVAL_TRACE_STEP_H_ |
15 | | #define THIRD_PARTY_CEL_CPP_EVAL_EVAL_TRACE_STEP_H_ |
16 | | |
17 | | #include <memory> |
18 | | #include <utility> |
19 | | #include <vector> |
20 | | |
21 | | #include "absl/status/status.h" |
22 | | #include "absl/types/optional.h" |
23 | | #include "common/native_type.h" |
24 | | #include "common/value.h" |
25 | | #include "eval/eval/attribute_trail.h" |
26 | | #include "eval/eval/direct_expression_step.h" |
27 | | #include "eval/eval/evaluator_core.h" |
28 | | #include "internal/status_macros.h" |
29 | | namespace google::api::expr::runtime { |
30 | | |
31 | | // A decorator that implements tracing for recursively evaluated CEL |
32 | | // expressions. |
33 | | // |
34 | | // Allows inspection for extensions to extract the wrapped expression. |
35 | | class TraceStep : public DirectExpressionStep { |
36 | | public: |
37 | | explicit TraceStep(std::unique_ptr<DirectExpressionStep> expression) |
38 | 0 | : DirectExpressionStep(-1), expression_(std::move(expression)) {} |
39 | | |
40 | | absl::Status Evaluate(ExecutionFrameBase& frame, cel::Value& result, |
41 | 0 | AttributeTrail& trail) const override { |
42 | 0 | CEL_RETURN_IF_ERROR(expression_->Evaluate(frame, result, trail)); |
43 | 0 | if (!frame.callback()) { |
44 | 0 | return absl::OkStatus(); |
45 | 0 | } |
46 | 0 | return frame.callback()(expression_->expr_id(), result, |
47 | 0 | frame.descriptor_pool(), frame.message_factory(), |
48 | 0 | frame.arena()); |
49 | 0 | } |
50 | | |
51 | 0 | cel::NativeTypeId GetNativeTypeId() const override { |
52 | 0 | return cel::NativeTypeId::For<TraceStep>(); |
53 | 0 | } |
54 | | |
55 | | absl::optional<std::vector<const DirectExpressionStep*>> GetDependencies() |
56 | 0 | const override { |
57 | 0 | return {{expression_.get()}}; |
58 | 0 | } |
59 | | |
60 | | absl::optional<std::vector<std::unique_ptr<DirectExpressionStep>>> |
61 | 0 | ExtractDependencies() override { |
62 | 0 | std::vector<std::unique_ptr<DirectExpressionStep>> dependencies; |
63 | 0 | dependencies.push_back(std::move(expression_)); |
64 | 0 | return dependencies; |
65 | 0 | }; |
66 | | |
67 | | private: |
68 | | std::unique_ptr<DirectExpressionStep> expression_; |
69 | | }; |
70 | | |
71 | | } // namespace google::api::expr::runtime |
72 | | |
73 | | #endif // THIRD_PARTY_CEL_CPP_EVAL_EVAL_TRACE_STEP_H_ |