-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcontainer_access_step.cc
More file actions
300 lines (267 loc) · 10.3 KB
/
Copy pathcontainer_access_step.cc
File metadata and controls
300 lines (267 loc) · 10.3 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "eval/eval/container_access_step.h"
#include <cstdint>
#include <memory>
#include <utility>
#include "google/protobuf/arena.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/types/span.h"
#include "base/attribute.h"
#include "base/kind.h"
#include "base/memory.h"
#include "base/value.h"
#include "base/values/bool_value.h"
#include "base/values/double_value.h"
#include "base/values/int_value.h"
#include "base/values/list_value.h"
#include "base/values/string_value.h"
#include "base/values/uint_value.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/expression_step_base.h"
#include "eval/internal/errors.h"
#include "eval/internal/interop.h"
#include "eval/public/cel_number.h"
#include "eval/public/cel_value.h"
#include "extensions/protobuf/memory_manager.h"
#include "internal/status_macros.h"
namespace google::api::expr::runtime {
namespace {
using ::cel::AttributeQualifier;
using ::cel::BoolValue;
using ::cel::DoubleValue;
using ::cel::Handle;
using ::cel::IntValue;
using ::cel::Kind;
using ::cel::KindToString;
using ::cel::ListValue;
using ::cel::MapValue;
using ::cel::StringValue;
using ::cel::UintValue;
using ::cel::Value;
using ::cel::extensions::ProtoMemoryManager;
using ::cel::interop_internal::CreateErrorValueFromView;
using ::cel::interop_internal::CreateIntValue;
using ::cel::interop_internal::CreateNoSuchKeyError;
using ::cel::interop_internal::CreateUintValue;
using ::cel::interop_internal::CreateUnknownValueFromView;
using ::google::protobuf::Arena;
inline constexpr int kNumContainerAccessArguments = 2;
// ContainerAccessStep performs message field access specified by Expr::Select
// message.
class ContainerAccessStep : public ExpressionStepBase {
public:
explicit ContainerAccessStep(int64_t expr_id) : ExpressionStepBase(expr_id) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
struct LookupResult {
Handle<Value> value;
AttributeTrail trail;
};
LookupResult PerformLookup(ExecutionFrame* frame) const;
absl::StatusOr<Handle<Value>> LookupInMap(const Handle<MapValue>& cel_map,
const Handle<Value>& key,
ExecutionFrame* frame) const;
absl::StatusOr<Handle<Value>> LookupInList(const Handle<ListValue>& cel_list,
const Handle<Value>& key,
ExecutionFrame* frame) const;
};
absl::optional<CelNumber> CelNumberFromValue(const Handle<Value>& value) {
switch (value->kind()) {
case Kind::kInt64:
return CelNumber::FromInt64(value.As<IntValue>()->value());
case Kind::kUint64:
return CelNumber::FromUint64(value.As<UintValue>()->value());
case Kind::kDouble:
return CelNumber::FromDouble(value.As<DoubleValue>()->value());
default:
return absl::nullopt;
}
}
absl::Status CheckMapKeyType(const Handle<Value>& key) {
Kind kind = key->kind();
switch (kind) {
case Kind::kString:
case Kind::kInt64:
case Kind::kUint64:
case Kind::kBool:
return absl::OkStatus();
default:
return absl::InvalidArgumentError(
absl::StrCat("Invalid map key type: '", KindToString(kind), "'"));
}
}
AttributeQualifier AttributeQualifierFromValue(const Handle<Value>& v) {
switch (v->kind()) {
case Kind::kString:
return AttributeQualifier::OfString(v.As<StringValue>()->ToString());
case Kind::kInt64:
return AttributeQualifier::OfInt(v.As<IntValue>()->value());
case Kind::kUint64:
return AttributeQualifier::OfUint(v.As<UintValue>()->value());
case Kind::kBool:
return AttributeQualifier::OfBool(v.As<BoolValue>()->value());
default:
// Non-matching qualifier.
return AttributeQualifier();
}
}
absl::StatusOr<Handle<Value>> ContainerAccessStep::LookupInMap(
const Handle<MapValue>& cel_map, const Handle<Value>& key,
ExecutionFrame* frame) const {
if (frame->enable_heterogeneous_numeric_lookups()) {
// Double isn't a supported key type but may be convertible to an integer.
absl::optional<CelNumber> number = CelNumberFromValue(key);
if (number.has_value()) {
// Consider uint as uint first then try coercion (prefer matching the
// original type of the key value).
if (key->Is<UintValue>()) {
CEL_ASSIGN_OR_RETURN(
auto maybe_value,
cel_map->Get(MapValue::GetContext(frame->value_factory()), key));
if (maybe_value.has_value()) {
return std::move(maybe_value).value();
}
}
// double / int / uint -> int
if (number->LosslessConvertibleToInt()) {
CEL_ASSIGN_OR_RETURN(
auto maybe_value,
cel_map->Get(MapValue::GetContext(frame->value_factory()),
CreateIntValue(number->AsInt())));
if (maybe_value.has_value()) {
return std::move(maybe_value).value();
}
}
// double / int -> uint
if (number->LosslessConvertibleToUint()) {
CEL_ASSIGN_OR_RETURN(
auto maybe_value,
cel_map->Get(MapValue::GetContext(frame->value_factory()),
CreateUintValue(number->AsUint())));
if (maybe_value.has_value()) {
return std::move(maybe_value).value();
}
}
return CreateErrorValueFromView(
CreateNoSuchKeyError(frame->memory_manager(), key->DebugString()));
}
}
CEL_RETURN_IF_ERROR(CheckMapKeyType(key));
CEL_ASSIGN_OR_RETURN(
auto maybe_value,
cel_map->Get(MapValue::GetContext(frame->value_factory()), key));
if (maybe_value.has_value()) {
return std::move(maybe_value).value();
}
return CreateErrorValueFromView(
CreateNoSuchKeyError(frame->memory_manager(), key->DebugString()));
}
absl::StatusOr<Handle<Value>> ContainerAccessStep::LookupInList(
const Handle<ListValue>& cel_list, const Handle<Value>& key,
ExecutionFrame* frame) const {
absl::optional<int64_t> maybe_idx;
if (frame->enable_heterogeneous_numeric_lookups()) {
auto number = CelNumberFromValue(key);
if (number.has_value() && number->LosslessConvertibleToInt()) {
maybe_idx = number->AsInt();
}
} else if (key->Is<IntValue>()) {
maybe_idx = key.As<IntValue>()->value();
}
if (maybe_idx.has_value()) {
int64_t idx = *maybe_idx;
if (idx < 0 || idx >= cel_list->size()) {
return absl::UnknownError(
absl::StrCat("Index error: index=", idx, " size=", cel_list->size()));
}
return cel_list->Get(ListValue::GetContext(frame->value_factory()), idx);
}
return absl::UnknownError(
absl::StrCat("Index error: expected integer type, got ",
CelValue::TypeName(key->kind())));
}
ContainerAccessStep::LookupResult ContainerAccessStep::PerformLookup(
ExecutionFrame* frame) const {
google::protobuf::Arena* arena =
ProtoMemoryManager::CastToProtoArena(frame->memory_manager());
auto input_args = frame->value_stack().GetSpan(kNumContainerAccessArguments);
AttributeTrail trail;
const Handle<Value> container = input_args[0];
const Handle<Value> key = input_args[1];
if (frame->enable_unknowns()) {
auto unknown_set =
frame->attribute_utility().MergeUnknowns(input_args, nullptr);
if (unknown_set) {
return {CreateUnknownValueFromView(unknown_set), std::move(trail)};
}
// We guarantee that GetAttributeSpan can aquire this number of arguments
// by calling HasEnough() at the beginning of Execute() method.
absl::Span<const AttributeTrail> input_attrs =
frame->value_stack().GetAttributeSpan(kNumContainerAccessArguments);
const auto& container_trail = input_attrs[0];
trail = container_trail.Step(AttributeQualifierFromValue(key),
frame->memory_manager());
if (frame->attribute_utility().CheckForUnknown(trail,
/*use_partial=*/false)) {
auto unknown_set =
frame->attribute_utility().CreateUnknownSet(trail.attribute());
return {CreateUnknownValueFromView(unknown_set), std::move(trail)};
}
}
for (const auto& value : input_args) {
if (value->Is<cel::ErrorValue>()) {
return {value, std::move(trail)};
}
}
// Select steps can be applied to either maps or messages
switch (container->kind()) {
case Kind::kMap: {
auto result = LookupInMap(container.As<MapValue>(), key, frame);
if (!result.ok()) {
return {CreateErrorValueFromView(Arena::Create<absl::Status>(
arena, std::move(result).status())),
std::move(trail)};
}
return {std::move(result).value(), std::move(trail)};
}
case CelValue::Type::kList: {
auto result = LookupInList(container.As<ListValue>(), key, frame);
if (!result.ok()) {
return {CreateErrorValueFromView(Arena::Create<absl::Status>(
arena, std::move(result).status())),
std::move(trail)};
}
return {std::move(result).value(), std::move(trail)};
}
default:
return {CreateErrorValueFromView(Arena::Create<absl::Status>(
arena, absl::StatusCode::kInvalidArgument,
absl::StrCat("Invalid container type: '",
KindToString(container->kind()), "'"))),
std::move(trail)};
}
}
absl::Status ContainerAccessStep::Evaluate(ExecutionFrame* frame) const {
if (!frame->value_stack().HasEnough(kNumContainerAccessArguments)) {
return absl::Status(
absl::StatusCode::kInternal,
"Insufficient arguments supplied for ContainerAccess-type expression");
}
auto result = PerformLookup(frame);
frame->value_stack().Pop(kNumContainerAccessArguments);
frame->value_stack().Push(std::move(result.value), std::move(result.trail));
return absl::OkStatus();
}
} // namespace
// Factory method for Select - based Execution step
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateContainerAccessStep(
const cel::ast::internal::Call& call, int64_t expr_id) {
int arg_count = call.args().size() + (call.has_target() ? 1 : 0);
if (arg_count != kNumContainerAccessArguments) {
return absl::InvalidArgumentError(absl::StrCat(
"Invalid argument count for index operation: ", arg_count));
}
return std::make_unique<ContainerAccessStep>(expr_id);
}
} // namespace google::api::expr::runtime