forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathident_step.cc
More file actions
113 lines (92 loc) · 3.26 KB
/
Copy pathident_step.cc
File metadata and controls
113 lines (92 loc) · 3.26 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
#include "eval/eval/ident_step.h"
#include "google/protobuf/arena.h"
#include "absl/status/statusor.h"
#include "absl/strings/substitute.h"
#include "eval/eval/attribute_trail.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/expression_step_base.h"
#include "eval/public/unknown_attribute_set.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
namespace {
class IdentStep : public ExpressionStepBase {
public:
IdentStep(absl::string_view name, int64_t expr_id)
: ExpressionStepBase(expr_id), name_(name) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
void DoEvaluate(ExecutionFrame* frame, CelValue* result,
AttributeTrail* trail) const;
std::string name_;
};
void IdentStep::DoEvaluate(ExecutionFrame* frame, CelValue* result,
AttributeTrail* trail) const {
// Special case - iterator looked up in
if (frame->GetIterVar(name_, result)) {
const AttributeTrail* iter_trail;
if (frame->GetIterAttr(name_, &iter_trail)) {
*trail = *iter_trail;
}
return;
}
auto value = frame->activation().FindValue(name_, frame->arena());
// Populate trails if either MissingAttributeError or UnknownPattern
// is enabled.
if (frame->enable_missing_attribute_errors() || frame->enable_unknowns()) {
google::api::expr::v1alpha1::Expr expr;
expr.mutable_ident_expr()->set_name(name_);
*trail = AttributeTrail(expr, frame->arena());
}
if (frame->enable_missing_attribute_errors() && !name_.empty() &&
frame->attribute_utility().CheckForMissingAttribute(*trail)) {
*result = CreateMissingAttributeError(frame->arena(), name_);
return;
}
{
// We handle masked unknown paths for the sake of uniformity, although it is
// better not to bind unknown values to activation in first place.
// TODO(issues/41) Deprecate this style of unknowns handling after
// Unknowns are properly supported.
bool unknown_value = frame->activation().IsPathUnknown(name_);
if (unknown_value) {
*result = CreateUnknownValueError(frame->arena(), name_);
return;
}
}
if (frame->enable_unknowns()) {
if (frame->attribute_utility().CheckForUnknown(*trail, false)) {
auto unknown_set = google::protobuf::Arena::Create<UnknownSet>(
frame->arena(), UnknownAttributeSet({trail->attribute()}));
*result = CelValue::CreateUnknownSet(unknown_set);
return;
}
}
if (value.has_value()) {
*result = value.value();
} else {
*result = CreateErrorValue(
frame->arena(),
absl::Substitute("No value with name \"$0\" found in Activation",
name_));
}
}
absl::Status IdentStep::Evaluate(ExecutionFrame* frame) const {
CelValue result;
AttributeTrail trail;
DoEvaluate(frame, &result, &trail);
frame->value_stack().Push(result, trail);
return absl::OkStatus();
}
} // namespace
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateIdentStep(
const google::api::expr::v1alpha1::Expr::Ident* ident_expr, int64_t expr_id) {
std::unique_ptr<ExpressionStep> step =
absl::make_unique<IdentStep>(ident_expr->name(), expr_id);
return std::move(step);
}
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google