Coverage Report

Created: 2026-11-12 06:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/values/custom_map_value.cc
Line
Count
Source
1
// Copyright 2023 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <cstddef>
16
#include <memory>
17
#include <optional>
18
#include <string>
19
#include <utility>
20
21
#include "absl/base/attributes.h"
22
#include "absl/base/no_destructor.h"
23
#include "absl/base/nullability.h"
24
#include "absl/base/optimization.h"
25
#include "absl/log/absl_check.h"
26
#include "absl/status/status.h"
27
#include "absl/status/statusor.h"
28
#include "absl/strings/str_cat.h"
29
#include "absl/strings/string_view.h"
30
#include "absl/types/optional.h"
31
#include "common/native_type.h"
32
#include "common/value.h"
33
#include "common/value_kind.h"
34
#include "common/values/list_value_builder.h"
35
#include "common/values/map_value_builder.h"
36
#include "common/values/values.h"
37
#include "eval/public/cel_value.h"
38
#include "internal/status_macros.h"
39
#include "internal/well_known_types.h"
40
#include "google/protobuf/arena.h"
41
#include "google/protobuf/descriptor.h"
42
#include "google/protobuf/io/zero_copy_stream.h"
43
#include "google/protobuf/message.h"
44
45
namespace cel {
46
47
namespace {
48
49
using ::cel::well_known_types::StructReflection;
50
using ::cel::well_known_types::ValueReflection;
51
using ::google::api::expr::runtime::CelList;
52
using ::google::api::expr::runtime::CelValue;
53
54
103k
absl::Status NoSuchKeyError(const Value& key) {
55
103k
  return absl::NotFoundError(
56
103k
      absl::StrCat("Key not found in map : ", key.DebugString()));
57
103k
}
58
59
585
absl::Status InvalidMapKeyTypeError(ValueKind kind) {
60
585
  return absl::InvalidArgumentError(
61
585
      absl::StrCat("Invalid map key type: '", ValueKindToString(kind), "'"));
62
585
}
63
64
class EmptyMapValue final : public common_internal::CompatMapValue {
65
 public:
66
468k
  static const EmptyMapValue& Get() {
67
468k
    static const absl::NoDestructor<EmptyMapValue> empty;
68
468k
    return *empty;
69
468k
  }
70
71
1
  EmptyMapValue() = default;
72
73
0
  google::protobuf::Arena* GetOwningArena() const override { return nullptr; }
74
75
0
  std::string DebugString() const override { return "{}"; }
76
77
0
  bool IsEmpty() const override { return true; }
78
79
6.93k
  size_t Size() const override { return 0; }
80
81
  absl::Status ListKeys(
82
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
83
      google::protobuf::MessageFactory* absl_nonnull message_factory,
84
      google::protobuf::Arena* absl_nonnull arena,
85
0
      ListValue* absl_nonnull result) const override {
86
0
    *result = ListValue();
87
0
    return absl::OkStatus();
88
0
  }
89
90
1.59k
  absl::StatusOr<absl_nonnull ValueIteratorPtr> NewIterator() const override {
91
1.59k
    return NewEmptyValueIterator();
92
1.59k
  }
93
94
  absl::Status ConvertToJsonObject(
95
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
96
      google::protobuf::MessageFactory* absl_nonnull message_factory,
97
6.37k
      google::protobuf::Message* absl_nonnull json) const override {
98
6.37k
    ABSL_DCHECK(descriptor_pool != nullptr);
99
6.37k
    ABSL_DCHECK(message_factory != nullptr);
100
6.37k
    ABSL_DCHECK(json != nullptr);
101
6.37k
    ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
102
6.37k
                   google::protobuf::Descriptor::WELLKNOWNTYPE_STRUCT);
103
104
6.37k
    json->Clear();
105
6.37k
    return absl::OkStatus();
106
6.37k
  }
107
108
0
  CustomMapValue Clone(google::protobuf::Arena* absl_nonnull) const override {
109
0
    return CustomMapValue();
110
0
  }
111
112
0
  absl::optional<CelValue> operator[](CelValue key) const override {
113
0
    return std::nullopt;
114
0
  }
115
116
  using CompatMapValue::Get;
117
  absl::optional<CelValue> Get(google::protobuf::Arena* arena,
118
0
                               CelValue key) const override {
119
0
    return std::nullopt;
120
0
  }
121
122
0
  absl::StatusOr<bool> Has(const CelValue& key) const override { return false; }
123
124
0
  int size() const override { return static_cast<int>(Size()); }
125
126
0
  absl::StatusOr<const CelList*> ListKeys() const override {
127
0
    return common_internal::EmptyCompatListValue();
128
0
  }
129
130
0
  absl::StatusOr<const CelList*> ListKeys(google::protobuf::Arena*) const override {
131
0
    return ListKeys();
132
0
  }
133
134
 private:
135
  absl::StatusOr<bool> Find(
136
      const Value& key,
137
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
138
      google::protobuf::MessageFactory* absl_nonnull message_factory,
139
      google::protobuf::Arena* absl_nonnull arena,
140
585k
      Value* absl_nonnull result) const override {
141
585k
    return false;
142
585k
  }
143
144
  absl::StatusOr<bool> Has(
145
      const Value& key,
146
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
147
      google::protobuf::MessageFactory* absl_nonnull message_factory,
148
12.0k
      google::protobuf::Arena* absl_nonnull arena) const override {
149
12.0k
    return false;
150
12.0k
  }
151
};
152
153
}  // namespace
154
155
namespace common_internal {
156
157
0
const CompatMapValue* absl_nonnull EmptyCompatMapValue() {
158
0
  return &EmptyMapValue::Get();
159
0
}
160
161
}  // namespace common_internal
162
163
class CustomMapValueInterfaceIterator final : public ValueIterator {
164
 public:
165
  explicit CustomMapValueInterfaceIterator(
166
      const CustomMapValueInterface* absl_nonnull interface)
167
0
      : interface_(interface) {}
168
169
0
  bool HasNext() override {
170
0
    if (keys_iterator_ == nullptr) {
171
0
      return !interface_->IsEmpty();
172
0
    }
173
0
    return keys_iterator_->HasNext();
174
0
  }
175
176
  absl::Status Next(const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
177
                    google::protobuf::MessageFactory* absl_nonnull message_factory,
178
                    google::protobuf::Arena* absl_nonnull arena,
179
0
                    Value* absl_nonnull result) override {
180
0
    if (keys_iterator_ == nullptr) {
181
0
      if (interface_->IsEmpty()) {
182
0
        return absl::FailedPreconditionError(
183
0
            "ValueIterator::Next() called when "
184
0
            "ValueIterator::HasNext() returns false");
185
0
      }
186
0
      CEL_RETURN_IF_ERROR(ProjectKeys(descriptor_pool, message_factory, arena));
187
0
    }
188
0
    return keys_iterator_->Next(descriptor_pool, message_factory, arena,
189
0
                                result);
190
0
  }
191
192
  absl::StatusOr<bool> Next1(
193
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
194
      google::protobuf::MessageFactory* absl_nonnull message_factory,
195
      google::protobuf::Arena* absl_nonnull arena,
196
0
      Value* absl_nonnull key_or_value) override {
197
0
    ABSL_DCHECK(descriptor_pool != nullptr);
198
0
    ABSL_DCHECK(message_factory != nullptr);
199
0
    ABSL_DCHECK(arena != nullptr);
200
0
    ABSL_DCHECK(key_or_value != nullptr);
201
202
0
    if (keys_iterator_ == nullptr) {
203
0
      if (interface_->IsEmpty()) {
204
0
        return false;
205
0
      }
206
0
      CEL_RETURN_IF_ERROR(ProjectKeys(descriptor_pool, message_factory, arena));
207
0
    }
208
209
0
    return keys_iterator_->Next1(descriptor_pool, message_factory, arena,
210
0
                                 key_or_value);
211
0
  }
212
213
  absl::StatusOr<bool> Next2(
214
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
215
      google::protobuf::MessageFactory* absl_nonnull message_factory,
216
      google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull key,
217
0
      Value* absl_nullable value) override {
218
0
    ABSL_DCHECK(descriptor_pool != nullptr);
219
0
    ABSL_DCHECK(message_factory != nullptr);
220
0
    ABSL_DCHECK(arena != nullptr);
221
0
    ABSL_DCHECK(key != nullptr);
222
223
0
    if (keys_iterator_ == nullptr) {
224
0
      if (interface_->IsEmpty()) {
225
0
        return false;
226
0
      }
227
0
      CEL_RETURN_IF_ERROR(ProjectKeys(descriptor_pool, message_factory, arena));
228
0
    }
229
230
0
    CEL_ASSIGN_OR_RETURN(
231
0
        bool ok,
232
0
        keys_iterator_->Next1(descriptor_pool, message_factory, arena, key));
233
0
    if (!ok) {
234
0
      return false;
235
0
    }
236
0
    if (value != nullptr) {
237
0
      CEL_ASSIGN_OR_RETURN(ok, interface_->Find(*key, descriptor_pool,
238
0
                                                message_factory, arena, value));
239
0
      if (!ok) {
240
0
        return absl::DataLossError(
241
0
            "map iterator returned key that was not present in the map");
242
0
      }
243
0
    }
244
0
    return true;
245
0
  }
246
247
 private:
248
  // Projects the keys from the map, setting `keys_` and `keys_iterator_`. If
249
  // this returns OK it is guaranteed that `keys_iterator_` is not null.
250
  absl::Status ProjectKeys(
251
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
252
      google::protobuf::MessageFactory* absl_nonnull message_factory,
253
0
      google::protobuf::Arena* absl_nonnull arena) {
254
0
    ABSL_DCHECK(keys_iterator_ == nullptr);
255
256
0
    CEL_RETURN_IF_ERROR(
257
0
        interface_->ListKeys(descriptor_pool, message_factory, arena, &keys_));
258
0
    CEL_ASSIGN_OR_RETURN(keys_iterator_, keys_.NewIterator());
259
0
    ABSL_CHECK(keys_iterator_->HasNext());  // Crash OK
260
0
    return absl::OkStatus();
261
0
  }
262
263
  const CustomMapValueInterface* absl_nonnull const interface_;
264
  ListValue keys_;
265
  absl_nullable ValueIteratorPtr keys_iterator_;
266
};
267
268
namespace {
269
270
class CustomMapValueDispatcherIterator final : public ValueIterator {
271
 public:
272
  explicit CustomMapValueDispatcherIterator(
273
      const CustomMapValueDispatcher* absl_nonnull dispatcher,
274
      CustomMapValueContent content)
275
0
      : dispatcher_(dispatcher), content_(content) {}
276
277
0
  bool HasNext() override {
278
0
    if (keys_iterator_ == nullptr) {
279
0
      if (dispatcher_->is_empty != nullptr) {
280
0
        return !dispatcher_->is_empty(dispatcher_, content_);
281
0
      }
282
0
      return dispatcher_->size(dispatcher_, content_) != 0;
283
0
    }
284
0
    return keys_iterator_->HasNext();
285
0
  }
286
287
  absl::Status Next(const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
288
                    google::protobuf::MessageFactory* absl_nonnull message_factory,
289
                    google::protobuf::Arena* absl_nonnull arena,
290
0
                    Value* absl_nonnull result) override {
291
0
    if (keys_iterator_ == nullptr) {
292
0
      if (dispatcher_->is_empty != nullptr
293
0
              ? dispatcher_->is_empty(dispatcher_, content_)
294
0
              : dispatcher_->size(dispatcher_, content_) == 0) {
295
0
        return absl::FailedPreconditionError(
296
0
            "ValueIterator::Next() called when "
297
0
            "ValueIterator::HasNext() returns false");
298
0
      }
299
0
      CEL_RETURN_IF_ERROR(ProjectKeys(descriptor_pool, message_factory, arena));
300
0
    }
301
0
    return keys_iterator_->Next(descriptor_pool, message_factory, arena,
302
0
                                result);
303
0
  }
304
305
  absl::StatusOr<bool> Next1(
306
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
307
      google::protobuf::MessageFactory* absl_nonnull message_factory,
308
      google::protobuf::Arena* absl_nonnull arena,
309
0
      Value* absl_nonnull key_or_value) override {
310
0
    ABSL_DCHECK(descriptor_pool != nullptr);
311
0
    ABSL_DCHECK(message_factory != nullptr);
312
0
    ABSL_DCHECK(arena != nullptr);
313
0
    ABSL_DCHECK(key_or_value != nullptr);
314
315
0
    if (keys_iterator_ == nullptr) {
316
0
      if (dispatcher_->is_empty != nullptr
317
0
              ? dispatcher_->is_empty(dispatcher_, content_)
318
0
              : dispatcher_->size(dispatcher_, content_) == 0) {
319
0
        return false;
320
0
      }
321
0
      CEL_RETURN_IF_ERROR(ProjectKeys(descriptor_pool, message_factory, arena));
322
0
    }
323
324
0
    return keys_iterator_->Next1(descriptor_pool, message_factory, arena,
325
0
                                 key_or_value);
326
0
  }
327
328
  absl::StatusOr<bool> Next2(
329
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
330
      google::protobuf::MessageFactory* absl_nonnull message_factory,
331
      google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull key,
332
0
      Value* absl_nullable value) override {
333
0
    ABSL_DCHECK(descriptor_pool != nullptr);
334
0
    ABSL_DCHECK(message_factory != nullptr);
335
0
    ABSL_DCHECK(arena != nullptr);
336
0
    ABSL_DCHECK(key != nullptr);
337
0
    ABSL_DCHECK(value != nullptr);
338
339
0
    if (keys_iterator_ == nullptr) {
340
0
      if (dispatcher_->is_empty != nullptr
341
0
              ? dispatcher_->is_empty(dispatcher_, content_)
342
0
              : dispatcher_->size(dispatcher_, content_) == 0) {
343
0
        return false;
344
0
      }
345
0
      CEL_RETURN_IF_ERROR(ProjectKeys(descriptor_pool, message_factory, arena));
346
0
    }
347
348
0
    CEL_ASSIGN_OR_RETURN(
349
0
        bool ok,
350
0
        keys_iterator_->Next1(descriptor_pool, message_factory, arena, key));
351
0
    if (!ok) {
352
0
      return false;
353
0
    }
354
0
    if (value != nullptr) {
355
0
      CEL_ASSIGN_OR_RETURN(
356
0
          ok, dispatcher_->find(dispatcher_, content_, *key, descriptor_pool,
357
0
                                message_factory, arena, value));
358
0
      if (!ok) {
359
0
        return absl::DataLossError(
360
0
            "map iterator returned key that was not present in the map");
361
0
      }
362
0
    }
363
0
    return true;
364
0
  }
365
366
 private:
367
  absl::Status ProjectKeys(
368
      const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
369
      google::protobuf::MessageFactory* absl_nonnull message_factory,
370
0
      google::protobuf::Arena* absl_nonnull arena) {
371
0
    ABSL_DCHECK(keys_iterator_ == nullptr);
372
373
0
    CEL_RETURN_IF_ERROR(dispatcher_->list_keys(dispatcher_, content_,
374
0
                                               descriptor_pool, message_factory,
375
0
                                               arena, &keys_));
376
0
    CEL_ASSIGN_OR_RETURN(keys_iterator_, keys_.NewIterator());
377
0
    ABSL_CHECK(keys_iterator_->HasNext());  // Crash OK
378
0
    return absl::OkStatus();
379
0
  }
380
381
  const CustomMapValueDispatcher* absl_nonnull const dispatcher_;
382
  const CustomMapValueContent content_;
383
  ListValue keys_;
384
  absl_nullable ValueIteratorPtr keys_iterator_;
385
};
386
387
}  // namespace
388
389
absl::Status CustomMapValueInterface::SerializeTo(
390
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
391
    google::protobuf::MessageFactory* absl_nonnull message_factory,
392
0
    google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const {
393
0
  ABSL_DCHECK(descriptor_pool != nullptr);
394
0
  ABSL_DCHECK(message_factory != nullptr);
395
0
  ABSL_DCHECK(output != nullptr);
396
397
0
  StructReflection reflection;
398
0
  CEL_RETURN_IF_ERROR(reflection.Initialize(descriptor_pool));
399
0
  const google::protobuf::Message* prototype =
400
0
      message_factory->GetPrototype(reflection.GetDescriptor());
401
0
  if (prototype == nullptr) {
402
0
    return absl::UnknownError(
403
0
        absl::StrCat("failed to get message prototype: ",
404
0
                     reflection.GetDescriptor()->full_name()));
405
0
  }
406
0
  google::protobuf::Arena arena;
407
0
  google::protobuf::Message* message = prototype->New(&arena);
408
0
  CEL_RETURN_IF_ERROR(
409
0
      ConvertToJsonObject(descriptor_pool, message_factory, message));
410
0
  if (!message->SerializePartialToZeroCopyStream(output)) {
411
0
    return absl::UnknownError(
412
0
        "failed to serialize message: google.protobuf.Struct");
413
0
  }
414
0
  return absl::OkStatus();
415
0
}
416
417
absl::Status CustomMapValueInterface::ForEach(
418
    ForEachCallback callback,
419
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
420
    google::protobuf::MessageFactory* absl_nonnull message_factory,
421
0
    google::protobuf::Arena* absl_nonnull arena) const {
422
0
  CEL_ASSIGN_OR_RETURN(auto iterator, NewIterator());
423
0
  while (iterator->HasNext()) {
424
0
    Value key;
425
0
    Value value;
426
0
    CEL_RETURN_IF_ERROR(
427
0
        iterator->Next(descriptor_pool, message_factory, arena, &key));
428
0
    CEL_ASSIGN_OR_RETURN(
429
0
        bool found, Find(key, descriptor_pool, message_factory, arena, &value));
430
0
    if (!found) {
431
0
      value = ErrorValue::From(NoSuchKeyError(key), arena);
432
0
    }
433
0
    CEL_ASSIGN_OR_RETURN(auto ok, callback(key, value));
434
0
    if (!ok) {
435
0
      break;
436
0
    }
437
0
  }
438
0
  return absl::OkStatus();
439
0
}
440
441
absl::StatusOr<absl_nonnull ValueIteratorPtr>
442
0
CustomMapValueInterface::NewIterator() const {
443
0
  return std::make_unique<CustomMapValueInterfaceIterator>(this);
444
0
}
445
446
absl::Status CustomMapValueInterface::Equal(
447
    const MapValue& other,
448
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
449
    google::protobuf::MessageFactory* absl_nonnull message_factory,
450
1.36k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
451
1.36k
  return MapValueEqual(*this, other, descriptor_pool, message_factory, arena,
452
1.36k
                       result);
453
1.36k
}
454
455
468k
CustomMapValue::CustomMapValue() {
456
468k
  content_ = CustomMapValueContent::From(CustomMapValueInterface::Content{
457
468k
      .interface = &EmptyMapValue::Get(), .arena = nullptr});
458
468k
}
459
460
22
NativeTypeId CustomMapValue::GetTypeId() const {
461
22
  if (dispatcher_ == nullptr) {
462
22
    CustomMapValueInterface::Content content =
463
22
        content_.To<CustomMapValueInterface::Content>();
464
22
    ABSL_DCHECK(content.interface != nullptr);
465
22
    return content.interface->GetNativeTypeId();
466
22
  }
467
0
  return dispatcher_->get_type_id(dispatcher_, content_);
468
22
}
469
470
276
absl::string_view CustomMapValue::GetTypeName() const { return "map"; }
471
472
0
std::string CustomMapValue::DebugString() const {
473
0
  if (dispatcher_ == nullptr) {
474
0
    CustomMapValueInterface::Content content =
475
0
        content_.To<CustomMapValueInterface::Content>();
476
0
    ABSL_DCHECK(content.interface != nullptr);
477
0
    return content.interface->DebugString();
478
0
  }
479
0
  if (dispatcher_->debug_string != nullptr) {
480
0
    return dispatcher_->debug_string(dispatcher_, content_);
481
0
  }
482
0
  return "map";
483
0
}
484
485
absl::Status CustomMapValue::SerializeTo(
486
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
487
    google::protobuf::MessageFactory* absl_nonnull message_factory,
488
0
    google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const {
489
0
  if (dispatcher_ == nullptr) {
490
0
    CustomMapValueInterface::Content content =
491
0
        content_.To<CustomMapValueInterface::Content>();
492
0
    ABSL_DCHECK(content.interface != nullptr);
493
0
    return content.interface->SerializeTo(descriptor_pool, message_factory,
494
0
                                          output);
495
0
  }
496
0
  if (dispatcher_->serialize_to != nullptr) {
497
0
    return dispatcher_->serialize_to(dispatcher_, content_, descriptor_pool,
498
0
                                     message_factory, output);
499
0
  }
500
0
  return absl::UnimplementedError(
501
0
      absl::StrCat(GetTypeName(), " is unserializable"));
502
0
}
503
504
absl::Status CustomMapValue::ConvertToJson(
505
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
506
    google::protobuf::MessageFactory* absl_nonnull message_factory,
507
96.5k
    google::protobuf::Message* absl_nonnull json) const {
508
96.5k
  ABSL_DCHECK(descriptor_pool != nullptr);
509
96.5k
  ABSL_DCHECK(message_factory != nullptr);
510
96.5k
  ABSL_DCHECK(json != nullptr);
511
96.5k
  ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
512
96.5k
                 google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
513
514
96.5k
  ValueReflection value_reflection;
515
96.5k
  CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor()));
516
96.5k
  google::protobuf::Message* json_object = value_reflection.MutableStructValue(json);
517
518
96.5k
  return ConvertToJsonObject(descriptor_pool, message_factory, json_object);
519
96.5k
}
520
521
absl::Status CustomMapValue::ConvertToJsonObject(
522
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
523
    google::protobuf::MessageFactory* absl_nonnull message_factory,
524
96.5k
    google::protobuf::Message* absl_nonnull json) const {
525
96.5k
  ABSL_DCHECK(descriptor_pool != nullptr);
526
96.5k
  ABSL_DCHECK(message_factory != nullptr);
527
96.5k
  ABSL_DCHECK(json != nullptr);
528
96.5k
  ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(),
529
96.5k
                 google::protobuf::Descriptor::WELLKNOWNTYPE_STRUCT);
530
531
96.5k
  if (dispatcher_ == nullptr) {
532
96.5k
    CustomMapValueInterface::Content content =
533
96.5k
        content_.To<CustomMapValueInterface::Content>();
534
96.5k
    ABSL_DCHECK(content.interface != nullptr);
535
96.5k
    return content.interface->ConvertToJsonObject(descriptor_pool,
536
96.5k
                                                  message_factory, json);
537
96.5k
  }
538
0
  if (dispatcher_->convert_to_json_object != nullptr) {
539
0
    return dispatcher_->convert_to_json_object(
540
0
        dispatcher_, content_, descriptor_pool, message_factory, json);
541
0
  }
542
0
  return absl::UnimplementedError(
543
0
      absl::StrCat(GetTypeName(), " is not convertable to JSON"));
544
0
}
545
546
absl::Status CustomMapValue::Equal(
547
    const Value& other,
548
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
549
    google::protobuf::MessageFactory* absl_nonnull message_factory,
550
1.94k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
551
1.94k
  ABSL_DCHECK(descriptor_pool != nullptr);
552
1.94k
  ABSL_DCHECK(message_factory != nullptr);
553
1.94k
  ABSL_DCHECK(arena != nullptr);
554
1.94k
  ABSL_DCHECK(result != nullptr);
555
556
1.94k
  if (auto other_map_value = other.AsMap(); other_map_value) {
557
1.36k
    if (dispatcher_ == nullptr) {
558
1.36k
      CustomMapValueInterface::Content content =
559
1.36k
          content_.To<CustomMapValueInterface::Content>();
560
1.36k
      ABSL_DCHECK(content.interface != nullptr);
561
1.36k
      return content.interface->Equal(*other_map_value, descriptor_pool,
562
1.36k
                                      message_factory, arena, result);
563
1.36k
    }
564
0
    if (dispatcher_->equal != nullptr) {
565
0
      return dispatcher_->equal(dispatcher_, content_, *other_map_value,
566
0
                                descriptor_pool, message_factory, arena,
567
0
                                result);
568
0
    }
569
0
    return common_internal::MapValueEqual(*this, *other_map_value,
570
0
                                          descriptor_pool, message_factory,
571
0
                                          arena, result);
572
0
  }
573
579
  *result = FalseValue();
574
579
  return absl::OkStatus();
575
1.94k
}
576
577
0
bool CustomMapValue::IsZeroValue() const {
578
0
  if (dispatcher_ == nullptr) {
579
0
    CustomMapValueInterface::Content content =
580
0
        content_.To<CustomMapValueInterface::Content>();
581
0
    ABSL_DCHECK(content.interface != nullptr);
582
0
    return content.interface->IsZeroValue();
583
0
  }
584
0
  return dispatcher_->is_zero_value(dispatcher_, content_);
585
0
}
586
587
0
CustomMapValue CustomMapValue::Clone(google::protobuf::Arena* absl_nonnull arena) const {
588
0
  ABSL_DCHECK(arena != nullptr);
589
590
0
  if (dispatcher_ == nullptr) {
591
0
    CustomMapValueInterface::Content content =
592
0
        content_.To<CustomMapValueInterface::Content>();
593
0
    ABSL_DCHECK(content.interface != nullptr);
594
0
    return content.interface->Clone(arena);
595
0
  }
596
0
  return dispatcher_->clone(dispatcher_, content_, arena);
597
0
}
598
599
0
bool CustomMapValue::IsEmpty() const {
600
0
  if (dispatcher_ == nullptr) {
601
0
    CustomMapValueInterface::Content content =
602
0
        content_.To<CustomMapValueInterface::Content>();
603
0
    ABSL_DCHECK(content.interface != nullptr);
604
0
    return content.interface->IsEmpty();
605
0
  }
606
0
  if (dispatcher_->is_empty != nullptr) {
607
0
    return dispatcher_->is_empty(dispatcher_, content_);
608
0
  }
609
0
  return dispatcher_->size(dispatcher_, content_) == 0;
610
0
}
611
612
18.7k
size_t CustomMapValue::Size() const {
613
18.7k
  if (dispatcher_ == nullptr) {
614
18.7k
    CustomMapValueInterface::Content content =
615
18.7k
        content_.To<CustomMapValueInterface::Content>();
616
18.7k
    ABSL_DCHECK(content.interface != nullptr);
617
18.7k
    return content.interface->Size();
618
18.7k
  }
619
0
  return dispatcher_->size(dispatcher_, content_);
620
18.7k
}
621
622
absl::Status CustomMapValue::Get(
623
    const Value& key,
624
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
625
    google::protobuf::MessageFactory* absl_nonnull message_factory,
626
104k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
627
104k
  ABSL_DCHECK(descriptor_pool != nullptr);
628
104k
  ABSL_DCHECK(message_factory != nullptr);
629
104k
  ABSL_DCHECK(arena != nullptr);
630
104k
  ABSL_DCHECK(result != nullptr);
631
632
209k
  CEL_ASSIGN_OR_RETURN(
633
209k
      bool ok, Find(key, descriptor_pool, message_factory, arena, result));
634
209k
  if (ABSL_PREDICT_FALSE(!ok)) {
635
103k
    switch (result->kind()) {
636
0
      case ValueKind::kError:
637
0
        ABSL_FALLTHROUGH_INTENDED;
638
0
      case ValueKind::kUnknown:
639
0
        break;
640
103k
      default:
641
103k
        *result = ErrorValue::From(NoSuchKeyError(key), arena);
642
103k
        break;
643
103k
    }
644
103k
  }
645
104k
  return absl::OkStatus();
646
209k
}
647
648
absl::StatusOr<bool> CustomMapValue::Find(
649
    const Value& key,
650
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
651
    google::protobuf::MessageFactory* absl_nonnull message_factory,
652
691k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
653
691k
  ABSL_DCHECK(descriptor_pool != nullptr);
654
691k
  ABSL_DCHECK(message_factory != nullptr);
655
691k
  ABSL_DCHECK(arena != nullptr);
656
691k
  ABSL_DCHECK(result != nullptr);
657
658
691k
  switch (key.kind()) {
659
0
    case ValueKind::kError:
660
0
      ABSL_FALLTHROUGH_INTENDED;
661
0
    case ValueKind::kUnknown:
662
0
      *result = key;
663
0
      return false;
664
41.0k
    case ValueKind::kBool:
665
41.0k
      ABSL_FALLTHROUGH_INTENDED;
666
340k
    case ValueKind::kInt:
667
340k
      ABSL_FALLTHROUGH_INTENDED;
668
624k
    case ValueKind::kUint:
669
624k
      ABSL_FALLTHROUGH_INTENDED;
670
691k
    case ValueKind::kString:
671
691k
      break;
672
0
    default:
673
0
      *result = ErrorValue::From(InvalidMapKeyTypeError(key.kind()), arena);
674
0
      return false;
675
691k
  }
676
677
691k
  if (dispatcher_ == nullptr) {
678
691k
    CustomMapValueInterface::Content content =
679
691k
        content_.To<CustomMapValueInterface::Content>();
680
691k
    ABSL_DCHECK(content.interface != nullptr);
681
691k
    auto status_or_found = content.interface->Find(
682
691k
        key, descriptor_pool, message_factory, arena, result);
683
691k
    if (!status_or_found.ok()) {
684
0
      *result = ErrorValue::From(std::move(status_or_found).status(), arena);
685
0
      return false;
686
0
    }
687
691k
    if (!*status_or_found) {
688
672k
      *result = NullValue();
689
672k
      return false;
690
672k
    }
691
19.1k
    return true;
692
691k
  }
693
0
  auto status_or_found =
694
0
      dispatcher_->find(dispatcher_, content_, key, descriptor_pool,
695
0
                        message_factory, arena, result);
696
0
  if (!status_or_found.ok()) {
697
0
    *result = ErrorValue::From(std::move(status_or_found).status(), arena);
698
0
    return false;
699
0
  }
700
0
  if (!*status_or_found) {
701
0
    *result = NullValue();
702
0
    return false;
703
0
  }
704
0
  return true;
705
0
}
706
707
absl::Status CustomMapValue::Has(
708
    const Value& key,
709
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
710
    google::protobuf::MessageFactory* absl_nonnull message_factory,
711
14.2k
    google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const {
712
14.2k
  ABSL_DCHECK(descriptor_pool != nullptr);
713
14.2k
  ABSL_DCHECK(message_factory != nullptr);
714
14.2k
  ABSL_DCHECK(arena != nullptr);
715
14.2k
  ABSL_DCHECK(result != nullptr);
716
717
14.2k
  switch (key.kind()) {
718
0
    case ValueKind::kError:
719
0
      ABSL_FALLTHROUGH_INTENDED;
720
0
    case ValueKind::kUnknown:
721
0
      *result = key;
722
0
      return absl::OkStatus();
723
599
    case ValueKind::kBool:
724
599
      ABSL_FALLTHROUGH_INTENDED;
725
2.01k
    case ValueKind::kInt:
726
2.01k
      ABSL_FALLTHROUGH_INTENDED;
727
3.32k
    case ValueKind::kUint:
728
3.32k
      ABSL_FALLTHROUGH_INTENDED;
729
13.7k
    case ValueKind::kString:
730
13.7k
      break;
731
585
    default:
732
585
      *result = ErrorValue::From(InvalidMapKeyTypeError(key.kind()), arena);
733
585
      return absl::OkStatus();
734
14.2k
  }
735
13.7k
  if (dispatcher_ == nullptr) {
736
13.7k
    CustomMapValueInterface::Content content =
737
13.7k
        content_.To<CustomMapValueInterface::Content>();
738
13.7k
    ABSL_DCHECK(content.interface != nullptr);
739
13.7k
    auto status_or_has =
740
13.7k
        content.interface->Has(key, descriptor_pool, message_factory, arena);
741
13.7k
    if (!status_or_has.ok()) {
742
0
      *result = ErrorValue::From(std::move(status_or_has).status(), arena);
743
0
      return absl::OkStatus();
744
0
    }
745
13.7k
    *result = BoolValue(*status_or_has);
746
13.7k
    return absl::OkStatus();
747
13.7k
  }
748
0
  auto status_or_has = dispatcher_->has(
749
0
      dispatcher_, content_, key, descriptor_pool, message_factory, arena);
750
0
  if (!status_or_has.ok()) {
751
0
    *result = ErrorValue::From(std::move(status_or_has).status(), arena);
752
0
    return absl::OkStatus();
753
0
  }
754
0
  *result = BoolValue(*status_or_has);
755
0
  return absl::OkStatus();
756
0
}
757
758
absl::Status CustomMapValue::ListKeys(
759
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
760
    google::protobuf::MessageFactory* absl_nonnull message_factory,
761
0
    google::protobuf::Arena* absl_nonnull arena, ListValue* absl_nonnull result) const {
762
0
  ABSL_DCHECK(descriptor_pool != nullptr);
763
0
  ABSL_DCHECK(message_factory != nullptr);
764
0
  ABSL_DCHECK(arena != nullptr);
765
0
  ABSL_DCHECK(result != nullptr);
766
767
0
  if (dispatcher_ == nullptr) {
768
0
    CustomMapValueInterface::Content content =
769
0
        content_.To<CustomMapValueInterface::Content>();
770
0
    ABSL_DCHECK(content.interface != nullptr);
771
0
    return content.interface->ListKeys(descriptor_pool, message_factory, arena,
772
0
                                       result);
773
0
  }
774
0
  return dispatcher_->list_keys(dispatcher_, content_, descriptor_pool,
775
0
                                message_factory, arena, result);
776
0
}
777
778
absl::Status CustomMapValue::ForEach(
779
    ForEachCallback callback,
780
    const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
781
    google::protobuf::MessageFactory* absl_nonnull message_factory,
782
0
    google::protobuf::Arena* absl_nonnull arena) const {
783
0
  ABSL_DCHECK(descriptor_pool != nullptr);
784
0
  ABSL_DCHECK(message_factory != nullptr);
785
0
  ABSL_DCHECK(arena != nullptr);
786
787
0
  if (dispatcher_ == nullptr) {
788
0
    CustomMapValueInterface::Content content =
789
0
        content_.To<CustomMapValueInterface::Content>();
790
0
    ABSL_DCHECK(content.interface != nullptr);
791
0
    return content.interface->ForEach(callback, descriptor_pool,
792
0
                                      message_factory, arena);
793
0
  }
794
0
  if (dispatcher_->for_each != nullptr) {
795
0
    return dispatcher_->for_each(dispatcher_, content_, callback,
796
0
                                 descriptor_pool, message_factory, arena);
797
0
  }
798
0
  absl_nonnull ValueIteratorPtr iterator;
799
0
  if (dispatcher_->new_iterator != nullptr) {
800
0
    CEL_ASSIGN_OR_RETURN(iterator,
801
0
                         dispatcher_->new_iterator(dispatcher_, content_));
802
0
  } else {
803
0
    iterator = std::make_unique<CustomMapValueDispatcherIterator>(dispatcher_,
804
0
                                                                  content_);
805
0
  }
806
0
  while (iterator->HasNext()) {
807
0
    Value key;
808
0
    Value value;
809
0
    CEL_RETURN_IF_ERROR(
810
0
        iterator->Next(descriptor_pool, message_factory, arena, &key));
811
0
    CEL_ASSIGN_OR_RETURN(
812
0
        bool found,
813
0
        dispatcher_->find(dispatcher_, content_, key, descriptor_pool,
814
0
                          message_factory, arena, &value));
815
0
    if (!found) {
816
0
      value = ErrorValue::From(NoSuchKeyError(key), arena);
817
0
    }
818
0
    CEL_ASSIGN_OR_RETURN(auto ok, callback(key, value));
819
0
    if (!ok) {
820
0
      break;
821
0
    }
822
0
  }
823
0
  return absl::OkStatus();
824
0
}
825
826
absl::StatusOr<absl_nonnull ValueIteratorPtr> CustomMapValue::NewIterator()
827
6.54k
    const {
828
6.54k
  if (dispatcher_ == nullptr) {
829
6.54k
    CustomMapValueInterface::Content content =
830
6.54k
        content_.To<CustomMapValueInterface::Content>();
831
6.54k
    ABSL_DCHECK(content.interface != nullptr);
832
6.54k
    return content.interface->NewIterator();
833
6.54k
  }
834
0
  if (dispatcher_->new_iterator != nullptr) {
835
0
    return dispatcher_->new_iterator(dispatcher_, content_);
836
0
  }
837
0
  return std::make_unique<CustomMapValueDispatcherIterator>(dispatcher_,
838
0
                                                            content_);
839
0
}
840
841
}  // namespace cel