-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcel_value.cc
More file actions
344 lines (300 loc) · 11.8 KB
/
Copy pathcel_value.cc
File metadata and controls
344 lines (300 loc) · 11.8 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "eval/public/cel_value.h"
#include <cstdint>
#include <string>
#include <vector>
#include "google/protobuf/arena.h"
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/string_view.h"
#include "base/memory.h"
#include "eval/internal/errors.h"
#include "eval/public/cel_value_internal.h"
#include "eval/public/structs/legacy_type_info_apis.h"
#include "extensions/protobuf/memory_manager.h"
namespace google::api::expr::runtime {
namespace {
using ::google::protobuf::Arena;
namespace interop = ::cel::interop_internal;
constexpr absl::string_view kNullTypeName = "null_type";
constexpr absl::string_view kBoolTypeName = "bool";
constexpr absl::string_view kInt64TypeName = "int";
constexpr absl::string_view kUInt64TypeName = "uint";
constexpr absl::string_view kDoubleTypeName = "double";
constexpr absl::string_view kStringTypeName = "string";
constexpr absl::string_view kBytesTypeName = "bytes";
constexpr absl::string_view kDurationTypeName = "google.protobuf.Duration";
constexpr absl::string_view kTimestampTypeName = "google.protobuf.Timestamp";
// Leading "." to prevent potential namespace clash.
constexpr absl::string_view kListTypeName = "list";
constexpr absl::string_view kMapTypeName = "map";
constexpr absl::string_view kCelTypeTypeName = "type";
struct DebugStringVisitor {
google::protobuf::Arena* const arena;
std::string operator()(bool arg) { return absl::StrFormat("%d", arg); }
std::string operator()(int64_t arg) { return absl::StrFormat("%lld", arg); }
std::string operator()(uint64_t arg) { return absl::StrFormat("%llu", arg); }
std::string operator()(double arg) { return absl::StrFormat("%f", arg); }
std::string operator()(CelValue::NullType) { return "null"; }
std::string operator()(CelValue::StringHolder arg) {
return absl::StrFormat("%s", arg.value());
}
std::string operator()(CelValue::BytesHolder arg) {
return absl::StrFormat("%s", arg.value());
}
std::string operator()(const MessageWrapper& arg) {
return arg.message_ptr() == nullptr
? "NULL"
: arg.legacy_type_info()->DebugString(arg);
}
std::string operator()(absl::Duration arg) {
return absl::FormatDuration(arg);
}
std::string operator()(absl::Time arg) {
return absl::FormatTime(arg, absl::UTCTimeZone());
}
std::string operator()(const CelList* arg) {
std::vector<std::string> elements;
elements.reserve(arg->size());
for (int i = 0; i < arg->size(); i++) {
elements.push_back(arg->Get(arena, i).DebugString());
}
return absl::StrCat("[", absl::StrJoin(elements, ", "), "]");
}
std::string operator()(const CelMap* arg) {
const CelList* keys = arg->ListKeys(arena).value();
std::vector<std::string> elements;
elements.reserve(keys->size());
for (int i = 0; i < keys->size(); i++) {
const auto& key = (*keys).Get(arena, i);
const auto& optional_value = arg->Get(arena, key);
elements.push_back(absl::StrCat("<", key.DebugString(), ">: <",
optional_value.has_value()
? optional_value->DebugString()
: "nullopt",
">"));
}
return absl::StrCat("{", absl::StrJoin(elements, ", "), "}");
}
std::string operator()(const UnknownSet* arg) {
return "?"; // Not implemented.
}
std::string operator()(CelValue::CelTypeHolder arg) {
return absl::StrCat(arg.value());
}
std::string operator()(const CelError* arg) { return arg->ToString(); }
};
} // namespace
CelValue CelValue::CreateDuration(absl::Duration value) {
if (value >= interop::kDurationHigh || value <= interop::kDurationLow) {
return CelValue(interop::DurationOverflowError());
}
return CreateUncheckedDuration(value);
}
// TODO(issues/136): These don't match the CEL runtime typenames. They should
// be updated where possible for consistency.
std::string CelValue::TypeName(Type value_type) {
switch (value_type) {
case Type::kNullType:
return "null_type";
case Type::kBool:
return "bool";
case Type::kInt64:
return "int64";
case Type::kUint64:
return "uint64";
case Type::kDouble:
return "double";
case Type::kString:
return "string";
case Type::kBytes:
return "bytes";
case Type::kMessage:
return "Message";
case Type::kDuration:
return "Duration";
case Type::kTimestamp:
return "Timestamp";
case Type::kList:
return "CelList";
case Type::kMap:
return "CelMap";
case Type::kCelType:
return "CelType";
case Type::kUnknownSet:
return "UnknownSet";
case Type::kError:
return "CelError";
case Type::kAny:
return "Any type";
default:
return "unknown";
}
}
absl::Status CelValue::CheckMapKeyType(const CelValue& key) {
switch (key.type()) {
case CelValue::Type::kString:
case CelValue::Type::kInt64:
case CelValue::Type::kUint64:
case CelValue::Type::kBool:
return absl::OkStatus();
default:
return absl::InvalidArgumentError(absl::StrCat(
"Invalid map key type: '", CelValue::TypeName(key.type()), "'"));
}
}
CelValue CelValue::ObtainCelType() const {
switch (type()) {
case Type::kNullType:
return CreateCelType(CelTypeHolder(kNullTypeName));
case Type::kBool:
return CreateCelType(CelTypeHolder(kBoolTypeName));
case Type::kInt64:
return CreateCelType(CelTypeHolder(kInt64TypeName));
case Type::kUint64:
return CreateCelType(CelTypeHolder(kUInt64TypeName));
case Type::kDouble:
return CreateCelType(CelTypeHolder(kDoubleTypeName));
case Type::kString:
return CreateCelType(CelTypeHolder(kStringTypeName));
case Type::kBytes:
return CreateCelType(CelTypeHolder(kBytesTypeName));
case Type::kMessage: {
MessageWrapper wrapper;
CelValue::GetValue(&wrapper);
if (wrapper.message_ptr() == nullptr) {
return CreateCelType(CelTypeHolder(kNullTypeName));
}
// Descritptor::full_name() returns const reference, so using pointer
// should be safe.
return CreateCelType(
CelTypeHolder(wrapper.legacy_type_info()->GetTypename(wrapper)));
}
case Type::kDuration:
return CreateCelType(CelTypeHolder(kDurationTypeName));
case Type::kTimestamp:
return CreateCelType(CelTypeHolder(kTimestampTypeName));
case Type::kList:
return CreateCelType(CelTypeHolder(kListTypeName));
case Type::kMap:
return CreateCelType(CelTypeHolder(kMapTypeName));
case Type::kCelType:
return CreateCelType(CelTypeHolder(kCelTypeTypeName));
case Type::kUnknownSet:
return *this;
case Type::kError:
return *this;
default: {
static const CelError* invalid_type_error =
new CelError(absl::InvalidArgumentError("Unsupported CelValue type"));
return CreateError(invalid_type_error);
}
}
}
// Returns debug string describing a value
const std::string CelValue::DebugString() const {
google::protobuf::Arena arena;
return absl::StrCat(CelValue::TypeName(type()), ": ",
InternalVisit<std::string>(DebugStringVisitor{&arena}));
}
CelValue CreateErrorValue(cel::MemoryManager& manager,
absl::string_view message,
absl::StatusCode error_code) {
// TODO(issues/5): assume arena-style allocator while migrating to new
// value type.
Arena* arena = cel::extensions::ProtoMemoryManager::CastToProtoArena(manager);
return CreateErrorValue(arena, message, error_code);
}
CelValue CreateErrorValue(cel::MemoryManager& manager,
const absl::Status& status) {
// TODO(issues/5): assume arena-style allocator while migrating to new
// value type.
Arena* arena = cel::extensions::ProtoMemoryManager::CastToProtoArena(manager);
return CreateErrorValue(arena, status);
}
CelValue CreateErrorValue(Arena* arena, absl::string_view message,
absl::StatusCode error_code) {
CelError* error = Arena::Create<CelError>(arena, error_code, message);
return CelValue::CreateError(error);
}
CelValue CreateErrorValue(Arena* arena, const absl::Status& status) {
CelError* error = Arena::Create<CelError>(arena, status);
return CelValue::CreateError(error);
}
CelValue CreateNoMatchingOverloadError(cel::MemoryManager& manager,
absl::string_view fn) {
return CelValue::CreateError(
interop::CreateNoMatchingOverloadError(manager, fn));
}
CelValue CreateNoMatchingOverloadError(google::protobuf::Arena* arena,
absl::string_view fn) {
return CelValue::CreateError(
interop::CreateNoMatchingOverloadError(arena, fn));
}
bool CheckNoMatchingOverloadError(CelValue value) {
return value.IsError() &&
value.ErrorOrDie()->code() == absl::StatusCode::kUnknown &&
absl::StrContains(value.ErrorOrDie()->message(),
interop::kErrNoMatchingOverload);
}
CelValue CreateNoSuchFieldError(cel::MemoryManager& manager,
absl::string_view field) {
return CelValue::CreateError(interop::CreateNoSuchFieldError(manager, field));
}
CelValue CreateNoSuchFieldError(google::protobuf::Arena* arena, absl::string_view field) {
return CelValue::CreateError(interop::CreateNoSuchFieldError(arena, field));
}
CelValue CreateNoSuchKeyError(cel::MemoryManager& manager,
absl::string_view key) {
return CelValue::CreateError(interop::CreateNoSuchKeyError(manager, key));
}
CelValue CreateNoSuchKeyError(google::protobuf::Arena* arena, absl::string_view key) {
return CelValue::CreateError(interop::CreateNoSuchKeyError(arena, key));
}
bool CheckNoSuchKeyError(CelValue value) {
return value.IsError() && absl::StartsWith(value.ErrorOrDie()->message(),
interop::kErrNoSuchKey);
}
CelValue CreateMissingAttributeError(google::protobuf::Arena* arena,
absl::string_view missing_attribute_path) {
return CelValue::CreateError(
interop::CreateMissingAttributeError(arena, missing_attribute_path));
}
CelValue CreateMissingAttributeError(cel::MemoryManager& manager,
absl::string_view missing_attribute_path) {
// TODO(issues/5): assume arena-style allocator while migrating
// to new value type.
return CelValue::CreateError(
interop::CreateMissingAttributeError(manager, missing_attribute_path));
}
bool IsMissingAttributeError(const CelValue& value) {
const CelError* error;
if (!value.GetValue(&error)) return false;
if (error && error->code() == absl::StatusCode::kInvalidArgument) {
auto path = error->GetPayload(interop::kPayloadUrlMissingAttributePath);
return path.has_value();
}
return false;
}
CelValue CreateUnknownFunctionResultError(cel::MemoryManager& manager,
absl::string_view help_message) {
return CelValue::CreateError(
interop::CreateUnknownFunctionResultError(manager, help_message));
}
CelValue CreateUnknownFunctionResultError(google::protobuf::Arena* arena,
absl::string_view help_message) {
return CelValue::CreateError(
interop::CreateUnknownFunctionResultError(arena, help_message));
}
bool IsUnknownFunctionResult(const CelValue& value) {
const CelError* error;
if (!value.GetValue(&error)) return false;
if (error == nullptr || error->code() != absl::StatusCode::kUnavailable) {
return false;
}
auto payload = error->GetPayload(interop::kPayloadUrlUnknownFunctionResult);
return payload.has_value() && payload.value() == "true";
}
} // namespace google::api::expr::runtime