/proc/self/cwd/common/values/struct_value.h
Line | Count | Source |
1 | | // Copyright 2023 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // IWYU pragma: private, include "common/value.h" |
16 | | // IWYU pragma: friend "common/value.h" |
17 | | |
18 | | // `StructValue` is the value representation of `StructType`. `StructValue` |
19 | | // itself is a composed type of more specific runtime representations. |
20 | | |
21 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRUCT_VALUE_H_ |
22 | | #define THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRUCT_VALUE_H_ |
23 | | |
24 | | #include <cstdint> |
25 | | #include <memory> |
26 | | #include <ostream> |
27 | | #include <string> |
28 | | #include <type_traits> |
29 | | #include <utility> |
30 | | |
31 | | #include "absl/base/attributes.h" |
32 | | #include "absl/base/nullability.h" |
33 | | #include "absl/meta/type_traits.h" |
34 | | #include "absl/status/status.h" |
35 | | #include "absl/status/statusor.h" |
36 | | #include "absl/strings/string_view.h" |
37 | | #include "absl/types/optional.h" |
38 | | #include "absl/types/span.h" |
39 | | #include "absl/utility/utility.h" |
40 | | #include "base/attribute.h" |
41 | | #include "common/native_type.h" |
42 | | #include "common/optional_ref.h" |
43 | | #include "common/type.h" |
44 | | #include "common/value_kind.h" |
45 | | #include "common/values/custom_struct_value.h" |
46 | | #include "common/values/legacy_struct_value.h" |
47 | | #include "common/values/message_value.h" |
48 | | #include "common/values/parsed_message_value.h" |
49 | | #include "common/values/struct_value_variant.h" |
50 | | #include "common/values/values.h" |
51 | | #include "runtime/runtime_options.h" |
52 | | #include "google/protobuf/arena.h" |
53 | | #include "google/protobuf/descriptor.h" |
54 | | #include "google/protobuf/io/zero_copy_stream.h" |
55 | | #include "google/protobuf/message.h" |
56 | | |
57 | | #pragma push_macro("GetMessage") |
58 | | #undef GetMessage |
59 | | |
60 | | namespace cel { |
61 | | |
62 | | class StructValue; |
63 | | class Value; |
64 | | |
65 | | class StructValue final |
66 | | : private common_internal::StructValueMixin<StructValue> { |
67 | | public: |
68 | | static constexpr ValueKind kKind = ValueKind::kStruct; |
69 | | |
70 | | template < |
71 | | typename T, |
72 | | typename = std::enable_if_t< |
73 | | common_internal::IsStructValueAlternativeV<absl::remove_cvref_t<T>>>> |
74 | | // NOLINTNEXTLINE(google-explicit-constructor) |
75 | | StructValue(T&& value) |
76 | 498k | : variant_(absl::in_place_type<absl::remove_cvref_t<T>>, |
77 | 498k | std::forward<T>(value)) {}Unexecuted instantiation: cel::StructValue::StructValue<cel::ParsedMessageValue const&, void>(cel::ParsedMessageValue const&) Unexecuted instantiation: cel::StructValue::StructValue<cel::CustomStructValue const&, void>(cel::CustomStructValue const&) Unexecuted instantiation: cel::StructValue::StructValue<cel::ParsedMessageValue, void>(cel::ParsedMessageValue&&) cel::StructValue::StructValue<cel::common_internal::LegacyStructValue const&, void>(cel::common_internal::LegacyStructValue const&) Line | Count | Source | 76 | 498k | : variant_(absl::in_place_type<absl::remove_cvref_t<T>>, | 77 | 498k | std::forward<T>(value)) {} |
Unexecuted instantiation: cel::StructValue::StructValue<cel::common_internal::LegacyStructValue, void>(cel::common_internal::LegacyStructValue&&) Unexecuted instantiation: cel::StructValue::StructValue<cel::CustomStructValue, void>(cel::CustomStructValue&&) |
78 | | |
79 | | // NOLINTNEXTLINE(google-explicit-constructor) |
80 | | StructValue(const MessageValue& other) |
81 | 0 | : variant_(other.ToStructValueVariant()) {} |
82 | | |
83 | | // NOLINTNEXTLINE(google-explicit-constructor) |
84 | | StructValue(MessageValue&& other) |
85 | 0 | : variant_(std::move(other).ToStructValueVariant()) {} |
86 | | |
87 | 0 | StructValue() = default; |
88 | | StructValue(const StructValue&) = default; |
89 | | StructValue(StructValue&& other) = default; |
90 | | StructValue& operator=(const StructValue&) = default; |
91 | | StructValue& operator=(StructValue&&) = default; |
92 | | |
93 | 0 | constexpr ValueKind kind() const { return kKind; } |
94 | | |
95 | | StructType GetRuntimeType() const; |
96 | | |
97 | | absl::string_view GetTypeName() const; |
98 | | |
99 | | NativeTypeId GetTypeId() const; |
100 | | |
101 | | std::string DebugString() const; |
102 | | |
103 | | // See Value::SerializeTo(). |
104 | | absl::Status SerializeTo( |
105 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
106 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
107 | | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const; |
108 | | |
109 | | // See Value::ConvertToJson(). |
110 | | absl::Status ConvertToJson( |
111 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
112 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
113 | | google::protobuf::Message* absl_nonnull json) const; |
114 | | |
115 | | // Like ConvertToJson(), except `json` **MUST** be an instance of |
116 | | // `google.protobuf.Struct`. |
117 | | absl::Status ConvertToJsonObject( |
118 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
119 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
120 | | google::protobuf::Message* absl_nonnull json) const; |
121 | | |
122 | | absl::Status Equal(const Value& other, |
123 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
124 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
125 | | google::protobuf::Arena* absl_nonnull arena, |
126 | | Value* absl_nonnull result) const; |
127 | | using StructValueMixin::Equal; |
128 | | |
129 | | bool IsZeroValue() const; |
130 | | |
131 | | absl::Status GetFieldByName( |
132 | | absl::string_view name, ProtoWrapperTypeOptions unboxing_options, |
133 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
134 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
135 | | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const; |
136 | | using StructValueMixin::GetFieldByName; |
137 | | |
138 | | absl::Status GetFieldByNumber( |
139 | | int64_t number, ProtoWrapperTypeOptions unboxing_options, |
140 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
141 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
142 | | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const; |
143 | | using StructValueMixin::GetFieldByNumber; |
144 | | |
145 | | absl::StatusOr<bool> HasFieldByName(absl::string_view name) const; |
146 | | |
147 | | absl::StatusOr<bool> HasFieldByNumber(int64_t number) const; |
148 | | |
149 | | using ForEachFieldCallback = CustomStructValueInterface::ForEachFieldCallback; |
150 | | |
151 | | absl::Status ForEachField( |
152 | | ForEachFieldCallback callback, |
153 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
154 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
155 | | google::protobuf::Arena* absl_nonnull arena) const; |
156 | | |
157 | | absl::Status Qualify( |
158 | | absl::Span<const SelectQualifier> qualifiers, bool presence_test, |
159 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
160 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
161 | | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result, |
162 | | int* absl_nonnull count) const; |
163 | | using StructValueMixin::Qualify; |
164 | | |
165 | | // Returns `true` if this value is an instance of a message value. If `true` |
166 | | // is returned, it is implied that `IsOpaque()` would also return true. |
167 | 0 | bool IsMessage() const { return IsParsedMessage(); } |
168 | | |
169 | | // Returns `true` if this value is an instance of a parsed message value. If |
170 | | // `true` is returned, it is implied that `IsMessage()` would also return |
171 | | // true. |
172 | 0 | bool IsParsedMessage() const { return variant_.Is<ParsedMessageValue>(); } |
173 | | |
174 | | // Convenience method for use with template metaprogramming. See |
175 | | // `IsMessage()`. |
176 | | template <typename T> |
177 | | std::enable_if_t<std::is_same_v<MessageValue, T>, bool> Is() const { |
178 | | return IsMessage(); |
179 | | } |
180 | | |
181 | | // Convenience method for use with template metaprogramming. See |
182 | | // `IsParsedMessage()`. |
183 | | template <typename T> |
184 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, bool> Is() const { |
185 | | return IsParsedMessage(); |
186 | | } |
187 | | |
188 | | // Performs a checked cast from a value to a message value, |
189 | | // returning a non-empty optional with either a value or reference to the |
190 | | // message value. Otherwise an empty optional is returned. |
191 | 0 | absl::optional<MessageValue> AsMessage() & { |
192 | 0 | return std::as_const(*this).AsMessage(); |
193 | 0 | } |
194 | | absl::optional<MessageValue> AsMessage() const&; |
195 | | absl::optional<MessageValue> AsMessage() &&; |
196 | 0 | absl::optional<MessageValue> AsMessage() const&& { return AsMessage(); } |
197 | | |
198 | | // Performs a checked cast from a value to a parsed message value, |
199 | | // returning a non-empty optional with either a value or reference to the |
200 | | // parsed message value. Otherwise an empty optional is returned. |
201 | | optional_ref<const ParsedMessageValue> AsParsedMessage() & |
202 | 0 | ABSL_ATTRIBUTE_LIFETIME_BOUND { |
203 | 0 | return std::as_const(*this).AsParsedMessage(); |
204 | 0 | } |
205 | | optional_ref<const ParsedMessageValue> AsParsedMessage() |
206 | | const& ABSL_ATTRIBUTE_LIFETIME_BOUND; |
207 | | absl::optional<ParsedMessageValue> AsParsedMessage() &&; |
208 | 0 | absl::optional<ParsedMessageValue> AsParsedMessage() const&& { |
209 | 0 | return common_internal::AsOptional(AsParsedMessage()); |
210 | 0 | } |
211 | | |
212 | | // Convenience method for use with template metaprogramming. See |
213 | | // `AsMessage()`. |
214 | | template <typename T> |
215 | | std::enable_if_t<std::is_same_v<MessageValue, T>, |
216 | | absl::optional<MessageValue>> |
217 | | As() & { |
218 | | return AsMessage(); |
219 | | } |
220 | | template <typename T> |
221 | | std::enable_if_t<std::is_same_v<MessageValue, T>, |
222 | | absl::optional<MessageValue>> |
223 | | As() const& { |
224 | | return AsMessage(); |
225 | | } |
226 | | template <typename T> |
227 | | std::enable_if_t<std::is_same_v<MessageValue, T>, |
228 | | absl::optional<MessageValue>> |
229 | | As() && { |
230 | | return std::move(*this).AsMessage(); |
231 | | } |
232 | | template <typename T> |
233 | | std::enable_if_t<std::is_same_v<MessageValue, T>, |
234 | | absl::optional<MessageValue>> |
235 | | As() const&& { |
236 | | return std::move(*this).AsMessage(); |
237 | | } |
238 | | |
239 | | // Convenience method for use with template metaprogramming. See |
240 | | // `AsParsedMessage()`. |
241 | | template <typename T> |
242 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, |
243 | | optional_ref<const ParsedMessageValue>> |
244 | | As() & ABSL_ATTRIBUTE_LIFETIME_BOUND { |
245 | | return AsParsedMessage(); |
246 | | } |
247 | | template <typename T> |
248 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, |
249 | | optional_ref<const ParsedMessageValue>> |
250 | | As() const& ABSL_ATTRIBUTE_LIFETIME_BOUND { |
251 | | return AsParsedMessage(); |
252 | | } |
253 | | template <typename T> |
254 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, |
255 | | absl::optional<ParsedMessageValue>> |
256 | | As() && { |
257 | | return std::move(*this).AsParsedMessage(); |
258 | | } |
259 | | template <typename T> |
260 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, |
261 | | absl::optional<ParsedMessageValue>> |
262 | | As() const&& { |
263 | | return std::move(*this).AsParsedMessage(); |
264 | | } |
265 | | |
266 | | // Performs an unchecked cast from a value to a message value. In |
267 | | // debug builds a best effort is made to crash. If `IsMessage()` would return |
268 | | // false, calling this method is undefined behavior. |
269 | 0 | MessageValue GetMessage() & { return std::as_const(*this).GetMessage(); } |
270 | | MessageValue GetMessage() const&; |
271 | | MessageValue GetMessage() &&; |
272 | 0 | MessageValue GetMessage() const&& { return GetMessage(); } |
273 | | |
274 | | // Performs an unchecked cast from a value to a parsed message value. In |
275 | | // debug builds a best effort is made to crash. If `IsParsedMessage()` would |
276 | | // return false, calling this method is undefined behavior. |
277 | 0 | const ParsedMessageValue& GetParsedMessage() & ABSL_ATTRIBUTE_LIFETIME_BOUND { |
278 | 0 | return std::as_const(*this).GetParsedMessage(); |
279 | 0 | } |
280 | | const ParsedMessageValue& GetParsedMessage() |
281 | | const& ABSL_ATTRIBUTE_LIFETIME_BOUND; |
282 | | ParsedMessageValue GetParsedMessage() &&; |
283 | 0 | ParsedMessageValue GetParsedMessage() const&& { return GetParsedMessage(); } |
284 | | |
285 | | // Convenience method for use with template metaprogramming. See |
286 | | // `GetMessage()`. |
287 | | template <typename T> |
288 | | std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get() & { |
289 | | return GetMessage(); |
290 | | } |
291 | | template <typename T> |
292 | | std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get() const& { |
293 | | return GetMessage(); |
294 | | } |
295 | | template <typename T> |
296 | | std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get() && { |
297 | | return std::move(*this).GetMessage(); |
298 | | } |
299 | | template <typename T> |
300 | | std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get() |
301 | | const&& { |
302 | | return std::move(*this).GetMessage(); |
303 | | } |
304 | | |
305 | | // Convenience method for use with template metaprogramming. See |
306 | | // `GetParsedMessage()`. |
307 | | template <typename T> |
308 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, |
309 | | const ParsedMessageValue&> |
310 | | Get() & ABSL_ATTRIBUTE_LIFETIME_BOUND { |
311 | | return GetParsedMessage(); |
312 | | } |
313 | | template <typename T> |
314 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, |
315 | | const ParsedMessageValue&> |
316 | | Get() const& ABSL_ATTRIBUTE_LIFETIME_BOUND { |
317 | | return GetParsedMessage(); |
318 | | } |
319 | | template <typename T> |
320 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, ParsedMessageValue> |
321 | | Get() && { |
322 | | return std::move(*this).GetParsedMessage(); |
323 | | } |
324 | | template <typename T> |
325 | | std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, ParsedMessageValue> |
326 | | Get() const&& { |
327 | | return std::move(*this).GetParsedMessage(); |
328 | | } |
329 | | |
330 | 0 | friend void swap(StructValue& lhs, StructValue& rhs) noexcept { |
331 | 0 | using std::swap; |
332 | 0 | swap(lhs.variant_, rhs.variant_); |
333 | 0 | } |
334 | | |
335 | | private: |
336 | | friend class Value; |
337 | | friend class common_internal::ValueMixin<StructValue>; |
338 | | friend class common_internal::StructValueMixin<StructValue>; |
339 | | |
340 | | common_internal::ValueVariant ToValueVariant() const&; |
341 | | common_internal::ValueVariant ToValueVariant() &&; |
342 | | |
343 | | // Unlike many of the other derived values, `StructValue` is itself a composed |
344 | | // type. This is to avoid making `StructValue` too big and by extension |
345 | | // `Value` too big. Instead we store the derived `StructValue` values in |
346 | | // `Value` and not `StructValue` itself. |
347 | | common_internal::StructValueVariant variant_; |
348 | | }; |
349 | | |
350 | 0 | inline std::ostream& operator<<(std::ostream& out, const StructValue& value) { |
351 | 0 | return out << value.DebugString(); |
352 | 0 | } |
353 | | |
354 | | template <> |
355 | | struct NativeTypeTraits<StructValue> final { |
356 | 0 | static NativeTypeId Id(const StructValue& value) { return value.GetTypeId(); } |
357 | | }; |
358 | | |
359 | | class StructValueBuilder { |
360 | | public: |
361 | 0 | virtual ~StructValueBuilder() = default; |
362 | | |
363 | | virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByName( |
364 | | absl::string_view name, Value value) = 0; |
365 | | |
366 | | virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByNumber( |
367 | | int64_t number, Value value) = 0; |
368 | | |
369 | | virtual absl::StatusOr<StructValue> Build() && = 0; |
370 | | }; |
371 | | |
372 | | using StructValueBuilderPtr = std::unique_ptr<StructValueBuilder>; |
373 | | |
374 | | } // namespace cel |
375 | | |
376 | | #pragma pop_macro("GetMessage") |
377 | | |
378 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRUCT_VALUE_H_ |