/proc/self/cwd/common/values/value_builder.cc
Line | Count | Source |
1 | | // Copyright 2024 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 <cstdint> |
17 | | #include <cstring> |
18 | | #include <memory> |
19 | | #include <new> |
20 | | #include <string> |
21 | | #include <type_traits> |
22 | | #include <utility> |
23 | | #include <vector> |
24 | | |
25 | | #include "absl/base/call_once.h" |
26 | | #include "absl/base/casts.h" |
27 | | #include "absl/base/nullability.h" |
28 | | #include "absl/base/optimization.h" |
29 | | #include "absl/container/flat_hash_map.h" |
30 | | #include "absl/hash/hash.h" |
31 | | #include "absl/log/absl_check.h" |
32 | | #include "absl/status/status.h" |
33 | | #include "absl/status/statusor.h" |
34 | | #include "absl/strings/str_cat.h" |
35 | | #include "absl/strings/str_join.h" |
36 | | #include "absl/types/optional.h" |
37 | | #include "absl/types/span.h" |
38 | | #include "common/allocator.h" |
39 | | #include "common/legacy_value.h" |
40 | | #include "common/native_type.h" |
41 | | #include "common/type.h" |
42 | | #include "common/value.h" |
43 | | #include "common/value_kind.h" |
44 | | #include "common/values/list_value_builder.h" |
45 | | #include "common/values/map_value_builder.h" |
46 | | #include "eval/public/cel_value.h" |
47 | | #include "internal/casts.h" |
48 | | #include "internal/manual.h" |
49 | | #include "internal/status_macros.h" |
50 | | #include "internal/well_known_types.h" |
51 | | #include "google/protobuf/arena.h" |
52 | | #include "google/protobuf/descriptor.h" |
53 | | #include "google/protobuf/message.h" |
54 | | |
55 | | namespace cel { |
56 | | |
57 | | namespace common_internal { |
58 | | |
59 | | namespace { |
60 | | |
61 | | using ::cel::well_known_types::ListValueReflection; |
62 | | using ::cel::well_known_types::StructReflection; |
63 | | using ::cel::well_known_types::ValueReflection; |
64 | | using ::google::api::expr::runtime::CelValue; |
65 | | |
66 | | using ValueVector = std::vector<Value, ArenaAllocator<Value>>; |
67 | | |
68 | 9.41M | absl::Status CheckListElement(const Value& value) { |
69 | 9.41M | if (auto error_value = value.AsError(); ABSL_PREDICT_FALSE(error_value)) { |
70 | 0 | return error_value->ToStatus(); |
71 | 0 | } |
72 | 9.41M | if (auto unknown_value = value.AsUnknown(); |
73 | 9.41M | ABSL_PREDICT_FALSE(unknown_value)) { |
74 | 0 | return absl::InvalidArgumentError("cannot add unknown value to list"); |
75 | 0 | } |
76 | 9.41M | return absl::OkStatus(); |
77 | 9.41M | } |
78 | | |
79 | | template <typename Vector> |
80 | | absl::Status ListValueToJsonArray( |
81 | | const Vector& vector, |
82 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
83 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
84 | 415k | google::protobuf::Message* absl_nonnull json) { |
85 | 415k | ABSL_DCHECK(descriptor_pool != nullptr); |
86 | 415k | ABSL_DCHECK(message_factory != nullptr); |
87 | 415k | ABSL_DCHECK(json != nullptr); |
88 | 415k | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
89 | 415k | google::protobuf::Descriptor::WELLKNOWNTYPE_LISTVALUE); |
90 | | |
91 | 415k | ListValueReflection reflection; |
92 | 415k | CEL_RETURN_IF_ERROR(reflection.Initialize(json->GetDescriptor())); |
93 | | |
94 | 415k | json->Clear(); |
95 | | |
96 | 415k | if (vector.empty()) { |
97 | 0 | return absl::OkStatus(); |
98 | 0 | } |
99 | | |
100 | 5.39M | for (const auto& element : vector) { |
101 | 5.39M | CEL_RETURN_IF_ERROR(element->ConvertToJson(descriptor_pool, message_factory, |
102 | 5.39M | reflection.AddValues(json))); |
103 | 5.39M | } |
104 | 414k | return absl::OkStatus(); |
105 | 415k | } |
106 | | |
107 | | template <typename Vector> |
108 | | absl::Status ListValueToJson( |
109 | | const Vector& vector, |
110 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
111 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
112 | | google::protobuf::Message* absl_nonnull json) { |
113 | | ABSL_DCHECK(descriptor_pool != nullptr); |
114 | | ABSL_DCHECK(message_factory != nullptr); |
115 | | ABSL_DCHECK(json != nullptr); |
116 | | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
117 | | google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE); |
118 | | |
119 | | ValueReflection reflection; |
120 | | CEL_RETURN_IF_ERROR(reflection.Initialize(json->GetDescriptor())); |
121 | | return ListValueToJsonArray(vector, descriptor_pool, message_factory, |
122 | | reflection.MutableListValue(json)); |
123 | | } |
124 | | |
125 | | class CompatListValueImplIterator final : public ValueIterator { |
126 | | public: |
127 | | explicit CompatListValueImplIterator(absl::Span<const Value> elements) |
128 | 286k | : elements_(elements) {} |
129 | | |
130 | 2.98k | bool HasNext() override { return index_ < elements_.size(); } |
131 | | |
132 | | absl::Status Next(const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
133 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
134 | | google::protobuf::Arena* absl_nonnull arena, |
135 | 2.98k | Value* absl_nonnull result) override { |
136 | 2.98k | if (ABSL_PREDICT_FALSE(index_ >= elements_.size())) { |
137 | 0 | return absl::FailedPreconditionError( |
138 | 0 | "ValueManager::Next called after ValueManager::HasNext returned " |
139 | 0 | "false"); |
140 | 0 | } |
141 | 2.98k | *result = elements_[index_++]; |
142 | 2.98k | return absl::OkStatus(); |
143 | 2.98k | } |
144 | | |
145 | | absl::StatusOr<bool> Next1( |
146 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
147 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
148 | | google::protobuf::Arena* absl_nonnull arena, |
149 | 1.99M | Value* absl_nonnull key_or_value) override { |
150 | 1.99M | ABSL_DCHECK(descriptor_pool != nullptr); |
151 | 1.99M | ABSL_DCHECK(message_factory != nullptr); |
152 | 1.99M | ABSL_DCHECK(arena != nullptr); |
153 | 1.99M | ABSL_DCHECK(key_or_value != nullptr); |
154 | | |
155 | 1.99M | if (index_ >= elements_.size()) { |
156 | 282k | return false; |
157 | 282k | } |
158 | 1.71M | *key_or_value = elements_[index_]; |
159 | 1.71M | ++index_; |
160 | 1.71M | return true; |
161 | 1.99M | } |
162 | | |
163 | | absl::StatusOr<bool> Next2( |
164 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
165 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
166 | | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull key, |
167 | 0 | Value* absl_nullable value) override { |
168 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
169 | 0 | ABSL_DCHECK(message_factory != nullptr); |
170 | 0 | ABSL_DCHECK(arena != nullptr); |
171 | 0 | ABSL_DCHECK(key != nullptr); |
172 | |
|
173 | 0 | if (index_ >= elements_.size()) { |
174 | 0 | return false; |
175 | 0 | } |
176 | 0 | if (value != nullptr) { |
177 | 0 | *value = elements_[index_]; |
178 | 0 | } |
179 | 0 | *key = IntValue(index_++); |
180 | 0 | return true; |
181 | 0 | } |
182 | | |
183 | | private: |
184 | | const absl::Span<const Value> elements_; |
185 | | size_t index_ = 0; |
186 | | }; |
187 | | |
188 | | struct ValueFormatter { |
189 | | void operator()(std::string* out, |
190 | 0 | const std::pair<const Value, Value>& value) const { |
191 | 0 | (*this)(out, value.first); |
192 | 0 | out->append(": "); |
193 | 0 | (*this)(out, value.second); |
194 | 0 | } |
195 | | |
196 | 0 | void operator()(std::string* out, const Value& value) const { |
197 | 0 | out->append(value.DebugString()); |
198 | 0 | } |
199 | | }; |
200 | | |
201 | | class ListValueBuilderImpl final : public ListValueBuilder { |
202 | | public: |
203 | | explicit ListValueBuilderImpl(google::protobuf::Arena* absl_nonnull arena) |
204 | 2.05M | : arena_(arena) { |
205 | 2.05M | elements_.Construct(arena); |
206 | 2.05M | } |
207 | | |
208 | 9.41M | absl::Status Add(Value value) override { |
209 | 9.41M | CEL_RETURN_IF_ERROR(CheckListElement(value)); |
210 | 9.41M | UnsafeAdd(std::move(value)); |
211 | 9.41M | return absl::OkStatus(); |
212 | 9.41M | } |
213 | | |
214 | 9.41M | void UnsafeAdd(Value value) override { |
215 | 9.41M | ABSL_DCHECK_OK(CheckListElement(value)); |
216 | 9.41M | elements_->emplace_back(std::move(value)); |
217 | 9.41M | } |
218 | | |
219 | 0 | size_t Size() const override { return elements_->size(); } |
220 | | |
221 | 2.05M | void Reserve(size_t capacity) override { elements_->reserve(capacity); } |
222 | | |
223 | | ListValue Build() && override; |
224 | | |
225 | | CustomListValue BuildCustom() &&; |
226 | | |
227 | | const CompatListValue* absl_nonnull BuildCompat() &&; |
228 | | |
229 | | const CompatListValue* absl_nonnull BuildCompatAt( |
230 | | void* absl_nonnull address) &&; |
231 | | |
232 | | private: |
233 | | google::protobuf::Arena* absl_nonnull const arena_; |
234 | | internal::Manual<ValueVector> elements_; |
235 | | }; |
236 | | |
237 | | class CompatListValueImpl final : public CompatListValue { |
238 | | public: |
239 | | explicit CompatListValueImpl(ValueVector&& elements) |
240 | 1.38M | : elements_(std::move(elements)) {} |
241 | | |
242 | 0 | google::protobuf::Arena* GetOwningArena() const override { |
243 | 0 | return elements_.get_allocator().arena(); |
244 | 0 | } |
245 | | |
246 | 0 | std::string DebugString() const override { |
247 | 0 | return absl::StrCat("[", absl::StrJoin(elements_, ", ", ValueFormatter{}), |
248 | 0 | "]"); |
249 | 0 | } |
250 | | |
251 | | absl::Status ConvertToJsonArray( |
252 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
253 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
254 | 415k | google::protobuf::Message* absl_nonnull json) const override { |
255 | 415k | return ListValueToJsonArray(elements_, descriptor_pool, message_factory, |
256 | 415k | json); |
257 | 415k | } |
258 | | |
259 | 0 | CustomListValue Clone(google::protobuf::Arena* absl_nonnull arena) const override { |
260 | 0 | ABSL_DCHECK(arena != nullptr); |
261 | |
|
262 | 0 | ListValueBuilderImpl builder(arena); |
263 | 0 | builder.Reserve(elements_.size()); |
264 | 0 | for (const auto& element : elements_) { |
265 | 0 | builder.UnsafeAdd(element.Clone(arena)); |
266 | 0 | } |
267 | 0 | return std::move(builder).BuildCustom(); |
268 | 0 | } |
269 | | |
270 | 635k | size_t Size() const override { return elements_.size(); } |
271 | | |
272 | | absl::Status ForEach( |
273 | | ForEachWithIndexCallback callback, |
274 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
275 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
276 | 182k | google::protobuf::Arena* absl_nonnull arena) const override { |
277 | 182k | const size_t size = elements_.size(); |
278 | 347k | for (size_t i = 0; i < size; ++i) { |
279 | 204k | CEL_ASSIGN_OR_RETURN(auto ok, callback(i, elements_[i])); |
280 | 204k | if (!ok) { |
281 | 40.0k | break; |
282 | 40.0k | } |
283 | 204k | } |
284 | 182k | return absl::OkStatus(); |
285 | 182k | } |
286 | | |
287 | 286k | absl::StatusOr<absl_nonnull ValueIteratorPtr> NewIterator() const override { |
288 | 286k | return std::make_unique<CompatListValueImplIterator>( |
289 | 286k | absl::MakeConstSpan(elements_)); |
290 | 286k | } |
291 | | |
292 | 0 | CelValue operator[](int index) const override { |
293 | 0 | return Get(elements_.get_allocator().arena(), index); |
294 | 0 | } |
295 | | |
296 | | // Like `operator[](int)` above, but also accepts an arena. Prefer calling |
297 | | // this variant if the arena is known. |
298 | 0 | CelValue Get(google::protobuf::Arena* arena, int index) const override { |
299 | 0 | if (arena == nullptr) { |
300 | 0 | arena = elements_.get_allocator().arena(); |
301 | 0 | } |
302 | 0 | if (ABSL_PREDICT_FALSE(index < 0 || index >= size())) { |
303 | 0 | return CelValue::CreateError(google::protobuf::Arena::Create<absl::Status>( |
304 | 0 | arena, common_internal::MakeIndexOutOfBoundsError(index))); |
305 | 0 | } |
306 | 0 | return common_internal::UnsafeLegacyValue( |
307 | 0 | elements_[index], |
308 | 0 | /*stable=*/true, |
309 | 0 | arena != nullptr ? arena : elements_.get_allocator().arena()); |
310 | 0 | } |
311 | | |
312 | 0 | int size() const override { return static_cast<int>(Size()); } |
313 | | |
314 | | protected: |
315 | | absl::Status Get(size_t index, |
316 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
317 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
318 | | google::protobuf::Arena* absl_nonnull arena, |
319 | 1.57M | Value* absl_nonnull result) const override { |
320 | 1.57M | if (index >= elements_.size()) { |
321 | 0 | *result = IndexOutOfBoundsError(index, arena); |
322 | 1.57M | } else { |
323 | 1.57M | *result = elements_[index]; |
324 | 1.57M | } |
325 | 1.57M | return absl::OkStatus(); |
326 | 1.57M | } |
327 | | |
328 | | private: |
329 | | const ValueVector elements_; |
330 | | }; |
331 | | |
332 | 2.05M | ListValue ListValueBuilderImpl::Build() && { |
333 | 2.05M | if (elements_->empty()) { |
334 | 666k | return ListValue(); |
335 | 666k | } |
336 | 1.38M | return std::move(*this).BuildCustom(); |
337 | 2.05M | } |
338 | | |
339 | 1.38M | CustomListValue ListValueBuilderImpl::BuildCustom() && { |
340 | 1.38M | if (elements_->empty()) { |
341 | 0 | return CustomListValue(EmptyCompatListValue(), arena_); |
342 | 0 | } |
343 | 1.38M | return CustomListValue(std::move(*this).BuildCompat(), arena_); |
344 | 1.38M | } |
345 | | |
346 | 1.38M | const CompatListValue* absl_nonnull ListValueBuilderImpl::BuildCompat() && { |
347 | 1.38M | if (elements_->empty()) { |
348 | 0 | return EmptyCompatListValue(); |
349 | 0 | } |
350 | 1.38M | return std::move(*this).BuildCompatAt(arena_->AllocateAligned( |
351 | 1.38M | sizeof(CompatListValueImpl), alignof(CompatListValueImpl))); |
352 | 1.38M | } |
353 | | |
354 | | const CompatListValue* absl_nonnull ListValueBuilderImpl::BuildCompatAt( |
355 | 1.38M | void* absl_nonnull address) && { |
356 | 1.38M | CompatListValueImpl* absl_nonnull impl = |
357 | 1.38M | ::new (address) CompatListValueImpl(std::move(*elements_)); |
358 | 1.38M | return impl; |
359 | 1.38M | } |
360 | | |
361 | | class MutableCompatListValueImpl final : public MutableCompatListValue { |
362 | | public: |
363 | | explicit MutableCompatListValueImpl(google::protobuf::Arena* absl_nonnull arena) |
364 | 0 | : elements_(arena) {} |
365 | | |
366 | 0 | google::protobuf::Arena* GetOwningArena() const override { |
367 | 0 | return elements_.get_allocator().arena(); |
368 | 0 | } |
369 | | |
370 | 0 | std::string DebugString() const override { |
371 | 0 | return absl::StrCat("[", absl::StrJoin(elements_, ", ", ValueFormatter{}), |
372 | 0 | "]"); |
373 | 0 | } |
374 | | |
375 | | absl::Status ConvertToJsonArray( |
376 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
377 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
378 | 0 | google::protobuf::Message* absl_nonnull json) const override { |
379 | 0 | return ListValueToJsonArray(elements_, descriptor_pool, message_factory, |
380 | 0 | json); |
381 | 0 | } |
382 | | |
383 | 0 | CustomListValue Clone(google::protobuf::Arena* absl_nonnull arena) const override { |
384 | 0 | ABSL_DCHECK(arena != nullptr); |
385 | |
|
386 | 0 | ListValueBuilderImpl builder(arena); |
387 | 0 | builder.Reserve(elements_.size()); |
388 | 0 | for (const auto& element : elements_) { |
389 | 0 | builder.UnsafeAdd(element.Clone(arena)); |
390 | 0 | } |
391 | 0 | return std::move(builder).BuildCustom(); |
392 | 0 | } |
393 | | |
394 | 0 | size_t Size() const override { return elements_.size(); } |
395 | | |
396 | | absl::Status ForEach( |
397 | | ForEachWithIndexCallback callback, |
398 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
399 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
400 | 0 | google::protobuf::Arena* absl_nonnull arena) const override { |
401 | 0 | const size_t size = elements_.size(); |
402 | 0 | for (size_t i = 0; i < size; ++i) { |
403 | 0 | CEL_ASSIGN_OR_RETURN(auto ok, callback(i, elements_[i])); |
404 | 0 | if (!ok) { |
405 | 0 | break; |
406 | 0 | } |
407 | 0 | } |
408 | 0 | return absl::OkStatus(); |
409 | 0 | } |
410 | | |
411 | 0 | absl::StatusOr<absl_nonnull ValueIteratorPtr> NewIterator() const override { |
412 | 0 | return std::make_unique<CompatListValueImplIterator>( |
413 | 0 | absl::MakeConstSpan(elements_)); |
414 | 0 | } |
415 | | |
416 | 0 | CelValue operator[](int index) const override { |
417 | 0 | return Get(elements_.get_allocator().arena(), index); |
418 | 0 | } |
419 | | |
420 | | // Like `operator[](int)` above, but also accepts an arena. Prefer calling |
421 | | // this variant if the arena is known. |
422 | 0 | CelValue Get(google::protobuf::Arena* arena, int index) const override { |
423 | 0 | if (arena == nullptr) { |
424 | 0 | arena = elements_.get_allocator().arena(); |
425 | 0 | } |
426 | 0 | if (ABSL_PREDICT_FALSE(index < 0 || index >= size())) { |
427 | 0 | return CelValue::CreateError(google::protobuf::Arena::Create<absl::Status>( |
428 | 0 | arena, common_internal::MakeIndexOutOfBoundsError(index))); |
429 | 0 | } |
430 | 0 | return common_internal::UnsafeLegacyValue( |
431 | 0 | elements_[index], /*stable=*/false, |
432 | 0 | arena != nullptr ? arena : elements_.get_allocator().arena()); |
433 | 0 | } |
434 | | |
435 | 0 | int size() const override { return static_cast<int>(Size()); } |
436 | | |
437 | 0 | absl::Status Append(Value value) const override { |
438 | 0 | CEL_RETURN_IF_ERROR(CheckListElement(value)); |
439 | 0 | elements_.emplace_back(std::move(value)); |
440 | 0 | return absl::OkStatus(); |
441 | 0 | } |
442 | | |
443 | 0 | void Reserve(size_t capacity) const override { elements_.reserve(capacity); } |
444 | | |
445 | | protected: |
446 | | absl::Status Get(size_t index, |
447 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
448 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
449 | | google::protobuf::Arena* absl_nonnull arena, |
450 | 0 | Value* absl_nonnull result) const override { |
451 | 0 | if (index >= elements_.size()) { |
452 | 0 | *result = IndexOutOfBoundsError(index, arena); |
453 | 0 | } else { |
454 | 0 | *result = elements_[index]; |
455 | 0 | } |
456 | 0 | return absl::OkStatus(); |
457 | 0 | } |
458 | | |
459 | | private: |
460 | | mutable ValueVector elements_; |
461 | | }; |
462 | | |
463 | | } // namespace |
464 | | |
465 | | absl::StatusOr<const CompatListValue* absl_nonnull> MakeCompatListValue( |
466 | | const CustomListValue& value, |
467 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
468 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
469 | 0 | google::protobuf::Arena* absl_nonnull arena) { |
470 | 0 | ListValueBuilderImpl builder(arena); |
471 | 0 | builder.Reserve(value.Size()); |
472 | |
|
473 | 0 | CEL_RETURN_IF_ERROR(value.ForEach( |
474 | 0 | [&](const Value& element) -> absl::StatusOr<bool> { |
475 | 0 | CEL_RETURN_IF_ERROR(builder.Add(element)); |
476 | 0 | return true; |
477 | 0 | }, |
478 | 0 | descriptor_pool, message_factory, arena)); |
479 | | |
480 | 0 | return std::move(builder).BuildCompat(); |
481 | 0 | } |
482 | | |
483 | | MutableListValue* absl_nonnull NewMutableListValue( |
484 | 0 | google::protobuf::Arena* absl_nonnull arena) { |
485 | 0 | return ::new (arena->AllocateAligned(sizeof(MutableCompatListValueImpl), |
486 | 0 | alignof(MutableCompatListValueImpl))) |
487 | 0 | MutableCompatListValueImpl(arena); |
488 | 0 | } |
489 | | |
490 | 0 | bool IsMutableListValue(const Value& value) { |
491 | 0 | if (auto custom_list_value = value.AsCustomList(); custom_list_value) { |
492 | 0 | NativeTypeId native_type_id = custom_list_value->GetTypeId(); |
493 | 0 | if (native_type_id == NativeTypeId::For<MutableListValue>() || |
494 | 0 | native_type_id == NativeTypeId::For<MutableCompatListValue>()) { |
495 | 0 | return true; |
496 | 0 | } |
497 | 0 | } |
498 | 0 | return false; |
499 | 0 | } |
500 | | |
501 | 0 | bool IsMutableListValue(const ListValue& value) { |
502 | 0 | if (auto custom_list_value = value.AsCustom(); custom_list_value) { |
503 | 0 | NativeTypeId native_type_id = custom_list_value->GetTypeId(); |
504 | 0 | if (native_type_id == NativeTypeId::For<MutableListValue>() || |
505 | 0 | native_type_id == NativeTypeId::For<MutableCompatListValue>()) { |
506 | 0 | return true; |
507 | 0 | } |
508 | 0 | } |
509 | 0 | return false; |
510 | 0 | } |
511 | | |
512 | 0 | const MutableListValue* absl_nullable AsMutableListValue(const Value& value) { |
513 | 0 | if (auto custom_list_value = value.AsCustomList(); custom_list_value) { |
514 | 0 | NativeTypeId native_type_id = custom_list_value->GetTypeId(); |
515 | 0 | if (native_type_id == NativeTypeId::For<MutableListValue>()) { |
516 | 0 | return cel::internal::down_cast<const MutableListValue*>( |
517 | 0 | custom_list_value->interface()); |
518 | 0 | } |
519 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatListValue>()) { |
520 | 0 | return cel::internal::down_cast<const MutableCompatListValue*>( |
521 | 0 | custom_list_value->interface()); |
522 | 0 | } |
523 | 0 | } |
524 | 0 | return nullptr; |
525 | 0 | } |
526 | | |
527 | | const MutableListValue* absl_nullable AsMutableListValue( |
528 | 0 | const ListValue& value) { |
529 | 0 | if (auto custom_list_value = value.AsCustom(); custom_list_value) { |
530 | 0 | NativeTypeId native_type_id = custom_list_value->GetTypeId(); |
531 | 0 | if (native_type_id == NativeTypeId::For<MutableListValue>()) { |
532 | 0 | return cel::internal::down_cast<const MutableListValue*>( |
533 | 0 | custom_list_value->interface()); |
534 | 0 | } |
535 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatListValue>()) { |
536 | 0 | return cel::internal::down_cast<const MutableCompatListValue*>( |
537 | 0 | custom_list_value->interface()); |
538 | 0 | } |
539 | 0 | } |
540 | 0 | return nullptr; |
541 | 0 | } |
542 | | |
543 | 0 | const MutableListValue& GetMutableListValue(const Value& value) { |
544 | 0 | ABSL_DCHECK(IsMutableListValue(value)) << value; |
545 | 0 | const auto& custom_list_value = value.GetCustomList(); |
546 | 0 | NativeTypeId native_type_id = custom_list_value.GetTypeId(); |
547 | 0 | if (native_type_id == NativeTypeId::For<MutableListValue>()) { |
548 | 0 | return cel::internal::down_cast<const MutableListValue&>( |
549 | 0 | *custom_list_value.interface()); |
550 | 0 | } |
551 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatListValue>()) { |
552 | 0 | return cel::internal::down_cast<const MutableCompatListValue&>( |
553 | 0 | *custom_list_value.interface()); |
554 | 0 | } |
555 | 0 | ABSL_UNREACHABLE(); |
556 | 0 | } |
557 | | |
558 | 0 | const MutableListValue& GetMutableListValue(const ListValue& value) { |
559 | 0 | ABSL_DCHECK(IsMutableListValue(value)) << value; |
560 | 0 | const auto& custom_list_value = value.GetCustom(); |
561 | 0 | NativeTypeId native_type_id = custom_list_value.GetTypeId(); |
562 | 0 | if (native_type_id == NativeTypeId::For<MutableListValue>()) { |
563 | 0 | return cel::internal::down_cast<const MutableListValue&>( |
564 | 0 | *custom_list_value.interface()); |
565 | 0 | } |
566 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatListValue>()) { |
567 | 0 | return cel::internal::down_cast<const MutableCompatListValue&>( |
568 | 0 | *custom_list_value.interface()); |
569 | 0 | } |
570 | 0 | ABSL_UNREACHABLE(); |
571 | 0 | } |
572 | | |
573 | | absl_nonnull cel::ListValueBuilderPtr NewListValueBuilder( |
574 | 2.05M | google::protobuf::Arena* absl_nonnull arena) { |
575 | 2.05M | return std::make_unique<ListValueBuilderImpl>(arena); |
576 | 2.05M | } |
577 | | |
578 | | } // namespace common_internal |
579 | | |
580 | | } // namespace cel |
581 | | |
582 | | namespace cel { |
583 | | |
584 | | namespace common_internal { |
585 | | |
586 | | namespace { |
587 | | |
588 | | using ::google::api::expr::runtime::CelList; |
589 | | using ::google::api::expr::runtime::CelValue; |
590 | | |
591 | 504k | absl::Status CheckMapValue(const Value& value) { |
592 | 504k | if (auto error_value = value.AsError(); ABSL_PREDICT_FALSE(error_value)) { |
593 | 0 | return error_value->ToStatus(); |
594 | 0 | } |
595 | 504k | if (auto unknown_value = value.AsUnknown(); |
596 | 504k | ABSL_PREDICT_FALSE(unknown_value)) { |
597 | 0 | return absl::InvalidArgumentError("cannot add unknown value to list"); |
598 | 0 | } |
599 | 504k | return absl::OkStatus(); |
600 | 504k | } |
601 | | |
602 | 778k | size_t ValueHash(const Value& value) { |
603 | 778k | switch (value.kind()) { |
604 | 2.92k | case ValueKind::kBool: |
605 | 2.92k | return absl::HashOf(value.kind(), value.GetBool()); |
606 | 178k | case ValueKind::kInt: |
607 | 178k | return absl::HashOf(ValueKind::kInt, |
608 | 178k | absl::implicit_cast<int64_t>(value.GetInt())); |
609 | 18.7k | case ValueKind::kUint: |
610 | 18.7k | return absl::HashOf(ValueKind::kUint, |
611 | 18.7k | absl::implicit_cast<uint64_t>(value.GetUint())); |
612 | 578k | case ValueKind::kString: |
613 | 578k | return absl::HashOf(value.kind(), value.GetString()); |
614 | 0 | default: |
615 | 0 | ABSL_UNREACHABLE(); |
616 | 778k | } |
617 | 778k | } |
618 | | |
619 | 0 | size_t ValueHash(const CelValue& value) { |
620 | 0 | switch (value.type()) { |
621 | 0 | case CelValue::Type::kBool: |
622 | 0 | return absl::HashOf(ValueKind::kBool, value.BoolOrDie()); |
623 | 0 | case CelValue::Type::kInt: |
624 | 0 | return absl::HashOf(ValueKind::kInt, value.Int64OrDie()); |
625 | 0 | case CelValue::Type::kUint: |
626 | 0 | return absl::HashOf(ValueKind::kUint, value.Uint64OrDie()); |
627 | 0 | case CelValue::Type::kString: |
628 | 0 | return absl::HashOf(ValueKind::kString, value.StringOrDie().value()); |
629 | 0 | default: |
630 | 0 | ABSL_UNREACHABLE(); |
631 | 0 | } |
632 | 0 | } |
633 | | |
634 | 98.4k | bool ValueEquals(const Value& lhs, const Value& rhs) { |
635 | 98.4k | switch (lhs.kind()) { |
636 | 707 | case ValueKind::kBool: |
637 | 707 | switch (rhs.kind()) { |
638 | 279 | case ValueKind::kBool: |
639 | 279 | return lhs.GetBool() == rhs.GetBool(); |
640 | 25 | case ValueKind::kInt: |
641 | 25 | return false; |
642 | 24 | case ValueKind::kUint: |
643 | 24 | return false; |
644 | 379 | case ValueKind::kString: |
645 | 379 | return false; |
646 | 0 | default: |
647 | 0 | ABSL_UNREACHABLE(); |
648 | 707 | } |
649 | 17.8k | case ValueKind::kInt: |
650 | 17.8k | switch (rhs.kind()) { |
651 | 15 | case ValueKind::kBool: |
652 | 15 | return false; |
653 | 16.8k | case ValueKind::kInt: |
654 | 16.8k | return lhs.GetInt() == rhs.GetInt(); |
655 | 1.05k | case ValueKind::kUint: |
656 | 1.05k | return false; |
657 | 1 | case ValueKind::kString: |
658 | 1 | return false; |
659 | 0 | default: |
660 | 0 | ABSL_UNREACHABLE(); |
661 | 17.8k | } |
662 | 5.35k | case ValueKind::kUint: |
663 | 5.35k | switch (rhs.kind()) { |
664 | 253 | case ValueKind::kBool: |
665 | 253 | return false; |
666 | 1.75k | case ValueKind::kInt: |
667 | 1.75k | return false; |
668 | 3.35k | case ValueKind::kUint: |
669 | 3.35k | return lhs.GetUint() == rhs.GetUint(); |
670 | 0 | case ValueKind::kString: |
671 | 0 | return false; |
672 | 0 | default: |
673 | 0 | ABSL_UNREACHABLE(); |
674 | 5.35k | } |
675 | 74.4k | case ValueKind::kString: |
676 | 74.4k | switch (rhs.kind()) { |
677 | 0 | case ValueKind::kBool: |
678 | 0 | return false; |
679 | 245 | case ValueKind::kInt: |
680 | 245 | return false; |
681 | 239 | case ValueKind::kUint: |
682 | 239 | return false; |
683 | 74.0k | case ValueKind::kString: |
684 | 74.0k | return lhs.GetString() == rhs.GetString(); |
685 | 0 | default: |
686 | 0 | ABSL_UNREACHABLE(); |
687 | 74.4k | } |
688 | 0 | default: |
689 | 0 | ABSL_UNREACHABLE(); |
690 | 98.4k | } |
691 | 98.4k | } |
692 | | |
693 | 0 | bool CelValueEquals(const CelValue& lhs, const Value& rhs) { |
694 | 0 | switch (lhs.type()) { |
695 | 0 | case CelValue::Type::kBool: |
696 | 0 | switch (rhs.kind()) { |
697 | 0 | case ValueKind::kBool: |
698 | 0 | return BoolValue(lhs.BoolOrDie()) == rhs.GetBool(); |
699 | 0 | case ValueKind::kInt: |
700 | 0 | return false; |
701 | 0 | case ValueKind::kUint: |
702 | 0 | return false; |
703 | 0 | case ValueKind::kString: |
704 | 0 | return false; |
705 | 0 | default: |
706 | 0 | ABSL_UNREACHABLE(); |
707 | 0 | } |
708 | 0 | case CelValue::Type::kInt: |
709 | 0 | switch (rhs.kind()) { |
710 | 0 | case ValueKind::kBool: |
711 | 0 | return false; |
712 | 0 | case ValueKind::kInt: |
713 | 0 | return IntValue(lhs.Int64OrDie()) == rhs.GetInt(); |
714 | 0 | case ValueKind::kUint: |
715 | 0 | return false; |
716 | 0 | case ValueKind::kString: |
717 | 0 | return false; |
718 | 0 | default: |
719 | 0 | ABSL_UNREACHABLE(); |
720 | 0 | } |
721 | 0 | case CelValue::Type::kUint: |
722 | 0 | switch (rhs.kind()) { |
723 | 0 | case ValueKind::kBool: |
724 | 0 | return false; |
725 | 0 | case ValueKind::kInt: |
726 | 0 | return false; |
727 | 0 | case ValueKind::kUint: |
728 | 0 | return UintValue(lhs.Uint64OrDie()) == rhs.GetUint(); |
729 | 0 | case ValueKind::kString: |
730 | 0 | return false; |
731 | 0 | default: |
732 | 0 | ABSL_UNREACHABLE(); |
733 | 0 | } |
734 | 0 | case CelValue::Type::kString: |
735 | 0 | switch (rhs.kind()) { |
736 | 0 | case ValueKind::kBool: |
737 | 0 | return false; |
738 | 0 | case ValueKind::kInt: |
739 | 0 | return false; |
740 | 0 | case ValueKind::kUint: |
741 | 0 | return false; |
742 | 0 | case ValueKind::kString: |
743 | 0 | return rhs.GetString().Equals(lhs.StringOrDie().value()); |
744 | 0 | default: |
745 | 0 | ABSL_UNREACHABLE(); |
746 | 0 | } |
747 | 0 | default: |
748 | 0 | ABSL_UNREACHABLE(); |
749 | 0 | } |
750 | 0 | } |
751 | | |
752 | 208k | absl::StatusOr<std::string> ValueToJsonString(const Value& value) { |
753 | 208k | switch (value.kind()) { |
754 | 208k | case ValueKind::kString: |
755 | 208k | return value.GetString().NativeString(); |
756 | 8 | default: |
757 | 8 | return common_internal::MakeTypeConversionError(value.GetRuntimeType(), |
758 | 8 | StringType()); |
759 | 208k | } |
760 | 208k | } |
761 | | |
762 | | template <typename Map> |
763 | | absl::Status MapValueToJsonObject( |
764 | | const Map& map, const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
765 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
766 | 90.1k | google::protobuf::Message* absl_nonnull json) { |
767 | 90.1k | ABSL_DCHECK(descriptor_pool != nullptr); |
768 | 90.1k | ABSL_DCHECK(message_factory != nullptr); |
769 | 90.1k | ABSL_DCHECK(json != nullptr); |
770 | 90.1k | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
771 | 90.1k | google::protobuf::Descriptor::WELLKNOWNTYPE_STRUCT); |
772 | | |
773 | 90.1k | StructReflection reflection; |
774 | 90.1k | CEL_RETURN_IF_ERROR(reflection.Initialize(json->GetDescriptor())); |
775 | | |
776 | 90.1k | json->Clear(); |
777 | | |
778 | 90.1k | if (map.empty()) { |
779 | 0 | return absl::OkStatus(); |
780 | 0 | } |
781 | | |
782 | 208k | for (const auto& entry : map) { |
783 | 208k | CEL_ASSIGN_OR_RETURN(auto key, ValueToJsonString(entry.first)); |
784 | 208k | CEL_RETURN_IF_ERROR(entry.second.ConvertToJson( |
785 | 208k | descriptor_pool, message_factory, reflection.InsertField(json, key))); |
786 | 208k | } |
787 | 90.1k | return absl::OkStatus(); |
788 | 90.1k | } |
789 | | |
790 | | template <typename Map> |
791 | | absl::Status MapValueToJson( |
792 | | const Map& map, const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
793 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
794 | | google::protobuf::Message* absl_nonnull json) { |
795 | | ABSL_DCHECK(descriptor_pool != nullptr); |
796 | | ABSL_DCHECK(message_factory != nullptr); |
797 | | ABSL_DCHECK(json != nullptr); |
798 | | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
799 | | google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE); |
800 | | |
801 | | ValueReflection reflection; |
802 | | CEL_RETURN_IF_ERROR(reflection.Initialize(json->GetDescriptor())); |
803 | | return MapValueToJsonObject(map, descriptor_pool, message_factory, |
804 | | reflection.MutableStructValue(json)); |
805 | | } |
806 | | |
807 | | struct ValueHasher { |
808 | | using is_transparent = void; |
809 | | |
810 | 778k | size_t operator()(const Value& value) const { return (ValueHash)(value); } |
811 | | |
812 | 0 | size_t operator()(const CelValue& value) const { return (ValueHash)(value); } |
813 | | }; |
814 | | |
815 | | struct ValueEqualer { |
816 | | using is_transparent = void; |
817 | | |
818 | 0 | bool operator()(const Value& lhs, const CelValue& rhs) const { |
819 | 0 | return (*this)(rhs, lhs); |
820 | 0 | } |
821 | | |
822 | 0 | bool operator()(const CelValue& lhs, const Value& rhs) const { |
823 | 0 | return (CelValueEquals)(lhs, rhs); |
824 | 0 | } |
825 | | |
826 | 98.4k | bool operator()(const Value& lhs, const Value& rhs) const { |
827 | 98.4k | return (ValueEquals)(lhs, rhs); |
828 | 98.4k | } |
829 | | }; |
830 | | |
831 | | using ValueFlatHashMapAllocator = ArenaAllocator<std::pair<const Value, Value>>; |
832 | | |
833 | | using ValueFlatHashMap = |
834 | | absl::flat_hash_map<Value, Value, ValueHasher, ValueEqualer, |
835 | | ValueFlatHashMapAllocator>; |
836 | | |
837 | | class CompatMapValueImplIterator final : public ValueIterator { |
838 | | public: |
839 | | explicit CompatMapValueImplIterator(const ValueFlatHashMap* absl_nonnull map) |
840 | 5.53k | : begin_(map->begin()), end_(map->end()) {} |
841 | | |
842 | 5.13k | bool HasNext() override { return begin_ != end_; } |
843 | | |
844 | | absl::Status Next(const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
845 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
846 | | google::protobuf::Arena* absl_nonnull arena, |
847 | 5.03k | Value* absl_nonnull result) override { |
848 | 5.03k | if (ABSL_PREDICT_FALSE(begin_ == end_)) { |
849 | 0 | return absl::FailedPreconditionError( |
850 | 0 | "ValueManager::Next called after ValueManager::HasNext returned " |
851 | 0 | "false"); |
852 | 0 | } |
853 | 5.03k | *result = begin_->first; |
854 | 5.03k | ++begin_; |
855 | 5.03k | return absl::OkStatus(); |
856 | 5.03k | } |
857 | | |
858 | | absl::StatusOr<bool> Next1( |
859 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
860 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
861 | | google::protobuf::Arena* absl_nonnull arena, |
862 | 5.03k | Value* absl_nonnull key_or_value) override { |
863 | 5.03k | ABSL_DCHECK(descriptor_pool != nullptr); |
864 | 5.03k | ABSL_DCHECK(message_factory != nullptr); |
865 | 5.03k | ABSL_DCHECK(arena != nullptr); |
866 | 5.03k | ABSL_DCHECK(key_or_value != nullptr); |
867 | | |
868 | 5.03k | if (begin_ == end_) { |
869 | 1.13k | return false; |
870 | 1.13k | } |
871 | 3.90k | *key_or_value = begin_->first; |
872 | 3.90k | ++begin_; |
873 | 3.90k | return true; |
874 | 5.03k | } |
875 | | |
876 | | absl::StatusOr<bool> Next2( |
877 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
878 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
879 | | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull key, |
880 | 0 | Value* absl_nullable value) override { |
881 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
882 | 0 | ABSL_DCHECK(message_factory != nullptr); |
883 | 0 | ABSL_DCHECK(arena != nullptr); |
884 | 0 | ABSL_DCHECK(key != nullptr); |
885 | |
|
886 | 0 | if (begin_ == end_) { |
887 | 0 | return false; |
888 | 0 | } |
889 | 0 | *key = begin_->first; |
890 | 0 | if (value != nullptr) { |
891 | 0 | *value = begin_->second; |
892 | 0 | } |
893 | 0 | ++begin_; |
894 | 0 | return true; |
895 | 0 | } |
896 | | |
897 | | private: |
898 | | typename ValueFlatHashMap::const_iterator begin_; |
899 | | const typename ValueFlatHashMap::const_iterator end_; |
900 | | }; |
901 | | |
902 | | class MapValueBuilderImpl final : public MapValueBuilder { |
903 | | public: |
904 | | explicit MapValueBuilderImpl(google::protobuf::Arena* absl_nonnull arena) |
905 | 748k | : arena_(arena) { |
906 | 748k | map_.Construct(arena_); |
907 | 748k | } |
908 | | |
909 | 504k | absl::Status Put(Value key, Value value) override { |
910 | 504k | CEL_RETURN_IF_ERROR(CheckMapKey(key)); |
911 | 504k | CEL_RETURN_IF_ERROR(CheckMapValue(value)); |
912 | 504k | if (auto it = map_->find(key); ABSL_PREDICT_FALSE(it != map_->end())) { |
913 | 49 | return DuplicateKeyError().ToStatus(); |
914 | 49 | } |
915 | 504k | UnsafePut(std::move(key), std::move(value)); |
916 | 504k | return absl::OkStatus(); |
917 | 504k | } |
918 | | |
919 | 504k | void UnsafePut(Value key, Value value) override { |
920 | 504k | auto insertion = map_->insert({std::move(key), std::move(value)}); |
921 | 504k | ABSL_DCHECK(insertion.second); |
922 | 504k | } |
923 | | |
924 | 0 | size_t Size() const override { return map_->size(); } |
925 | | |
926 | 748k | void Reserve(size_t capacity) override { map_->reserve(capacity); } |
927 | | |
928 | | MapValue Build() && override; |
929 | | |
930 | | CustomMapValue BuildCustom() &&; |
931 | | |
932 | | const CompatMapValue* absl_nonnull BuildCompat() &&; |
933 | | |
934 | | private: |
935 | | google::protobuf::Arena* absl_nonnull const arena_; |
936 | | internal::Manual<ValueFlatHashMap> map_; |
937 | | }; |
938 | | |
939 | | class CompatMapValueImpl final : public CompatMapValue { |
940 | | public: |
941 | 240k | explicit CompatMapValueImpl(ValueFlatHashMap&& map) : map_(std::move(map)) {} |
942 | | |
943 | 0 | google::protobuf::Arena* GetOwningArena() const override { |
944 | 0 | return map_.get_allocator().arena(); |
945 | 0 | } |
946 | | |
947 | 0 | std::string DebugString() const override { |
948 | 0 | return absl::StrCat("{", absl::StrJoin(map_, ", ", ValueFormatter{}), "}"); |
949 | 0 | } |
950 | | |
951 | | absl::Status ConvertToJsonObject( |
952 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
953 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
954 | 90.1k | google::protobuf::Message* absl_nonnull json) const override { |
955 | 90.1k | return MapValueToJsonObject(map_, descriptor_pool, message_factory, json); |
956 | 90.1k | } |
957 | | |
958 | 0 | CustomMapValue Clone(google::protobuf::Arena* absl_nonnull arena) const override { |
959 | 0 | ABSL_DCHECK(arena != nullptr); |
960 | |
|
961 | 0 | MapValueBuilderImpl builder(arena); |
962 | 0 | builder.Reserve(map_.size()); |
963 | 0 | for (const auto& entry : map_) { |
964 | 0 | builder.UnsafePut(entry.first.Clone(arena), entry.second.Clone(arena)); |
965 | 0 | } |
966 | 0 | return std::move(builder).BuildCustom(); |
967 | 0 | } |
968 | | |
969 | 13.1k | size_t Size() const override { return map_.size(); } |
970 | | |
971 | | absl::Status ListKeys( |
972 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
973 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
974 | | google::protobuf::Arena* absl_nonnull arena, |
975 | 0 | ListValue* absl_nonnull result) const override { |
976 | 0 | *result = CustomListValue(ProjectKeys(), map_.get_allocator().arena()); |
977 | 0 | return absl::OkStatus(); |
978 | 0 | } |
979 | | |
980 | | absl::Status ForEach( |
981 | | ForEachCallback callback, |
982 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
983 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
984 | 0 | google::protobuf::Arena* absl_nonnull arena) const override { |
985 | 0 | for (const auto& entry : map_) { |
986 | 0 | CEL_ASSIGN_OR_RETURN(auto ok, callback(entry.first, entry.second)); |
987 | 0 | if (!ok) { |
988 | 0 | break; |
989 | 0 | } |
990 | 0 | } |
991 | 0 | return absl::OkStatus(); |
992 | 0 | } |
993 | | |
994 | 5.53k | absl::StatusOr<absl_nonnull ValueIteratorPtr> NewIterator() const override { |
995 | 5.53k | return std::make_unique<CompatMapValueImplIterator>(&map_); |
996 | 5.53k | } |
997 | | |
998 | 0 | absl::optional<CelValue> operator[](CelValue key) const override { |
999 | 0 | return Get(map_.get_allocator().arena(), key); |
1000 | 0 | } |
1001 | | |
1002 | | using CompatMapValue::Get; |
1003 | | absl::optional<CelValue> Get(google::protobuf::Arena* arena, |
1004 | 0 | CelValue key) const override { |
1005 | 0 | if (auto status = CelValue::CheckMapKeyType(key); !status.ok()) { |
1006 | 0 | status.IgnoreError(); |
1007 | 0 | return std::nullopt; |
1008 | 0 | } |
1009 | 0 | if (auto it = map_.find(key); it != map_.end()) { |
1010 | 0 | return common_internal::UnsafeLegacyValue( |
1011 | 0 | it->second, /*stable=*/true, |
1012 | 0 | arena != nullptr ? arena : map_.get_allocator().arena()); |
1013 | 0 | } |
1014 | 0 | return std::nullopt; |
1015 | 0 | } |
1016 | | |
1017 | 0 | absl::StatusOr<bool> Has(const CelValue& key) const override { |
1018 | | // This check safeguards against issues with invalid key types such as NaN. |
1019 | 0 | CEL_RETURN_IF_ERROR(CelValue::CheckMapKeyType(key)); |
1020 | 0 | return map_.find(key) != map_.end(); |
1021 | 0 | } |
1022 | | |
1023 | 0 | int size() const override { return static_cast<int>(Size()); } |
1024 | | |
1025 | 0 | absl::StatusOr<const CelList*> ListKeys() const override { |
1026 | 0 | return ProjectKeys(); |
1027 | 0 | } |
1028 | | |
1029 | 0 | absl::StatusOr<const CelList*> ListKeys(google::protobuf::Arena* arena) const override { |
1030 | 0 | return ProjectKeys(); |
1031 | 0 | } |
1032 | | |
1033 | | protected: |
1034 | | absl::StatusOr<bool> Find( |
1035 | | const Value& key, |
1036 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1037 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1038 | | google::protobuf::Arena* absl_nonnull arena, |
1039 | 106k | Value* absl_nonnull result) const override { |
1040 | 106k | CEL_RETURN_IF_ERROR(CheckMapKey(key)); |
1041 | 106k | if (auto it = map_.find(key); it != map_.end()) { |
1042 | 19.1k | *result = it->second; |
1043 | 19.1k | return true; |
1044 | 19.1k | } |
1045 | 87.4k | return false; |
1046 | 106k | } |
1047 | | |
1048 | | absl::StatusOr<bool> Has( |
1049 | | const Value& key, |
1050 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1051 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1052 | 1.68k | google::protobuf::Arena* absl_nonnull arena) const override { |
1053 | 1.68k | CEL_RETURN_IF_ERROR(CheckMapKey(key)); |
1054 | 1.68k | return map_.find(key) != map_.end(); |
1055 | 1.68k | } |
1056 | | |
1057 | | private: |
1058 | 0 | const CompatListValue* absl_nonnull ProjectKeys() const { |
1059 | 0 | absl::call_once(keys_once_, [this]() { |
1060 | 0 | ListValueBuilderImpl builder(map_.get_allocator().arena()); |
1061 | 0 | builder.Reserve(map_.size()); |
1062 | |
|
1063 | 0 | for (const auto& entry : map_) { |
1064 | 0 | builder.UnsafeAdd(entry.first); |
1065 | 0 | } |
1066 | |
|
1067 | 0 | std::move(builder).BuildCompatAt(&keys_[0]); |
1068 | 0 | }); |
1069 | 0 | return std::launder( |
1070 | 0 | reinterpret_cast<const CompatListValueImpl*>(&keys_[0])); |
1071 | 0 | } |
1072 | | |
1073 | | const ValueFlatHashMap map_; |
1074 | | mutable absl::once_flag keys_once_; |
1075 | | alignas(CompatListValueImpl) mutable char keys_[sizeof(CompatListValueImpl)]; |
1076 | | }; |
1077 | | |
1078 | 708k | MapValue MapValueBuilderImpl::Build() && { |
1079 | 708k | if (map_->empty()) { |
1080 | 468k | return MapValue(); |
1081 | 468k | } |
1082 | 240k | return std::move(*this).BuildCustom(); |
1083 | 708k | } |
1084 | | |
1085 | 240k | CustomMapValue MapValueBuilderImpl::BuildCustom() && { |
1086 | 240k | if (map_->empty()) { |
1087 | 0 | return CustomMapValue(EmptyCompatMapValue(), arena_); |
1088 | 0 | } |
1089 | 240k | return CustomMapValue(std::move(*this).BuildCompat(), arena_); |
1090 | 240k | } |
1091 | | |
1092 | 240k | const CompatMapValue* absl_nonnull MapValueBuilderImpl::BuildCompat() && { |
1093 | 240k | if (map_->empty()) { |
1094 | 0 | return EmptyCompatMapValue(); |
1095 | 0 | } |
1096 | 240k | CompatMapValueImpl* absl_nonnull impl = ::new (arena_->AllocateAligned( |
1097 | 240k | sizeof(CompatMapValueImpl), alignof(CompatMapValueImpl))) |
1098 | 240k | CompatMapValueImpl(std::move(*map_)); |
1099 | 240k | return impl; |
1100 | 240k | } |
1101 | | |
1102 | | class MutableCompatMapValueImpl final : public MutableCompatMapValue { |
1103 | | public: |
1104 | | explicit MutableCompatMapValueImpl(google::protobuf::Arena* absl_nonnull arena) |
1105 | 0 | : map_(arena) {} |
1106 | | |
1107 | 0 | google::protobuf::Arena* GetOwningArena() const override { |
1108 | 0 | return map_.get_allocator().arena(); |
1109 | 0 | } |
1110 | | |
1111 | 0 | std::string DebugString() const override { |
1112 | 0 | return absl::StrCat("{", absl::StrJoin(map_, ", ", ValueFormatter{}), "}"); |
1113 | 0 | } |
1114 | | |
1115 | | absl::Status ConvertToJsonObject( |
1116 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1117 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1118 | 0 | google::protobuf::Message* absl_nonnull json) const override { |
1119 | 0 | return MapValueToJsonObject(map_, descriptor_pool, message_factory, json); |
1120 | 0 | } |
1121 | | |
1122 | 0 | CustomMapValue Clone(google::protobuf::Arena* absl_nonnull arena) const override { |
1123 | 0 | ABSL_DCHECK(arena != nullptr); |
1124 | |
|
1125 | 0 | MapValueBuilderImpl builder(arena); |
1126 | 0 | builder.Reserve(map_.size()); |
1127 | 0 | for (const auto& entry : map_) { |
1128 | 0 | builder.UnsafePut(entry.first.Clone(arena), entry.second.Clone(arena)); |
1129 | 0 | } |
1130 | 0 | return std::move(builder).BuildCustom(); |
1131 | 0 | } |
1132 | | |
1133 | 0 | size_t Size() const override { return map_.size(); } |
1134 | | |
1135 | | absl::Status ListKeys( |
1136 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1137 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1138 | | google::protobuf::Arena* absl_nonnull arena, |
1139 | 0 | ListValue* absl_nonnull result) const override { |
1140 | 0 | *result = CustomListValue(ProjectKeys(), map_.get_allocator().arena()); |
1141 | 0 | return absl::OkStatus(); |
1142 | 0 | } |
1143 | | |
1144 | | absl::Status ForEach( |
1145 | | ForEachCallback callback, |
1146 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1147 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1148 | 0 | google::protobuf::Arena* absl_nonnull arena) const override { |
1149 | 0 | for (const auto& entry : map_) { |
1150 | 0 | CEL_ASSIGN_OR_RETURN(auto ok, callback(entry.first, entry.second)); |
1151 | 0 | if (!ok) { |
1152 | 0 | break; |
1153 | 0 | } |
1154 | 0 | } |
1155 | 0 | return absl::OkStatus(); |
1156 | 0 | } |
1157 | | |
1158 | 0 | absl::StatusOr<absl_nonnull ValueIteratorPtr> NewIterator() const override { |
1159 | 0 | return std::make_unique<CompatMapValueImplIterator>(&map_); |
1160 | 0 | } |
1161 | | |
1162 | 0 | absl::optional<CelValue> operator[](CelValue key) const override { |
1163 | 0 | return Get(map_.get_allocator().arena(), key); |
1164 | 0 | } |
1165 | | |
1166 | | using MutableCompatMapValue::Get; |
1167 | | absl::optional<CelValue> Get(google::protobuf::Arena* arena, |
1168 | 0 | CelValue key) const override { |
1169 | 0 | if (auto status = CelValue::CheckMapKeyType(key); !status.ok()) { |
1170 | 0 | status.IgnoreError(); |
1171 | 0 | return std::nullopt; |
1172 | 0 | } |
1173 | 0 | if (auto it = map_.find(key); it != map_.end()) { |
1174 | 0 | return common_internal::UnsafeLegacyValue( |
1175 | 0 | it->second, /*stable=*/false, |
1176 | 0 | arena != nullptr ? arena : map_.get_allocator().arena()); |
1177 | 0 | } |
1178 | 0 | return std::nullopt; |
1179 | 0 | } |
1180 | | |
1181 | 0 | absl::StatusOr<bool> Has(const CelValue& key) const override { |
1182 | | // This check safeguards against issues with invalid key types such as NaN. |
1183 | 0 | CEL_RETURN_IF_ERROR(CelValue::CheckMapKeyType(key)); |
1184 | 0 | return map_.find(key) != map_.end(); |
1185 | 0 | } |
1186 | | |
1187 | 0 | int size() const override { return static_cast<int>(Size()); } |
1188 | | |
1189 | 0 | absl::StatusOr<const CelList*> ListKeys() const override { |
1190 | 0 | return ProjectKeys(); |
1191 | 0 | } |
1192 | | |
1193 | 0 | absl::StatusOr<const CelList*> ListKeys(google::protobuf::Arena* arena) const override { |
1194 | 0 | return ProjectKeys(); |
1195 | 0 | } |
1196 | | |
1197 | 0 | absl::Status Put(Value key, Value value) const override { |
1198 | 0 | CEL_RETURN_IF_ERROR(CheckMapKey(key)); |
1199 | 0 | CEL_RETURN_IF_ERROR(CheckMapValue(value)); |
1200 | 0 | if (auto it = map_.find(key); ABSL_PREDICT_FALSE(it != map_.end())) { |
1201 | 0 | return DuplicateKeyError().ToStatus(); |
1202 | 0 | } |
1203 | 0 | auto insertion = map_.insert({std::move(key), std::move(value)}); |
1204 | 0 | ABSL_DCHECK(insertion.second); |
1205 | 0 | return absl::OkStatus(); |
1206 | 0 | } |
1207 | | |
1208 | 0 | void Reserve(size_t capacity) const override { map_.reserve(capacity); } |
1209 | | |
1210 | | protected: |
1211 | | absl::StatusOr<bool> Find( |
1212 | | const Value& key, |
1213 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1214 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1215 | | google::protobuf::Arena* absl_nonnull arena, |
1216 | 0 | Value* absl_nonnull result) const override { |
1217 | 0 | CEL_RETURN_IF_ERROR(CheckMapKey(key)); |
1218 | 0 | if (auto it = map_.find(key); it != map_.end()) { |
1219 | 0 | *result = it->second; |
1220 | 0 | return true; |
1221 | 0 | } |
1222 | 0 | return false; |
1223 | 0 | } |
1224 | | |
1225 | | absl::StatusOr<bool> Has( |
1226 | | const Value& key, |
1227 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1228 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1229 | 0 | google::protobuf::Arena* absl_nonnull arena) const override { |
1230 | 0 | CEL_RETURN_IF_ERROR(CheckMapKey(key)); |
1231 | 0 | return map_.find(key) != map_.end(); |
1232 | 0 | } |
1233 | | |
1234 | | private: |
1235 | 0 | const CompatListValue* absl_nonnull ProjectKeys() const { |
1236 | 0 | absl::call_once(keys_once_, [this]() { |
1237 | 0 | ListValueBuilderImpl builder(map_.get_allocator().arena()); |
1238 | 0 | builder.Reserve(map_.size()); |
1239 | |
|
1240 | 0 | for (const auto& entry : map_) { |
1241 | 0 | builder.UnsafeAdd(entry.first); |
1242 | 0 | } |
1243 | |
|
1244 | 0 | std::move(builder).BuildCompatAt(&keys_[0]); |
1245 | 0 | }); |
1246 | 0 | return std::launder( |
1247 | 0 | reinterpret_cast<const CompatListValueImpl*>(&keys_[0])); |
1248 | 0 | } |
1249 | | |
1250 | | mutable ValueFlatHashMap map_; |
1251 | | mutable absl::once_flag keys_once_; |
1252 | | alignas(CompatListValueImpl) mutable char keys_[sizeof(CompatListValueImpl)]; |
1253 | | }; |
1254 | | |
1255 | | } // namespace |
1256 | | |
1257 | | absl::StatusOr<const CompatMapValue* absl_nonnull> MakeCompatMapValue( |
1258 | | const CustomMapValue& value, |
1259 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
1260 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
1261 | 0 | google::protobuf::Arena* absl_nonnull arena) { |
1262 | 0 | MapValueBuilderImpl builder(arena); |
1263 | 0 | builder.Reserve(value.Size()); |
1264 | |
|
1265 | 0 | CEL_RETURN_IF_ERROR(value.ForEach( |
1266 | 0 | [&](const Value& key, const Value& value) -> absl::StatusOr<bool> { |
1267 | 0 | CEL_RETURN_IF_ERROR(builder.Put(key, value)); |
1268 | 0 | return true; |
1269 | 0 | }, |
1270 | 0 | descriptor_pool, message_factory, arena)); |
1271 | | |
1272 | 0 | return std::move(builder).BuildCompat(); |
1273 | 0 | } |
1274 | | |
1275 | | MutableMapValue* absl_nonnull NewMutableMapValue( |
1276 | 0 | google::protobuf::Arena* absl_nonnull arena) { |
1277 | 0 | return ::new (arena->AllocateAligned(sizeof(MutableCompatMapValueImpl), |
1278 | 0 | alignof(MutableCompatMapValueImpl))) |
1279 | 0 | MutableCompatMapValueImpl(arena); |
1280 | 0 | } |
1281 | | |
1282 | 0 | bool IsMutableMapValue(const Value& value) { |
1283 | 0 | if (auto custom_map_value = value.AsCustomMap(); custom_map_value) { |
1284 | 0 | NativeTypeId native_type_id = custom_map_value->GetTypeId(); |
1285 | 0 | if (native_type_id == NativeTypeId::For<MutableMapValue>() || |
1286 | 0 | native_type_id == NativeTypeId::For<MutableCompatMapValue>()) { |
1287 | 0 | return true; |
1288 | 0 | } |
1289 | 0 | } |
1290 | 0 | return false; |
1291 | 0 | } |
1292 | | |
1293 | 0 | bool IsMutableMapValue(const MapValue& value) { |
1294 | 0 | if (auto custom_map_value = value.AsCustom(); custom_map_value) { |
1295 | 0 | NativeTypeId native_type_id = custom_map_value->GetTypeId(); |
1296 | 0 | if (native_type_id == NativeTypeId::For<MutableMapValue>() || |
1297 | 0 | native_type_id == NativeTypeId::For<MutableCompatMapValue>()) { |
1298 | 0 | return true; |
1299 | 0 | } |
1300 | 0 | } |
1301 | 0 | return false; |
1302 | 0 | } |
1303 | | |
1304 | 0 | const MutableMapValue* absl_nullable AsMutableMapValue(const Value& value) { |
1305 | 0 | if (auto custom_map_value = value.AsCustomMap(); custom_map_value) { |
1306 | 0 | NativeTypeId native_type_id = custom_map_value->GetTypeId(); |
1307 | 0 | if (native_type_id == NativeTypeId::For<MutableMapValue>()) { |
1308 | 0 | return cel::internal::down_cast<const MutableMapValue*>( |
1309 | 0 | custom_map_value->interface()); |
1310 | 0 | } |
1311 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatMapValue>()) { |
1312 | 0 | return cel::internal::down_cast<const MutableCompatMapValue*>( |
1313 | 0 | custom_map_value->interface()); |
1314 | 0 | } |
1315 | 0 | } |
1316 | 0 | return nullptr; |
1317 | 0 | } |
1318 | | |
1319 | 0 | const MutableMapValue* absl_nullable AsMutableMapValue(const MapValue& value) { |
1320 | 0 | if (auto custom_map_value = value.AsCustom(); custom_map_value) { |
1321 | 0 | NativeTypeId native_type_id = custom_map_value->GetTypeId(); |
1322 | 0 | if (native_type_id == NativeTypeId::For<MutableMapValue>()) { |
1323 | 0 | return cel::internal::down_cast<const MutableMapValue*>( |
1324 | 0 | custom_map_value->interface()); |
1325 | 0 | } |
1326 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatMapValue>()) { |
1327 | 0 | return cel::internal::down_cast<const MutableCompatMapValue*>( |
1328 | 0 | custom_map_value->interface()); |
1329 | 0 | } |
1330 | 0 | } |
1331 | 0 | return nullptr; |
1332 | 0 | } |
1333 | | |
1334 | 0 | const MutableMapValue& GetMutableMapValue(const Value& value) { |
1335 | 0 | ABSL_DCHECK(IsMutableMapValue(value)) << value; |
1336 | 0 | const auto& custom_map_value = value.GetCustomMap(); |
1337 | 0 | NativeTypeId native_type_id = custom_map_value.GetTypeId(); |
1338 | 0 | if (native_type_id == NativeTypeId::For<MutableMapValue>()) { |
1339 | 0 | return cel::internal::down_cast<const MutableMapValue&>( |
1340 | 0 | *custom_map_value.interface()); |
1341 | 0 | } |
1342 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatMapValue>()) { |
1343 | 0 | return cel::internal::down_cast<const MutableCompatMapValue&>( |
1344 | 0 | *custom_map_value.interface()); |
1345 | 0 | } |
1346 | 0 | ABSL_UNREACHABLE(); |
1347 | 0 | } |
1348 | | |
1349 | 0 | const MutableMapValue& GetMutableMapValue(const MapValue& value) { |
1350 | 0 | ABSL_DCHECK(IsMutableMapValue(value)) << value; |
1351 | 0 | const auto& custom_map_value = value.GetCustom(); |
1352 | 0 | NativeTypeId native_type_id = custom_map_value.GetTypeId(); |
1353 | 0 | if (native_type_id == NativeTypeId::For<MutableMapValue>()) { |
1354 | 0 | return cel::internal::down_cast<const MutableMapValue&>( |
1355 | 0 | *custom_map_value.interface()); |
1356 | 0 | } |
1357 | 0 | if (native_type_id == NativeTypeId::For<MutableCompatMapValue>()) { |
1358 | 0 | return cel::internal::down_cast<const MutableCompatMapValue&>( |
1359 | 0 | *custom_map_value.interface()); |
1360 | 0 | } |
1361 | 0 | ABSL_UNREACHABLE(); |
1362 | 0 | } |
1363 | | |
1364 | | absl_nonnull cel::MapValueBuilderPtr NewMapValueBuilder( |
1365 | 748k | google::protobuf::Arena* absl_nonnull arena) { |
1366 | 748k | return std::make_unique<MapValueBuilderImpl>(arena); |
1367 | 748k | } |
1368 | | |
1369 | | } // namespace common_internal |
1370 | | |
1371 | | } // namespace cel |