17
17
#include < cstdint>
18
18
#include < limits>
19
19
#include < string>
20
+ #include < type_traits>
20
21
#include < utility>
21
22
#include < vector>
22
23
24
+ #include " google/protobuf/empty.pb.h"
23
25
#include " absl/base/nullability.h"
24
26
#include " absl/base/optimization.h"
25
27
#include " absl/log/absl_check.h"
33
35
#include " common/memory.h"
34
36
#include " common/value.h"
35
37
#include " extensions/protobuf/internal/qualify.h"
38
+ #include " internal/empty_descriptors.h"
36
39
#include " internal/json.h"
37
40
#include " internal/message_equality.h"
38
41
#include " internal/status_macros.h"
42
45
#include " google/protobuf/descriptor.h"
43
46
#include " google/protobuf/io/zero_copy_stream.h"
44
47
#include " google/protobuf/message.h"
48
+ #include " google/protobuf/message_lite.h"
45
49
46
50
namespace cel {
47
51
52
+ namespace {
53
+
48
54
using ::cel::well_known_types::ValueReflection;
49
55
56
+ template <typename T>
57
+ std::enable_if_t <std::is_base_of_v<google::protobuf::Message, T>,
58
+ absl::Nonnull<const google::protobuf::Message*>>
59
+ EmptyParsedMessageValue () {
60
+ return &T::default_instance ();
61
+ }
62
+
63
+ template <typename T>
64
+ std::enable_if_t <
65
+ std::conjunction_v<std::is_base_of<google::protobuf::MessageLite, T>,
66
+ std::negation<std::is_base_of<google::protobuf::Message, T>>>,
67
+ absl::Nonnull<const google::protobuf::Message*>>
68
+ EmptyParsedMessageValue () {
69
+ return internal::GetEmptyDefaultInstance ();
70
+ }
71
+
72
+ } // namespace
73
+
74
+ ParsedMessageValue::ParsedMessageValue ()
75
+ : value_(EmptyParsedMessageValue<google::protobuf::Empty>()),
76
+ arena_ (nullptr ) {}
77
+
50
78
bool ParsedMessageValue::IsZeroValue () const {
51
- ABSL_DCHECK (*this );
52
- if (ABSL_PREDICT_FALSE (value_ == nullptr )) {
53
- return true ;
54
- }
55
79
const auto * reflection = GetReflection ();
56
80
if (!reflection->GetUnknownFields (*value_).empty ()) {
57
81
return false ;
@@ -62,9 +86,6 @@ bool ParsedMessageValue::IsZeroValue() const {
62
86
}
63
87
64
88
std::string ParsedMessageValue::DebugString () const {
65
- if (ABSL_PREDICT_FALSE (value_ == nullptr )) {
66
- return " INVALID" ;
67
- }
68
89
return absl::StrCat (*value_);
69
90
}
70
91
@@ -75,11 +96,6 @@ absl::Status ParsedMessageValue::SerializeTo(
75
96
ABSL_DCHECK (descriptor_pool != nullptr );
76
97
ABSL_DCHECK (message_factory != nullptr );
77
98
ABSL_DCHECK (output != nullptr );
78
- ABSL_DCHECK (*this );
79
-
80
- if (ABSL_PREDICT_FALSE (value_ == nullptr )) {
81
- return absl::OkStatus ();
82
- }
83
99
84
100
if (!value_->SerializePartialToZeroCopyStream (output)) {
85
101
return absl::UnknownError (
@@ -97,16 +113,11 @@ absl::Status ParsedMessageValue::ConvertToJson(
97
113
ABSL_DCHECK (json != nullptr );
98
114
ABSL_DCHECK_EQ (json->GetDescriptor ()->well_known_type (),
99
115
google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE);
100
- ABSL_DCHECK (*this );
101
116
102
117
ValueReflection value_reflection;
103
118
CEL_RETURN_IF_ERROR (value_reflection.Initialize (json->GetDescriptor ()));
104
119
google::protobuf::Message* json_object = value_reflection.MutableStructValue (json);
105
120
106
- if (ABSL_PREDICT_FALSE (value_ == nullptr )) {
107
- json_object->Clear ();
108
- return absl::OkStatus ();
109
- }
110
121
return internal::MessageToJson (*value_, descriptor_pool, message_factory,
111
122
json_object);
112
123
}
@@ -120,12 +131,7 @@ absl::Status ParsedMessageValue::ConvertToJsonObject(
120
131
ABSL_DCHECK (json != nullptr );
121
132
ABSL_DCHECK_EQ (json->GetDescriptor ()->well_known_type (),
122
133
google::protobuf::Descriptor::WELLKNOWNTYPE_STRUCT);
123
- ABSL_DCHECK (*this );
124
134
125
- if (ABSL_PREDICT_FALSE (value_ == nullptr )) {
126
- json->Clear ();
127
- return absl::OkStatus ();
128
- }
129
135
return internal::MessageToJson (*value_, descriptor_pool, message_factory,
130
136
json);
131
137
}
@@ -135,7 +141,11 @@ absl::Status ParsedMessageValue::Equal(
135
141
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
136
142
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
137
143
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
138
- ABSL_DCHECK (*this );
144
+ ABSL_DCHECK (descriptor_pool != nullptr );
145
+ ABSL_DCHECK (message_factory != nullptr );
146
+ ABSL_DCHECK (arena != nullptr );
147
+ ABSL_DCHECK (result != nullptr );
148
+
139
149
if (auto other_message = other.AsParsedMessage (); other_message) {
140
150
CEL_ASSIGN_OR_RETURN (
141
151
auto equal, internal::MessageEquals (*value_, **other_message,
@@ -154,10 +164,8 @@ absl::Status ParsedMessageValue::Equal(
154
164
155
165
ParsedMessageValue ParsedMessageValue::Clone (
156
166
absl::Nonnull<google::protobuf::Arena*> arena) const {
157
- ABSL_DCHECK (*this );
158
- if (ABSL_PREDICT_FALSE (value_ == nullptr )) {
159
- return ParsedMessageValue ();
160
- }
167
+ ABSL_DCHECK (arena != nullptr );
168
+
161
169
if (arena_ == arena) {
162
170
return *this ;
163
171
}
@@ -171,6 +179,11 @@ absl::Status ParsedMessageValue::GetFieldByName(
171
179
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
172
180
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
173
181
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
182
+ ABSL_DCHECK (descriptor_pool != nullptr );
183
+ ABSL_DCHECK (message_factory != nullptr );
184
+ ABSL_DCHECK (arena != nullptr );
185
+ ABSL_DCHECK (result != nullptr );
186
+
174
187
const auto * descriptor = GetDescriptor ();
175
188
const auto * field = descriptor->FindFieldByName (name);
176
189
if (field == nullptr ) {
@@ -190,6 +203,11 @@ absl::Status ParsedMessageValue::GetFieldByNumber(
190
203
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
191
204
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
192
205
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
206
+ ABSL_DCHECK (descriptor_pool != nullptr );
207
+ ABSL_DCHECK (message_factory != nullptr );
208
+ ABSL_DCHECK (arena != nullptr );
209
+ ABSL_DCHECK (result != nullptr );
210
+
193
211
const auto * descriptor = GetDescriptor ();
194
212
if (number < std::numeric_limits<int32_t >::min () ||
195
213
number > std::numeric_limits<int32_t >::max ()) {
@@ -238,10 +256,10 @@ absl::Status ParsedMessageValue::ForEachField(
238
256
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
239
257
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
240
258
absl::Nonnull<google::protobuf::Arena*> arena) const {
241
- ABSL_DCHECK (* this );
242
- if ( ABSL_PREDICT_FALSE (value_ == nullptr )) {
243
- return absl::OkStatus ( );
244
- }
259
+ ABSL_DCHECK (descriptor_pool != nullptr );
260
+ ABSL_DCHECK (message_factory != nullptr );
261
+ ABSL_DCHECK (arena != nullptr );
262
+
245
263
std::vector<const google::protobuf::FieldDescriptor*> fields;
246
264
const auto * reflection = GetReflection ();
247
265
reflection->ListFields (*value_, &fields);
@@ -322,7 +340,13 @@ absl::Status ParsedMessageValue::Qualify(
322
340
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
323
341
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result,
324
342
absl::Nonnull<int *> count) const {
325
- ABSL_DCHECK (*this );
343
+ ABSL_DCHECK (!qualifiers.empty ());
344
+ ABSL_DCHECK (descriptor_pool != nullptr );
345
+ ABSL_DCHECK (message_factory != nullptr );
346
+ ABSL_DCHECK (arena != nullptr );
347
+ ABSL_DCHECK (result != nullptr );
348
+ ABSL_DCHECK (count != nullptr );
349
+
326
350
if (ABSL_PREDICT_FALSE (qualifiers.empty ())) {
327
351
return absl::InvalidArgumentError (" invalid select qualifier path." );
328
352
}
@@ -357,13 +381,21 @@ absl::Status ParsedMessageValue::GetField(
357
381
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
358
382
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
359
383
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const {
384
+ ABSL_DCHECK (field != nullptr );
385
+ ABSL_DCHECK (descriptor_pool != nullptr );
386
+ ABSL_DCHECK (message_factory != nullptr );
387
+ ABSL_DCHECK (arena != nullptr );
388
+ ABSL_DCHECK (result != nullptr );
389
+
360
390
*result = Value::WrapField (unboxing_options, value_, field, descriptor_pool,
361
391
message_factory, arena);
362
392
return absl::OkStatus ();
363
393
}
364
394
365
395
bool ParsedMessageValue::HasField (
366
396
absl::Nonnull<const google::protobuf::FieldDescriptor*> field) const {
397
+ ABSL_DCHECK (field != nullptr );
398
+
367
399
const auto * reflection = GetReflection ();
368
400
if (field->is_map () || field->is_repeated ()) {
369
401
return reflection->FieldSize (*value_, field) > 0 ;
0 commit comments