forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_access_step.cc
More file actions
167 lines (143 loc) · 5.49 KB
/
Copy pathcontainer_access_step.cc
File metadata and controls
167 lines (143 loc) · 5.49 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "eval/eval/container_access_step.h"
#include "google/protobuf/arena.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/expression_step_base.h"
#include "eval/public/cel_value.h"
#include "eval/public/unknown_attribute_set.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
namespace {
constexpr int NUM_CONTAINER_ACCESS_ARGUMENTS = 2;
// ContainerAccessStep performs message field access specified by Expr::Select
// message.
class ContainerAccessStep : public ExpressionStepBase {
public:
ContainerAccessStep(int64_t expr_id) : ExpressionStepBase(expr_id) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
using ValueAttributePair = std::pair<CelValue, AttributeTrail>;
ValueAttributePair PerformLookup(ExecutionFrame* frame) const;
CelValue LookupInMap(const CelMap* cel_map, const CelValue& key,
google::protobuf::Arena* arena) const;
CelValue LookupInList(const CelList* cel_list, const CelValue& key,
google::protobuf::Arena* arena) const;
};
inline CelValue ContainerAccessStep::LookupInMap(const CelMap* cel_map,
const CelValue& key,
google::protobuf::Arena* arena) const {
switch (key.type()) {
case CelValue::Type::kBool:
case CelValue::Type::kInt64:
case CelValue::Type::kUint64:
case CelValue::Type::kString: {
absl::optional<CelValue> maybe_value = (*cel_map)[key];
if (maybe_value.has_value()) {
return maybe_value.value();
}
break;
}
default: {
break;
}
}
return CreateNoSuchKeyError(arena, absl::StrCat("Key not found in map"));
}
inline CelValue ContainerAccessStep::LookupInList(const CelList* cel_list,
const CelValue& key,
google::protobuf::Arena* arena) const {
switch (key.type()) {
case CelValue::Type::kInt64: {
int64_t idx = key.Int64OrDie();
if (idx < 0 || idx >= cel_list->size()) {
return CreateErrorValue(arena,
absl::StrCat("Index error: index=", idx,
" size=", cel_list->size()));
}
return (*cel_list)[idx];
}
default: {
return CreateErrorValue(
arena, absl::StrCat("Index error: expected integer type, got ",
CelValue::TypeName(key.type())));
}
}
}
ContainerAccessStep::ValueAttributePair ContainerAccessStep::PerformLookup(
ExecutionFrame* frame) const {
auto input_args =
frame->value_stack().GetSpan(NUM_CONTAINER_ACCESS_ARGUMENTS);
AttributeTrail trail;
const CelValue& container = input_args[0];
const CelValue& key = input_args[1];
if (frame->enable_unknowns()) {
auto unknown_set =
frame->attribute_utility().MergeUnknowns(input_args, nullptr);
if (unknown_set) {
return {CelValue::CreateUnknownSet(unknown_set), trail};
}
// We guarantee that GetAttributeSpan can aquire this number of arguments
// by calling HasEnough() at the beginning of Execute() method.
auto input_attrs =
frame->value_stack().GetAttributeSpan(NUM_CONTAINER_ACCESS_ARGUMENTS);
auto container_trail = input_attrs[0];
trail = container_trail.Step(CelAttributeQualifier::Create(key),
frame->arena());
if (frame->attribute_utility().CheckForUnknown(trail,
/*use_partial=*/false)) {
auto unknown_set = google::protobuf::Arena::Create<UnknownSet>(
frame->arena(), UnknownAttributeSet({trail.attribute()}));
return {CelValue::CreateUnknownSet(unknown_set), trail};
}
}
for (const auto& value : input_args) {
if (value.IsError()) {
return {value, trail};
}
}
// Select steps can be applied to either maps or messages
switch (container.type()) {
case CelValue::Type::kMap: {
const CelMap* cel_map = container.MapOrDie();
return {LookupInMap(cel_map, key, frame->arena()), trail};
}
case CelValue::Type::kList: {
const CelList* cel_list = container.ListOrDie();
return {LookupInList(cel_list, key, frame->arena()), trail};
}
default: {
auto error = CreateErrorValue(
frame->arena(),
absl::StrCat("Unexpected container type for [] operation: ",
CelValue::TypeName(key.type())));
return {error, trail};
}
}
}
absl::Status ContainerAccessStep::Evaluate(ExecutionFrame* frame) const {
if (!frame->value_stack().HasEnough(NUM_CONTAINER_ACCESS_ARGUMENTS)) {
return absl::Status(
absl::StatusCode::kInternal,
"Insufficient arguments supplied for ContainerAccess-type expression");
}
auto result = PerformLookup(frame);
frame->value_stack().Pop(NUM_CONTAINER_ACCESS_ARGUMENTS);
frame->value_stack().Push(result.first, result.second);
return absl::OkStatus();
}
} // namespace
// Factory method for Select - based Execution step
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateContainerAccessStep(
const google::api::expr::v1alpha1::Expr::Call*, int64_t expr_id) {
std::unique_ptr<ExpressionStep> step =
absl::make_unique<ContainerAccessStep>(expr_id);
return std::move(step);
}
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google