-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathvalue.h
More file actions
2945 lines (2668 loc) · 110 KB
/
value.h
File metadata and controls
2945 lines (2668 loc) · 110 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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUE_H_
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/meta/type_traits.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "absl/utility/utility.h"
#include "base/attribute.h"
#include "common/arena.h"
#include "common/native_type.h"
#include "common/optional_ref.h"
#include "common/type.h"
#include "common/typeinfo.h"
#include "common/value_kind.h"
#include "common/values/bool_value.h" // IWYU pragma: export
#include "common/values/bytes_value.h" // IWYU pragma: export
#include "common/values/bytes_value_input_stream.h" // IWYU pragma: export
#include "common/values/bytes_value_output_stream.h" // IWYU pragma: export
#include "common/values/custom_list_value.h" // IWYU pragma: export
#include "common/values/custom_map_value.h" // IWYU pragma: export
#include "common/values/custom_struct_value.h" // IWYU pragma: export
#include "common/values/double_value.h" // IWYU pragma: export
#include "common/values/duration_value.h" // IWYU pragma: export
#include "common/values/enum_value.h" // IWYU pragma: export
#include "common/values/error_value.h" // IWYU pragma: export
#include "common/values/int_value.h" // IWYU pragma: export
#include "common/values/list_value.h" // IWYU pragma: export
#include "common/values/map_value.h" // IWYU pragma: export
#include "common/values/message_value.h" // IWYU pragma: export
#include "common/values/null_value.h" // IWYU pragma: export
#include "common/values/opaque_value.h" // IWYU pragma: export
#include "common/values/optional_value.h" // IWYU pragma: export
#include "common/values/parsed_json_list_value.h" // IWYU pragma: export
#include "common/values/parsed_json_map_value.h" // IWYU pragma: export
#include "common/values/parsed_map_field_value.h" // IWYU pragma: export
#include "common/values/parsed_message_value.h" // IWYU pragma: export
#include "common/values/parsed_repeated_field_value.h" // IWYU pragma: export
#include "common/values/string_value.h" // IWYU pragma: export
#include "common/values/struct_value.h" // IWYU pragma: export
#include "common/values/timestamp_value.h" // IWYU pragma: export
#include "common/values/type_value.h" // IWYU pragma: export
#include "common/values/uint_value.h" // IWYU pragma: export
#include "common/values/unknown_value.h" // IWYU pragma: export
#include "common/values/value_variant.h"
#include "common/values/values.h"
#include "internal/status_macros.h"
#include "runtime/runtime_options.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/generated_enum_reflection.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/map_field.h"
#include "google/protobuf/message.h"
#undef CEL_DISPATCHER_CONST_INIT
#ifdef _MSC_VER > 0
#define CEL_DISPATCHER_CONST_INIT static
#else
#define CEL_DISPATCHER_CONST_INIT ABSL_CONST_INIT
#endif // ifdef _MSC_VE
namespace cel {
// `Value` is a composition type which encompasses all values supported by the
// Common Expression Language. When default constructed or moved, `Value` is in
// a known but invalid state. Any attempt to use it from then on, without
// assigning another type, is undefined behavior. In debug builds, we do our
// best to fail.
class Value final : private common_internal::ValueMixin<Value> {
public:
// Returns an appropriate `Value` for the dynamic protobuf enum. For open
// enums, returns `cel::IntValue`. For closed enums, returns `cel::ErrorValue`
// if the value is not present in the enum otherwise returns `cel::IntValue`.
static Value Enum(const google::protobuf::EnumValueDescriptor* absl_nonnull value);
static Value Enum(const google::protobuf::EnumDescriptor* absl_nonnull type,
int32_t number);
// SFINAE overload for generated protobuf enums which are not well-known.
// Always returns `cel::IntValue`.
template <typename T>
static common_internal::EnableIfGeneratedEnum<T, IntValue> Enum(T value) {
return IntValue(value);
}
// SFINAE overload for google::protobuf::NullValue. Always returns
// `cel::NullValue`.
template <typename T>
static common_internal::EnableIfWellKnownEnum<T, google::protobuf::NullValue,
NullValue>
Enum(T) {
return NullValue();
}
// Returns an appropriate `Value` for the dynamic protobuf message. If
// `message` is the well known type `google.protobuf.Any`, `descriptor_pool`
// and `message_factory` will be used to unpack the value. Both must outlive
// the resulting value and any of its shallow copies. Otherwise the message is
// copied using `arena`.
static Value FromMessage(
const google::protobuf::Message& message,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
static Value FromMessage(
google::protobuf::Message&& message,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `Value` for the dynamic protobuf message. If
// `message` is the well known type `google.protobuf.Any`, `descriptor_pool`
// and `message_factory` will be used to unpack the value. Both must outlive
// the resulting value and any of its shallow copies. Otherwise the message is
// borrowed (no copying). If the message is on an arena, that arena will be
// attributed as the owner. Otherwise `arena` is used.
static Value WrapMessage(
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `Value` for the dynamic protobuf message. If
// `message` is the well known type `google.protobuf.Any`, `descriptor_pool`
// and `message_factory` will be used to unpack the value. Both must outlive
// the resulting value and any of its shallow copies. Otherwise the message is
// borrowed (no copying). This function does not attempt to validate arena
// ownership of a dynamic message that was not unpacked from a well known
// type. Caller is responsible for ensuring the resulting value and any
// derived values do not outlive the input message.
static Value WrapMessageUnsafe(
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `Value` for the dynamic protobuf message field. If
// `field` in `message` is the well known type `google.protobuf.Any`,
// `descriptor_pool` and `message_factory` will be used to unpack the value.
// Both must outlive the resulting value and any of its shallow copies.
// Otherwise the field is borrowed (no copying). If the message is on an
// arena, that arena will be attributed as the owner. Otherwise `arena` is
// used.
static Value WrapField(
ProtoWrapperTypeOptions wrapper_type_options,
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::FieldDescriptor* absl_nonnull field
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
static Value WrapField(
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::FieldDescriptor* absl_nonnull field
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return WrapField(ProtoWrapperTypeOptions::kUnsetNull, message, field,
descriptor_pool, message_factory, arena);
}
// Returns an appropriate `Value` for the dynamic protobuf message field. If
// `field` in `message` is the well known type `google.protobuf.Any`,
// `descriptor_pool` and `message_factory` will be used to unpack the value.
// Both must outlive the resulting value and any of its shallow copies.
// Otherwise the field is borrowed (no copying). Caller is responsible for
// ensuring the resulting value and any derived values do not outlive the
// input message.
static Value WrapFieldUnsafe(
ProtoWrapperTypeOptions wrapper_type_options,
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::FieldDescriptor* absl_nonnull field
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `Value` for the dynamic protobuf message repeated
// field. If `field` in `message` is the well known type
// `google.protobuf.Any`, `descriptor_pool` and `message_factory` will be used
// to unpack the value. Both must outlive the resulting value and any of its
// shallow copies.
static Value WrapRepeatedField(
int index,
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::FieldDescriptor* absl_nonnull field
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `Value` for the dynamic protobuf message repeated
// field. If `field` in `message` is the well known type
// `google.protobuf.Any`, `descriptor_pool` and `message_factory` will be used
// to unpack the value. Both must outlive the resulting value and any of its
// shallow copies.
static Value WrapRepeatedFieldUnsafe(
int index,
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::FieldDescriptor* absl_nonnull field
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `StringValue` for the dynamic protobuf message map
// field key. The map field key must be a string or the behavior is undefined.
static StringValue WrapMapFieldKeyString(
const google::protobuf::MapKey& key,
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `Value` for the dynamic protobuf message map
// field value. If `field` in `message`, which is `value`, is the well known
// type `google.protobuf.Any`, `descriptor_pool` and `message_factory` will be
// used to unpack the value. Both must outlive the resulting value and any of
// its shallow copies.
static Value WrapMapFieldValue(
const google::protobuf::MapValueConstRef& value,
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::FieldDescriptor* absl_nonnull field
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Returns an appropriate `Value` for the dynamic protobuf message map
// field value. If `field` in `message`, which is `value`, is the well known
// type `google.protobuf.Any`, `descriptor_pool` and `message_factory` will be
// used to unpack the value. Both must outlive the resulting value and any of
// its shallow copies. Caller is responsible for ensuring the resulting value
// and any derived values do not outlive the input message.
static Value WrapMapFieldValueUnsafe(
const google::protobuf::MapValueConstRef& value,
const google::protobuf::Message* absl_nonnull message ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::FieldDescriptor* absl_nonnull field
ABSL_ATTRIBUTE_LIFETIME_BOUND,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::MessageFactory* absl_nonnull message_factory
ABSL_ATTRIBUTE_LIFETIME_BOUND,
google::protobuf::Arena* absl_nonnull arena ABSL_ATTRIBUTE_LIFETIME_BOUND);
Value() = default;
Value(const Value&) = default;
Value& operator=(const Value&) = default;
Value(Value&& other) = default;
Value& operator=(Value&&) = default;
// NOLINTNEXTLINE(google-explicit-constructor)
Value(const ListValue& value) : variant_(value.ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(ListValue&& value) : variant_(std::move(value).ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(const ListValue& value) {
variant_ = value.ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(ListValue&& value) {
variant_ = std::move(value).ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(const MapValue& value) : variant_(value.ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(MapValue&& value) : variant_(std::move(value).ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(const MapValue& value) {
variant_ = value.ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(MapValue&& value) {
variant_ = std::move(value).ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(const StructValue& value) : variant_(value.ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(StructValue&& value) : variant_(std::move(value).ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(const StructValue& value) {
variant_ = value.ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(StructValue&& value) {
variant_ = std::move(value).ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(const MessageValue& value) : variant_(value.ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(MessageValue&& value) : variant_(std::move(value).ToValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(const MessageValue& value) {
variant_ = value.ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(MessageValue&& value) {
variant_ = std::move(value).ToValueVariant();
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(const OptionalValue& value)
: variant_(absl::in_place_type<OpaqueValue>,
static_cast<const OpaqueValue&>(value)) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value(OptionalValue&& value)
: variant_(absl::in_place_type<OpaqueValue>,
static_cast<OpaqueValue&&>(value)) {}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(const OptionalValue& value) {
variant_.Assign(static_cast<const OpaqueValue&>(value));
return *this;
}
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(OptionalValue&& value) {
variant_.Assign(static_cast<OpaqueValue&&>(value));
return *this;
}
template <typename T,
typename = std::enable_if_t<
common_internal::IsValueAlternativeV<absl::remove_cvref_t<T>>>>
// NOLINTNEXTLINE(google-explicit-constructor)
Value(T&& alternative) noexcept
: variant_(absl::in_place_type<absl::remove_cvref_t<T>>,
std::forward<T>(alternative)) {}
template <typename T,
typename = std::enable_if_t<
common_internal::IsValueAlternativeV<absl::remove_cvref_t<T>>>>
// NOLINTNEXTLINE(google-explicit-constructor)
Value& operator=(T&& alternative) noexcept {
variant_.Assign(std::forward<T>(alternative));
return *this;
}
ValueKind kind() const { return variant_.kind(); }
Type GetRuntimeType() const;
absl::string_view GetTypeName() const;
std::string DebugString() const;
// `SerializeTo` serializes this value to `output`. If an error is returned,
// `output` is in a valid but unspecified state. If this value does not
// support serialization, `FAILED_PRECONDITION` is returned.
absl::Status SerializeTo(
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const;
// `ConvertToJson` converts this value to its JSON representation. The
// argument `json` **MUST** be an instance of `google.protobuf.Value` which is
// can either be the generated message or a dynamic message. The descriptor
// pool `descriptor_pool` and message factory `message_factory` are used to
// deal with serialized messages and a few corners cases.
absl::Status ConvertToJson(
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Message* absl_nonnull json) const;
// `ConvertToJsonArray` converts this value to its JSON representation if and
// only if it can be represented as an array. The argument `json` **MUST** be
// an instance of `google.protobuf.ListValue` which is can either be the
// generated message or a dynamic message. The descriptor pool
// `descriptor_pool` and message factory `message_factory` are used to deal
// with serialized messages and a few corners cases.
absl::Status ConvertToJsonArray(
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Message* absl_nonnull json) const;
// `ConvertToJsonArray` converts this value to its JSON representation if and
// only if it can be represented as an object. The argument `json` **MUST** be
// an instance of `google.protobuf.Struct` which is can either be the
// generated message or a dynamic message. The descriptor pool
// `descriptor_pool` and message factory `message_factory` are used to deal
// with serialized messages and a few corners cases.
absl::Status ConvertToJsonObject(
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Message* absl_nonnull json) const;
absl::Status Equal(const Value& other,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena,
Value* absl_nonnull result) const;
using ValueMixin::Equal;
bool IsZeroValue() const;
// Clones the value to another arena, if necessary, such that the lifetime of
// the value is tied to the arena.
Value Clone(google::protobuf::Arena* absl_nonnull arena) const;
friend void swap(Value& lhs, Value& rhs) noexcept {
using std::swap;
swap(lhs.variant_, rhs.variant_);
}
friend std::ostream& operator<<(std::ostream& out, const Value& value);
ABSL_DEPRECATED("Just use operator.()")
Value* operator->() { return this; }
ABSL_DEPRECATED("Just use operator.()")
const Value* operator->() const { return this; }
// Returns `true` if this value is an instance of a bool value.
bool IsBool() const { return variant_.Is<BoolValue>(); }
// Returns `true` if this value is an instance of a bool value and true.
bool IsTrue() const { return IsBool() && GetBool().NativeValue(); }
// Returns `true` if this value is an instance of a bool value and false.
bool IsFalse() const { return IsBool() && !GetBool().NativeValue(); }
// Returns `true` if this value is an instance of a bytes value.
bool IsBytes() const { return variant_.Is<BytesValue>(); }
// Returns `true` if this value is an instance of a double value.
bool IsDouble() const { return variant_.Is<DoubleValue>(); }
// Returns `true` if this value is an instance of a duration value.
bool IsDuration() const { return variant_.Is<DurationValue>(); }
// Returns `true` if this value is an instance of an error value.
bool IsError() const { return variant_.Is<ErrorValue>(); }
// Returns `true` if this value is an instance of an int value.
bool IsInt() const { return variant_.Is<IntValue>(); }
// Returns `true` if this value is an instance of a list value.
bool IsList() const {
return variant_.Is<common_internal::LegacyListValue>() ||
variant_.Is<CustomListValue>() ||
variant_.Is<ParsedRepeatedFieldValue>() ||
variant_.Is<ParsedJsonListValue>();
}
// Returns `true` if this value is an instance of a map value.
bool IsMap() const {
return variant_.Is<common_internal::LegacyMapValue>() ||
variant_.Is<CustomMapValue>() ||
variant_.Is<ParsedMapFieldValue>() ||
variant_.Is<ParsedJsonMapValue>();
}
// Returns `true` if this value is an instance of a message value. If `true`
// is returned, it is implied that `IsStruct()` would also return true.
bool IsMessage() const { return variant_.Is<ParsedMessageValue>(); }
// Returns `true` if this value is an instance of a null value.
bool IsNull() const { return variant_.Is<NullValue>(); }
// Returns `true` if this value is an instance of an opaque value.
bool IsOpaque() const { return variant_.Is<OpaqueValue>(); }
// Returns `true` if this value is an instance of an optional value. If `true`
// is returned, it is implied that `IsOpaque()` would also return true.
bool IsOptional() const {
if (const auto* alternative = variant_.As<OpaqueValue>();
alternative != nullptr) {
return alternative->IsOptional();
}
return false;
}
// Returns `true` if this value is an instance of a parsed JSON list value. If
// `true` is returned, it is implied that `IsList()` would also return
// true.
bool IsParsedJsonList() const { return variant_.Is<ParsedJsonListValue>(); }
// Returns `true` if this value is an instance of a parsed JSON map value. If
// `true` is returned, it is implied that `IsMap()` would also return
// true.
bool IsParsedJsonMap() const { return variant_.Is<ParsedJsonMapValue>(); }
// Returns `true` if this value is an instance of a custom list value. If
// `true` is returned, it is implied that `IsList()` would also return
// true.
bool IsCustomList() const { return variant_.Is<CustomListValue>(); }
// Returns `true` if this value is an instance of a custom map value. If
// `true` is returned, it is implied that `IsMap()` would also return
// true.
bool IsCustomMap() const { return variant_.Is<CustomMapValue>(); }
// Returns `true` if this value is an instance of a parsed map field value. If
// `true` is returned, it is implied that `IsMap()` would also return
// true.
bool IsParsedMapField() const { return variant_.Is<ParsedMapFieldValue>(); }
// Returns `true` if this value is an instance of a parsed message value. If
// `true` is returned, it is implied that `IsMessage()` would also return
// true.
bool IsParsedMessage() const { return variant_.Is<ParsedMessageValue>(); }
// Returns `true` if this value is an instance of a parsed repeated field
// value. If `true` is returned, it is implied that `IsList()` would also
// return true.
bool IsParsedRepeatedField() const {
return variant_.Is<ParsedRepeatedFieldValue>();
}
// Returns `true` if this value is an instance of a custom struct value. If
// `true` is returned, it is implied that `IsStruct()` would also return
// true.
bool IsCustomStruct() const { return variant_.Is<CustomStructValue>(); }
// Returns `true` if this value is an instance of a string value.
bool IsString() const { return variant_.Is<StringValue>(); }
// Returns `true` if this value is an instance of a struct value.
bool IsStruct() const {
return variant_.Is<common_internal::LegacyStructValue>() ||
variant_.Is<CustomStructValue>() ||
variant_.Is<ParsedMessageValue>();
}
// Returns `true` if this value is an instance of a timestamp value.
bool IsTimestamp() const { return variant_.Is<TimestampValue>(); }
// Returns `true` if this value is an instance of a type value.
bool IsType() const { return variant_.Is<TypeValue>(); }
// Returns `true` if this value is an instance of a uint value.
bool IsUint() const { return variant_.Is<UintValue>(); }
// Returns `true` if this value is an instance of an unknown value.
bool IsUnknown() const { return variant_.Is<UnknownValue>(); }
// Convenience method for use with template metaprogramming. See
// `IsBool()`.
template <typename T>
std::enable_if_t<std::is_same_v<BoolValue, T>, bool> Is() const {
return IsBool();
}
// Convenience method for use with template metaprogramming. See
// `IsBytes()`.
template <typename T>
std::enable_if_t<std::is_same_v<BytesValue, T>, bool> Is() const {
return IsBytes();
}
// Convenience method for use with template metaprogramming. See
// `IsDouble()`.
template <typename T>
std::enable_if_t<std::is_same_v<DoubleValue, T>, bool> Is() const {
return IsDouble();
}
// Convenience method for use with template metaprogramming. See
// `IsDuration()`.
template <typename T>
std::enable_if_t<std::is_same_v<DurationValue, T>, bool> Is() const {
return IsDuration();
}
// Convenience method for use with template metaprogramming. See
// `IsError()`.
template <typename T>
std::enable_if_t<std::is_same_v<ErrorValue, T>, bool> Is() const {
return IsError();
}
// Convenience method for use with template metaprogramming. See
// `IsInt()`.
template <typename T>
std::enable_if_t<std::is_same_v<IntValue, T>, bool> Is() const {
return IsInt();
}
// Convenience method for use with template metaprogramming. See
// `IsList()`.
template <typename T>
std::enable_if_t<std::is_same_v<ListValue, T>, bool> Is() const {
return IsList();
}
// Convenience method for use with template metaprogramming. See
// `IsMap()`.
template <typename T>
std::enable_if_t<std::is_same_v<MapValue, T>, bool> Is() const {
return IsMap();
}
// Convenience method for use with template metaprogramming. See
// `IsMessage()`.
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>, bool> Is() const {
return IsMessage();
}
// Convenience method for use with template metaprogramming. See
// `IsNull()`.
template <typename T>
std::enable_if_t<std::is_same_v<NullValue, T>, bool> Is() const {
return IsNull();
}
// Convenience method for use with template metaprogramming. See
// `IsOpaque()`.
template <typename T>
std::enable_if_t<std::is_same_v<OpaqueValue, T>, bool> Is() const {
return IsOpaque();
}
// Convenience method for use with template metaprogramming. See
// `IsOptional()`.
template <typename T>
std::enable_if_t<std::is_same_v<OptionalValue, T>, bool> Is() const {
return IsOptional();
}
// Convenience method for use with template metaprogramming. See
// `IsParsedJsonList()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedJsonListValue, T>, bool> Is() const {
return IsParsedJsonList();
}
// Convenience method for use with template metaprogramming. See
// `IsParsedJsonMap()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedJsonMapValue, T>, bool> Is() const {
return IsParsedJsonMap();
}
// Convenience method for use with template metaprogramming. See
// `IsCustomList()`.
template <typename T>
std::enable_if_t<std::is_same_v<CustomListValue, T>, bool> Is() const {
return IsCustomList();
}
// Convenience method for use with template metaprogramming. See
// `IsCustomMap()`.
template <typename T>
std::enable_if_t<std::is_same_v<CustomMapValue, T>, bool> Is() const {
return IsCustomMap();
}
// Convenience method for use with template metaprogramming. See
// `IsParsedMapField()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMapFieldValue, T>, bool> Is() const {
return IsParsedMapField();
}
// Convenience method for use with template metaprogramming. See
// `IsParsedMessage()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, bool> Is() const {
return IsParsedMessage();
}
// Convenience method for use with template metaprogramming. See
// `IsParsedRepeatedField()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedRepeatedFieldValue, T>, bool> Is()
const {
return IsParsedRepeatedField();
}
// Convenience method for use with template metaprogramming. See
// `IsParsedStruct()`.
template <typename T>
std::enable_if_t<std::is_same_v<CustomStructValue, T>, bool> Is() const {
return IsCustomStruct();
}
// Convenience method for use with template metaprogramming. See
// `IsString()`.
template <typename T>
std::enable_if_t<std::is_same_v<StringValue, T>, bool> Is() const {
return IsString();
}
// Convenience method for use with template metaprogramming. See
// `IsStruct()`.
template <typename T>
std::enable_if_t<std::is_same_v<StructValue, T>, bool> Is() const {
return IsStruct();
}
// Convenience method for use with template metaprogramming. See
// `IsTimestamp()`.
template <typename T>
std::enable_if_t<std::is_same_v<TimestampValue, T>, bool> Is() const {
return IsTimestamp();
}
// Convenience method for use with template metaprogramming. See
// `IsType()`.
template <typename T>
std::enable_if_t<std::is_same_v<TypeValue, T>, bool> Is() const {
return IsType();
}
// Convenience method for use with template metaprogramming. See
// `IsUint()`.
template <typename T>
std::enable_if_t<std::is_same_v<UintValue, T>, bool> Is() const {
return IsUint();
}
// Convenience method for use with template metaprogramming. See
// `IsUnknown()`.
template <typename T>
std::enable_if_t<std::is_same_v<UnknownValue, T>, bool> Is() const {
return IsUnknown();
}
// Performs a checked cast from a value to a bool value,
// returning a non-empty optional with either a value or reference to the
// bool value. Otherwise an empty optional is returned.
absl::optional<BoolValue> AsBool() const {
if (const auto* alternative = variant_.As<BoolValue>();
alternative != nullptr) {
return *alternative;
}
return absl::nullopt;
}
// Performs a checked cast from a value to a bytes value,
// returning a non-empty optional with either a value or reference to the
// bytes value. Otherwise an empty optional is returned.
optional_ref<const BytesValue> AsBytes() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsBytes();
}
optional_ref<const BytesValue> AsBytes() const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<BytesValue> AsBytes() &&;
absl::optional<BytesValue> AsBytes() const&& {
return common_internal::AsOptional(AsBytes());
}
// Performs a checked cast from a value to a double value,
// returning a non-empty optional with either a value or reference to the
// double value. Otherwise an empty optional is returned.
absl::optional<DoubleValue> AsDouble() const;
// Performs a checked cast from a value to a duration value,
// returning a non-empty optional with either a value or reference to the
// duration value. Otherwise an empty optional is returned.
absl::optional<DurationValue> AsDuration() const;
// Performs a checked cast from a value to an error value,
// returning a non-empty optional with either a value or reference to the
// error value. Otherwise an empty optional is returned.
optional_ref<const ErrorValue> AsError() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsError();
}
optional_ref<const ErrorValue> AsError() const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<ErrorValue> AsError() &&;
absl::optional<ErrorValue> AsError() const&& {
return common_internal::AsOptional(AsError());
}
// Performs a checked cast from a value to an int value,
// returning a non-empty optional with either a value or reference to the
// int value. Otherwise an empty optional is returned.
absl::optional<IntValue> AsInt() const;
// Performs a checked cast from a value to a list value,
// returning a non-empty optional with either a value or reference to the
// list value. Otherwise an empty optional is returned.
absl::optional<ListValue> AsList() & { return std::as_const(*this).AsList(); }
absl::optional<ListValue> AsList() const&;
absl::optional<ListValue> AsList() &&;
absl::optional<ListValue> AsList() const&& {
return common_internal::AsOptional(AsList());
}
// Performs a checked cast from a value to a map value,
// returning a non-empty optional with either a value or reference to the
// map value. Otherwise an empty optional is returned.
absl::optional<MapValue> AsMap() & { return std::as_const(*this).AsMap(); }
absl::optional<MapValue> AsMap() const&;
absl::optional<MapValue> AsMap() &&;
absl::optional<MapValue> AsMap() const&& {
return common_internal::AsOptional(AsMap());
}
// Performs a checked cast from a value to a message value,
// returning a non-empty optional with either a value or reference to the
// message value. Otherwise an empty optional is returned.
absl::optional<MessageValue> AsMessage() & {
return std::as_const(*this).AsMessage();
}
absl::optional<MessageValue> AsMessage() const&;
absl::optional<MessageValue> AsMessage() &&;
absl::optional<MessageValue> AsMessage() const&& {
return common_internal::AsOptional(AsMessage());
}
// Performs a checked cast from a value to a null value,
// returning a non-empty optional with either a value or reference to the
// null value. Otherwise an empty optional is returned.
absl::optional<NullValue> AsNull() const;
// Performs a checked cast from a value to an opaque value,
// returning a non-empty optional with either a value or reference to the
// opaque value. Otherwise an empty optional is returned.
optional_ref<const OpaqueValue> AsOpaque() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsOpaque();
}
optional_ref<const OpaqueValue> AsOpaque()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<OpaqueValue> AsOpaque() &&;
absl::optional<OpaqueValue> AsOpaque() const&& {
return common_internal::AsOptional(AsOpaque());
}
// Performs a checked cast from a value to an optional value,
// returning a non-empty optional with either a value or reference to the
// optional value. Otherwise an empty optional is returned.
optional_ref<const OptionalValue> AsOptional() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsOptional();
}
optional_ref<const OptionalValue> AsOptional()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<OptionalValue> AsOptional() &&;
absl::optional<OptionalValue> AsOptional() const&& {
return common_internal::AsOptional(AsOptional());
}
// Performs a checked cast from a value to a parsed JSON list value,
// returning a non-empty optional with either a value or reference to the
// parsed message value. Otherwise an empty optional is returned.
optional_ref<const ParsedJsonListValue> AsParsedJsonList() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsParsedJsonList();
}
optional_ref<const ParsedJsonListValue> AsParsedJsonList()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<ParsedJsonListValue> AsParsedJsonList() &&;
absl::optional<ParsedJsonListValue> AsParsedJsonList() const&& {
return common_internal::AsOptional(AsParsedJsonList());
}
// Performs a checked cast from a value to a parsed JSON map value,
// returning a non-empty optional with either a value or reference to the
// parsed message value. Otherwise an empty optional is returned.
optional_ref<const ParsedJsonMapValue> AsParsedJsonMap() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsParsedJsonMap();
}
optional_ref<const ParsedJsonMapValue> AsParsedJsonMap()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<ParsedJsonMapValue> AsParsedJsonMap() &&;
absl::optional<ParsedJsonMapValue> AsParsedJsonMap() const&& {
return common_internal::AsOptional(AsParsedJsonMap());
}
// Performs a checked cast from a value to a custom list value,
// returning a non-empty optional with either a value or reference to the
// custom list value. Otherwise an empty optional is returned.
optional_ref<const CustomListValue> AsCustomList() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsCustomList();
}
optional_ref<const CustomListValue> AsCustomList()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<CustomListValue> AsCustomList() &&;
absl::optional<CustomListValue> AsCustomList() const&& {
return common_internal::AsOptional(AsCustomList());
}
// Performs a checked cast from a value to a custom map value,
// returning a non-empty optional with either a value or reference to the
// custom map value. Otherwise an empty optional is returned.
optional_ref<const CustomMapValue> AsCustomMap() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsCustomMap();
}
optional_ref<const CustomMapValue> AsCustomMap()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<CustomMapValue> AsCustomMap() &&;
absl::optional<CustomMapValue> AsCustomMap() const&& {
return common_internal::AsOptional(AsCustomMap());
}
// Performs a checked cast from a value to a parsed map field value,
// returning a non-empty optional with either a value or reference to the
// parsed map field value. Otherwise an empty optional is returned.
optional_ref<const ParsedMapFieldValue> AsParsedMapField() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsParsedMapField();
}
optional_ref<const ParsedMapFieldValue> AsParsedMapField()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<ParsedMapFieldValue> AsParsedMapField() &&;
absl::optional<ParsedMapFieldValue> AsParsedMapField() const&& {
return common_internal::AsOptional(AsParsedMapField());
}
// Performs a checked cast from a value to a parsed message value,
// returning a non-empty optional with either a value or reference to the
// parsed message value. Otherwise an empty optional is returned.
optional_ref<const ParsedMessageValue> AsParsedMessage() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsParsedMessage();
}
optional_ref<const ParsedMessageValue> AsParsedMessage()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<ParsedMessageValue> AsParsedMessage() &&;
absl::optional<ParsedMessageValue> AsParsedMessage() const&& {
return common_internal::AsOptional(AsParsedMessage());
}
// Performs a checked cast from a value to a parsed repeated field value,
// returning a non-empty optional with either a value or reference to the
// parsed repeated field value. Otherwise an empty optional is returned.
optional_ref<const ParsedRepeatedFieldValue> AsParsedRepeatedField() &
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).AsParsedRepeatedField();
}
optional_ref<const ParsedRepeatedFieldValue> AsParsedRepeatedField()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::optional<ParsedRepeatedFieldValue> AsParsedRepeatedField() &&;
absl::optional<ParsedRepeatedFieldValue> AsParsedRepeatedField() const&& {