-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcustom_map_value.cc
More file actions
711 lines (635 loc) · 24.9 KB
/
custom_map_value.cc
File metadata and controls
711 lines (635 loc) · 24.9 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstddef>
#include <memory>
#include <string>
#include "absl/base/attributes.h"
#include "absl/base/no_destructor.h"
#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "common/native_type.h"
#include "common/value.h"
#include "common/value_kind.h"
#include "common/values/list_value_builder.h"
#include "common/values/map_value_builder.h"
#include "common/values/values.h"
#include "eval/public/cel_value.h"
#include "internal/status_macros.h"
#include "internal/well_known_types.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel {
namespace {
using ::cel::well_known_types::StructReflection;
using ::cel::well_known_types::ValueReflection;
using ::google::api::expr::runtime::CelList;
using ::google::api::expr::runtime::CelValue;
absl::Status NoSuchKeyError(const Value& key) {
return absl::NotFoundError(
absl::StrCat("Key not found in map : ", key.DebugString()));
}
absl::Status InvalidMapKeyTypeError(ValueKind kind) {
return absl::InvalidArgumentError(
absl::StrCat("Invalid map key type: '", ValueKindToString(kind), "'"));
}
class EmptyMapValue final : public common_internal::CompatMapValue {
public:
static const EmptyMapValue& Get() {
static const absl::NoDestructor<EmptyMapValue> empty;
return *empty;
}
EmptyMapValue() = default;
std::string DebugString() const override { return "{}"; }
bool IsEmpty() const override { return true; }
size_t Size() const override { return 0; }
absl::Status ListKeys(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena,
absl::Nonnull<ListValue*> result) const override {
*result = ListValue();
return absl::OkStatus();
}
absl::StatusOr<absl::Nonnull<ValueIteratorPtr>> NewIterator() const override {
return NewEmptyValueIterator();
}
absl::Status ConvertToJson(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const override {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(json != nullptr);
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
ValueReflection value_reflection;
CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
value_reflection.MutableListValue(json)->Clear();
return absl::OkStatus();
}
absl::Status ConvertToJsonObject(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const override {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(json != nullptr);
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_STRUCT);
json->Clear();
return absl::OkStatus();
}
CustomMapValue Clone(absl::Nonnull<google::protobuf::Arena*>) const override {
return CustomMapValue();
}
absl::optional<CelValue> operator[](CelValue key) const override {
return absl::nullopt;
}
using CompatMapValue::Get;
absl::optional<CelValue> Get(google::protobuf::Arena* arena,
CelValue key) const override {
return absl::nullopt;
}
using CompatMapValue::Has;
absl::StatusOr<bool> Has(const CelValue& key) const override { return false; }
int size() const override { return static_cast<int>(Size()); }
absl::StatusOr<const CelList*> ListKeys() const override {
return common_internal::EmptyCompatListValue();
}
absl::StatusOr<const CelList*> ListKeys(google::protobuf::Arena*) const override {
return ListKeys();
}
private:
absl::StatusOr<bool> FindImpl(
const Value& key,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena,
absl::Nonnull<Value*> result) const override {
return false;
}
absl::StatusOr<bool> HasImpl(
const Value& key,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena) const override {
return false;
}
};
} // namespace
namespace common_internal {
absl::Nonnull<const CompatMapValue*> EmptyCompatMapValue() {
return &EmptyMapValue::Get();
}
} // namespace common_internal
namespace {
class CustomMapValueInterfaceKeysIterator final : public ValueIterator {
public:
explicit CustomMapValueInterfaceKeysIterator(
absl::Nonnull<const CustomMapValueInterface*> interface)
: interface_(interface) {}
bool HasNext() override {
if (keys_iterator_ == nullptr) {
return !interface_->IsEmpty();
}
return keys_iterator_->HasNext();
}
absl::Status Next(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena,
absl::Nonnull<Value*> result) override {
if (keys_iterator_ == nullptr) {
if (interface_->IsEmpty()) {
return absl::FailedPreconditionError(
"ValueIterator::Next() called when "
"ValueIterator::HasNext() returns false");
}
CEL_RETURN_IF_ERROR(interface_->ListKeys(descriptor_pool, message_factory,
arena, &keys_));
CEL_ASSIGN_OR_RETURN(keys_iterator_, keys_.NewIterator());
ABSL_CHECK(keys_iterator_->HasNext()); // Crash OK
}
return keys_iterator_->Next(descriptor_pool, message_factory, arena,
result);
}
private:
absl::Nonnull<const CustomMapValueInterface*> const interface_;
ListValue keys_;
absl::Nullable<ValueIteratorPtr> keys_iterator_;
};
class CustomMapValueDispatcherKeysIterator final : public ValueIterator {
public:
explicit CustomMapValueDispatcherKeysIterator(
absl::Nonnull<const CustomMapValueDispatcher*> dispatcher,
CustomMapValueContent content)
: dispatcher_(dispatcher), content_(content) {}
bool HasNext() override {
if (keys_iterator_ == nullptr) {
if (dispatcher_->is_empty != nullptr) {
return !dispatcher_->is_empty(dispatcher_, content_);
}
return dispatcher_->size(dispatcher_, content_) != 0;
}
return keys_iterator_->HasNext();
}
absl::Status Next(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena,
absl::Nonnull<Value*> result) override {
if (keys_iterator_ == nullptr) {
if (dispatcher_->is_empty != nullptr
? dispatcher_->is_empty(dispatcher_, content_)
: dispatcher_->size(dispatcher_, content_) == 0) {
return absl::FailedPreconditionError(
"ValueIterator::Next() called when "
"ValueIterator::HasNext() returns false");
}
CEL_RETURN_IF_ERROR(
dispatcher_->list_keys(dispatcher_, content_, descriptor_pool,
message_factory, arena, &keys_));
CEL_ASSIGN_OR_RETURN(keys_iterator_, keys_.NewIterator());
ABSL_CHECK(keys_iterator_->HasNext()); // Crash OK
}
return keys_iterator_->Next(descriptor_pool, message_factory, arena,
result);
}
private:
absl::Nonnull<const CustomMapValueDispatcher*> const dispatcher_;
const CustomMapValueContent content_;
ListValue keys_;
absl::Nullable<ValueIteratorPtr> keys_iterator_;
};
} // namespace
absl::Status CustomMapValueInterface::SerializeTo(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<absl::Cord*> value) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(value != nullptr);
StructReflection reflection;
CEL_RETURN_IF_ERROR(reflection.Initialize(descriptor_pool));
const google::protobuf::Message* prototype =
message_factory->GetPrototype(reflection.GetDescriptor());
if (prototype == nullptr) {
return absl::UnknownError(
absl::StrCat("failed to get message prototype: ",
reflection.GetDescriptor()->full_name()));
}
google::protobuf::Arena arena;
google::protobuf::Message* message = prototype->New(&arena);
CEL_RETURN_IF_ERROR(
ConvertToJsonObject(descriptor_pool, message_factory, message));
if (!message->SerializePartialToString(value)) {
return absl::UnknownError(
"failed to serialize message: google.protobuf.Struct");
}
return absl::OkStatus();
}
absl::Status CustomMapValueInterface::ForEach(
ForEachCallback callback,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena) const {
CEL_ASSIGN_OR_RETURN(auto iterator, NewIterator());
while (iterator->HasNext()) {
Value key;
Value value;
CEL_RETURN_IF_ERROR(
iterator->Next(descriptor_pool, message_factory, arena, &key));
CEL_ASSIGN_OR_RETURN(bool found, FindImpl(key, descriptor_pool,
message_factory, arena, &value));
if (!found) {
value = ErrorValue(NoSuchKeyError(key));
}
CEL_ASSIGN_OR_RETURN(auto ok, callback(key, value));
if (!ok) {
break;
}
}
return absl::OkStatus();
}
absl::StatusOr<absl::Nonnull<ValueIteratorPtr>>
CustomMapValueInterface::NewIterator() const {
return std::make_unique<CustomMapValueInterfaceKeysIterator>(this);
}
absl::Status CustomMapValueInterface::Equal(
const Value& other,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
if (auto list_value = other.As<MapValue>(); list_value.has_value()) {
return MapValueEqual(*this, *list_value, descriptor_pool, message_factory,
arena, result);
}
*result = FalseValue();
return absl::OkStatus();
}
CustomMapValue::CustomMapValue() {
content_ = CustomMapValueContent::From(CustomMapValueInterface::Content{
.interface = &EmptyMapValue::Get(), .arena = nullptr});
}
NativeTypeId CustomMapValue::GetTypeId() const {
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return NativeTypeId::Of(*content.interface);
}
return dispatcher_->get_type_id(dispatcher_, content_);
}
absl::string_view CustomMapValue::GetTypeName() const { return "map"; }
std::string CustomMapValue::DebugString() const {
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->DebugString();
}
if (dispatcher_->debug_string != nullptr) {
return dispatcher_->debug_string(dispatcher_, content_);
}
return "map";
}
absl::Status CustomMapValue::SerializeTo(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<absl::Cord*> value) const {
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->SerializeTo(descriptor_pool, message_factory,
value);
}
if (dispatcher_->serialize_to != nullptr) {
return dispatcher_->serialize_to(dispatcher_, content_, descriptor_pool,
message_factory, value);
}
return absl::UnimplementedError(
absl::StrCat(GetTypeName(), " is unserializable"));
}
absl::Status CustomMapValue::ConvertToJson(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(json != nullptr);
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
ValueReflection value_reflection;
CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
google::protobuf::Message* json_object = value_reflection.MutableStructValue(json);
return ConvertToJsonObject(descriptor_pool, message_factory, json_object);
}
absl::Status CustomMapValue::ConvertToJsonObject(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(json != nullptr);
ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
google::protobuf::Descriptor::WELLKNOWNTYPE_STRUCT);
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->ConvertToJsonObject(descriptor_pool,
message_factory, json);
}
if (dispatcher_->convert_to_json_object != nullptr) {
return dispatcher_->convert_to_json_object(
dispatcher_, content_, descriptor_pool, message_factory, json);
}
return absl::UnimplementedError(
absl::StrCat(GetTypeName(), " is not convertable to JSON"));
}
absl::Status CustomMapValue::Equal(
const Value& other,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(arena != nullptr);
ABSL_DCHECK(result != nullptr);
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->Equal(other, descriptor_pool, message_factory,
arena, result);
}
if (auto other_map_value = other.AsMap(); other_map_value) {
if (dispatcher_->equal != nullptr) {
return dispatcher_->equal(dispatcher_, content_, *other_map_value,
descriptor_pool, message_factory, arena,
result);
}
return common_internal::MapValueEqual(*this, *other_map_value,
descriptor_pool, message_factory,
arena, result);
}
*result = FalseValue();
return absl::OkStatus();
}
bool CustomMapValue::IsZeroValue() const {
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->IsZeroValue();
}
return dispatcher_->is_zero_value(dispatcher_, content_);
}
CustomMapValue CustomMapValue::Clone(
absl::Nonnull<google::protobuf::Arena*> arena) const {
ABSL_DCHECK(arena != nullptr);
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
if (content.arena != arena) {
return content.interface->Clone(arena);
}
return *this;
}
return dispatcher_->clone(dispatcher_, content_, arena);
}
bool CustomMapValue::IsEmpty() const {
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->IsEmpty();
}
if (dispatcher_->is_empty != nullptr) {
return dispatcher_->is_empty(dispatcher_, content_);
}
return dispatcher_->size(dispatcher_, content_) == 0;
}
size_t CustomMapValue::Size() const {
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->Size();
}
return dispatcher_->size(dispatcher_, content_);
}
absl::Status CustomMapValue::Get(
const Value& key,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(arena != nullptr);
ABSL_DCHECK(result != nullptr);
CEL_ASSIGN_OR_RETURN(
bool ok, Find(key, descriptor_pool, message_factory, arena, result));
if (ABSL_PREDICT_FALSE(!ok)) {
switch (result->kind()) {
case ValueKind::kError:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kUnknown:
break;
default:
*result = ErrorValue(NoSuchKeyError(key));
break;
}
}
return absl::OkStatus();
}
absl::StatusOr<bool> CustomMapValue::Find(
const Value& key,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(arena != nullptr);
ABSL_DCHECK(result != nullptr);
switch (key.kind()) {
case ValueKind::kError:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kUnknown:
*result = key;
return false;
case ValueKind::kBool:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kInt:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kUint:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kString:
break;
default:
*result = ErrorValue(InvalidMapKeyTypeError(key.kind()));
return false;
}
bool ok;
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
CEL_ASSIGN_OR_RETURN(
ok, content.interface->FindImpl(key, descriptor_pool, message_factory,
arena, result));
} else {
CEL_ASSIGN_OR_RETURN(
ok, dispatcher_->find(dispatcher_, content_, key, descriptor_pool,
message_factory, arena, result));
}
if (ok) {
return true;
}
*result = NullValue{};
return false;
}
absl::Status CustomMapValue::Has(
const Value& key,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(arena != nullptr);
ABSL_DCHECK(result != nullptr);
switch (key.kind()) {
case ValueKind::kError:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kUnknown:
*result = key;
return absl::OkStatus();
case ValueKind::kBool:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kInt:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kUint:
ABSL_FALLTHROUGH_INTENDED;
case ValueKind::kString:
break;
default:
*result = ErrorValue(InvalidMapKeyTypeError(key.kind()));
return absl::OkStatus();
}
bool has;
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
CEL_ASSIGN_OR_RETURN(
has, content.interface->HasImpl(key, descriptor_pool, message_factory,
arena));
} else {
CEL_ASSIGN_OR_RETURN(
has, dispatcher_->has(dispatcher_, content_, key, descriptor_pool,
message_factory, arena));
}
*result = BoolValue(has);
return absl::OkStatus();
}
absl::Status CustomMapValue::ListKeys(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena,
absl::Nonnull<ListValue*> result) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(arena != nullptr);
ABSL_DCHECK(result != nullptr);
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->ListKeys(descriptor_pool, message_factory, arena,
result);
}
return dispatcher_->list_keys(dispatcher_, content_, descriptor_pool,
message_factory, arena, result);
}
absl::Status CustomMapValue::ForEach(
ForEachCallback callback,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena) const {
ABSL_DCHECK(descriptor_pool != nullptr);
ABSL_DCHECK(message_factory != nullptr);
ABSL_DCHECK(arena != nullptr);
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->ForEach(callback, descriptor_pool,
message_factory, arena);
}
if (dispatcher_->for_each != nullptr) {
return dispatcher_->for_each(dispatcher_, content_, callback,
descriptor_pool, message_factory, arena);
}
absl::Nonnull<ValueIteratorPtr> iterator;
if (dispatcher_->new_iterator != nullptr) {
CEL_ASSIGN_OR_RETURN(iterator,
dispatcher_->new_iterator(dispatcher_, content_));
} else {
iterator = std::make_unique<CustomMapValueDispatcherKeysIterator>(
dispatcher_, content_);
}
while (iterator->HasNext()) {
Value key;
Value value;
CEL_RETURN_IF_ERROR(
iterator->Next(descriptor_pool, message_factory, arena, &key));
CEL_ASSIGN_OR_RETURN(
bool found,
dispatcher_->find(dispatcher_, content_, key, descriptor_pool,
message_factory, arena, &value));
if (!found) {
value = ErrorValue(NoSuchKeyError(key));
}
CEL_ASSIGN_OR_RETURN(auto ok, callback(key, value));
if (!ok) {
break;
}
}
return absl::OkStatus();
}
absl::StatusOr<absl::Nonnull<ValueIteratorPtr>> CustomMapValue::NewIterator()
const {
if (dispatcher_ == nullptr) {
CustomMapValueInterface::Content content =
content_.To<CustomMapValueInterface::Content>();
ABSL_DCHECK(content.interface != nullptr);
return content.interface->NewIterator();
}
if (dispatcher_->new_iterator != nullptr) {
return dispatcher_->new_iterator(dispatcher_, content_);
}
return std::make_unique<CustomMapValueDispatcherKeysIterator>(dispatcher_,
content_);
}
} // namespace cel