/proc/self/cwd/common/values/opaque_value.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 <string> |
16 | | #include <type_traits> |
17 | | #include <utility> |
18 | | |
19 | | #include "absl/base/nullability.h" |
20 | | #include "absl/base/optimization.h" |
21 | | #include "absl/log/absl_check.h" |
22 | | #include "absl/status/status.h" |
23 | | #include "absl/strings/str_cat.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "absl/types/optional.h" |
26 | | #include "common/native_type.h" |
27 | | #include "common/optional_ref.h" |
28 | | #include "common/type.h" |
29 | | #include "common/value.h" |
30 | | #include "google/protobuf/arena.h" |
31 | | #include "google/protobuf/descriptor.h" |
32 | | #include "google/protobuf/io/zero_copy_stream.h" |
33 | | #include "google/protobuf/message.h" |
34 | | |
35 | | namespace cel { |
36 | | |
37 | | // Code below assumes OptionalValue has the same layout as OpaqueValue. |
38 | | static_assert(std::is_base_of_v<OpaqueValue, OptionalValue>); |
39 | | static_assert(sizeof(OpaqueValue) == sizeof(OptionalValue)); |
40 | | static_assert(alignof(OpaqueValue) == alignof(OptionalValue)); |
41 | | |
42 | 0 | OpaqueValue OpaqueValue::Clone(google::protobuf::Arena* absl_nonnull arena) const { |
43 | 0 | ABSL_DCHECK(arena != nullptr); |
44 | 0 | ABSL_DCHECK(*this); |
45 | |
|
46 | 0 | if (ABSL_PREDICT_FALSE(dispatcher_ == nullptr)) { |
47 | 0 | OpaqueValueInterface::Content content = |
48 | 0 | content_.To<OpaqueValueInterface::Content>(); |
49 | 0 | if (content.interface == nullptr) { |
50 | 0 | return *this; |
51 | 0 | } |
52 | 0 | if (content.arena != arena) { |
53 | 0 | return content.interface->Clone(arena); |
54 | 0 | } |
55 | 0 | return *this; |
56 | 0 | } |
57 | 0 | if (dispatcher_->get_arena(dispatcher_, content_) != arena) { |
58 | 0 | return dispatcher_->clone(dispatcher_, content_, arena); |
59 | 0 | } |
60 | 0 | return *this; |
61 | 0 | } |
62 | | |
63 | 0 | OpaqueType OpaqueValue::GetRuntimeType() const { |
64 | 0 | ABSL_DCHECK(*this); |
65 | |
|
66 | 0 | if (ABSL_PREDICT_FALSE(dispatcher_ == nullptr)) { |
67 | 0 | OpaqueValueInterface::Content content = |
68 | 0 | content_.To<OpaqueValueInterface::Content>(); |
69 | 0 | ABSL_DCHECK(content.interface != nullptr); |
70 | 0 | return content.interface->GetRuntimeType(); |
71 | 0 | } |
72 | 0 | return dispatcher_->get_runtime_type(dispatcher_, content_); |
73 | 0 | } |
74 | | |
75 | 0 | absl::string_view OpaqueValue::GetTypeName() const { |
76 | 0 | ABSL_DCHECK(*this); |
77 | |
|
78 | 0 | if (ABSL_PREDICT_FALSE(dispatcher_ == nullptr)) { |
79 | 0 | OpaqueValueInterface::Content content = |
80 | 0 | content_.To<OpaqueValueInterface::Content>(); |
81 | 0 | ABSL_DCHECK(content.interface != nullptr); |
82 | 0 | return content.interface->GetTypeName(); |
83 | 0 | } |
84 | 0 | return dispatcher_->get_type_name(dispatcher_, content_); |
85 | 0 | } |
86 | | |
87 | 0 | std::string OpaqueValue::DebugString() const { |
88 | 0 | ABSL_DCHECK(*this); |
89 | |
|
90 | 0 | if (ABSL_PREDICT_FALSE(dispatcher_ == nullptr)) { |
91 | 0 | OpaqueValueInterface::Content content = |
92 | 0 | content_.To<OpaqueValueInterface::Content>(); |
93 | 0 | ABSL_DCHECK(content.interface != nullptr); |
94 | 0 | return content.interface->DebugString(); |
95 | 0 | } |
96 | 0 | return dispatcher_->debug_string(dispatcher_, content_); |
97 | 0 | } |
98 | | |
99 | | // See Value::SerializeTo(). |
100 | | absl::Status OpaqueValue::SerializeTo( |
101 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
102 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
103 | 0 | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const { |
104 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
105 | 0 | ABSL_DCHECK(message_factory != nullptr); |
106 | 0 | ABSL_DCHECK(output != nullptr); |
107 | |
|
108 | 0 | return absl::FailedPreconditionError( |
109 | 0 | absl::StrCat(GetTypeName(), "is unserializable")); |
110 | 0 | } |
111 | | |
112 | | // See Value::ConvertToJson(). |
113 | | absl::Status OpaqueValue::ConvertToJson( |
114 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
115 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
116 | 0 | google::protobuf::Message* absl_nonnull json) const { |
117 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
118 | 0 | ABSL_DCHECK(message_factory != nullptr); |
119 | 0 | ABSL_DCHECK(json != nullptr); |
120 | |
|
121 | 0 | return absl::FailedPreconditionError( |
122 | 0 | absl::StrCat(GetTypeName(), " is not convertable to JSON")); |
123 | 0 | } |
124 | | |
125 | | absl::Status OpaqueValue::Equal( |
126 | | const Value& other, |
127 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
128 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
129 | 0 | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const { |
130 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
131 | 0 | ABSL_DCHECK(message_factory != nullptr); |
132 | 0 | ABSL_DCHECK(arena != nullptr); |
133 | 0 | ABSL_DCHECK(result != nullptr); |
134 | |
|
135 | 0 | if (auto other_opaque = other.AsOpaque(); other_opaque) { |
136 | 0 | if (ABSL_PREDICT_FALSE(dispatcher_ == nullptr)) { |
137 | 0 | OpaqueValueInterface::Content content = |
138 | 0 | content_.To<OpaqueValueInterface::Content>(); |
139 | 0 | ABSL_DCHECK(content.interface != nullptr); |
140 | 0 | return content.interface->Equal(*other_opaque, descriptor_pool, |
141 | 0 | message_factory, arena, result); |
142 | 0 | } |
143 | 0 | return dispatcher_->equal(dispatcher_, content_, *other_opaque, |
144 | 0 | descriptor_pool, message_factory, arena, result); |
145 | 0 | } |
146 | 0 | *result = FalseValue(); |
147 | 0 | return absl::OkStatus(); |
148 | 0 | } |
149 | | |
150 | 0 | NativeTypeId OpaqueValue::GetTypeId() const { |
151 | 0 | ABSL_DCHECK(*this); |
152 | |
|
153 | 0 | if (ABSL_PREDICT_FALSE(dispatcher_ == nullptr)) { |
154 | 0 | OpaqueValueInterface::Content content = |
155 | 0 | content_.To<OpaqueValueInterface::Content>(); |
156 | 0 | if (content.interface == nullptr) { |
157 | 0 | return NativeTypeId(); |
158 | 0 | } |
159 | 0 | return content.interface->GetNativeTypeId(); |
160 | 0 | } |
161 | 0 | return dispatcher_->get_type_id(dispatcher_, content_); |
162 | 0 | } |
163 | | |
164 | 0 | bool OpaqueValue::IsOptional() const { |
165 | 0 | return dispatcher_ != nullptr && |
166 | 0 | dispatcher_->get_type_id(dispatcher_, content_) == |
167 | 0 | NativeTypeId::For<OptionalValue>(); |
168 | 0 | } |
169 | | |
170 | 0 | optional_ref<const OptionalValue> OpaqueValue::AsOptional() const& { |
171 | 0 | if (IsOptional()) { |
172 | 0 | return *reinterpret_cast<const OptionalValue*>(this); |
173 | 0 | } |
174 | 0 | return absl::nullopt; |
175 | 0 | } |
176 | | |
177 | 0 | absl::optional<OptionalValue> OpaqueValue::AsOptional() && { |
178 | 0 | if (IsOptional()) { |
179 | 0 | return std::move(*reinterpret_cast<OptionalValue*>(this)); |
180 | 0 | } |
181 | 0 | return absl::nullopt; |
182 | 0 | } |
183 | | |
184 | 0 | const OptionalValue& OpaqueValue::GetOptional() const& { |
185 | 0 | ABSL_DCHECK(IsOptional()) << *this; |
186 | 0 | return *reinterpret_cast<const OptionalValue*>(this); |
187 | 0 | } |
188 | | |
189 | 0 | OptionalValue OpaqueValue::GetOptional() && { |
190 | 0 | ABSL_DCHECK(IsOptional()) << *this; |
191 | 0 | return std::move(*reinterpret_cast<OptionalValue*>(this)); |
192 | 0 | } |
193 | | |
194 | | } // namespace cel |