forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshadowable_value_step.cc
More file actions
98 lines (81 loc) · 3.17 KB
/
Copy pathshadowable_value_step.cc
File metadata and controls
98 lines (81 loc) · 3.17 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
#include "eval/eval/shadowable_value_step.h"
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "common/value.h"
#include "eval/eval/attribute_trail.h"
#include "eval/eval/direct_expression_step.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/expression_step_base.h"
#include "internal/status_macros.h"
namespace google::api::expr::runtime {
namespace {
using ::cel::Value;
class ShadowableValueStep : public ExpressionStepBase {
public:
ShadowableValueStep(std::string identifier, cel::Value value, int64_t expr_id)
: ExpressionStepBase(expr_id),
identifier_(std::move(identifier)),
value_(std::move(value)) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
std::string identifier_;
Value value_;
};
absl::Status ShadowableValueStep::Evaluate(ExecutionFrame* frame) const {
cel::Value result;
CEL_ASSIGN_OR_RETURN(auto found,
frame->modern_activation().FindVariable(
identifier_, frame->descriptor_pool(),
frame->message_factory(), frame->arena(), &result));
if (found) {
frame->value_stack().Push(std::move(result));
} else {
frame->value_stack().Push(value_);
}
return absl::OkStatus();
}
class DirectShadowableValueStep : public DirectExpressionStep {
public:
DirectShadowableValueStep(std::string identifier, cel::Value value,
int64_t expr_id)
: DirectExpressionStep(expr_id),
identifier_(std::move(identifier)),
value_(std::move(value)) {}
absl::Status Evaluate(ExecutionFrameBase& frame, Value& result,
AttributeTrail& attribute) const override;
private:
std::string identifier_;
Value value_;
};
// TODO(uncreated-issue/67): Attribute tracking is skipped for the shadowed case. May
// cause problems for users with unknown tracking and variables named like
// 'list' etc, but follows the current behavior of the stack machine version.
absl::Status DirectShadowableValueStep::Evaluate(
ExecutionFrameBase& frame, Value& result, AttributeTrail& attribute) const {
CEL_ASSIGN_OR_RETURN(auto found,
frame.activation().FindVariable(
identifier_, frame.descriptor_pool(),
frame.message_factory(), frame.arena(), &result));
if (!found) {
result = value_;
}
return absl::OkStatus();
}
} // namespace
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateShadowableValueStep(
absl::string_view name, cel::Value value, int64_t expr_id) {
return absl::make_unique<ShadowableValueStep>(std::string(name),
std::move(value), expr_id);
}
std::unique_ptr<DirectExpressionStep> CreateDirectShadowableValueStep(
absl::string_view name, cel::Value value, int64_t expr_id) {
return std::make_unique<DirectShadowableValueStep>(std::string(name),
std::move(value), expr_id);
}
} // namespace google::api::expr::runtime