forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_value.h
More file actions
373 lines (319 loc) · 13.6 KB
/
Copy pathstruct_value.h
File metadata and controls
373 lines (319 loc) · 13.6 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
// 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.
// IWYU pragma: private, include "common/value.h"
// IWYU pragma: friend "common/value.h"
// `StructValue` is the value representation of `StructType`. `StructValue`
// itself is a composed type of more specific runtime representations.
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRUCT_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRUCT_VALUE_H_
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.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/native_type.h"
#include "common/optional_ref.h"
#include "common/type.h"
#include "common/value_kind.h"
#include "common/values/custom_struct_value.h"
#include "common/values/legacy_struct_value.h"
#include "common/values/message_value.h"
#include "common/values/parsed_message_value.h"
#include "common/values/struct_value_variant.h"
#include "common/values/values.h"
#include "runtime/runtime_options.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/message.h"
namespace cel {
class StructValue;
class Value;
class StructValue final
: private common_internal::StructValueMixin<StructValue> {
public:
static constexpr ValueKind kKind = ValueKind::kStruct;
template <
typename T,
typename = std::enable_if_t<
common_internal::IsStructValueAlternativeV<absl::remove_cvref_t<T>>>>
// NOLINTNEXTLINE(google-explicit-constructor)
StructValue(T&& value)
: variant_(absl::in_place_type<absl::remove_cvref_t<T>>,
std::forward<T>(value)) {}
// NOLINTNEXTLINE(google-explicit-constructor)
StructValue(const MessageValue& other)
: variant_(other.ToStructValueVariant()) {}
// NOLINTNEXTLINE(google-explicit-constructor)
StructValue(MessageValue&& other)
: variant_(std::move(other).ToStructValueVariant()) {}
StructValue() = default;
StructValue(const StructValue&) = default;
StructValue(StructValue&& other) = default;
StructValue& operator=(const StructValue&) = default;
StructValue& operator=(StructValue&&) = default;
constexpr ValueKind kind() const { return kKind; }
StructType GetRuntimeType() const;
absl::string_view GetTypeName() const;
NativeTypeId GetTypeId() const;
std::string DebugString() const;
// See Value::SerializeTo().
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;
// See Value::ConvertToJson().
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;
// Like ConvertToJson(), except `json` **MUST** be an instance of
// `google.protobuf.Struct`.
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 StructValueMixin::Equal;
bool IsZeroValue() const;
absl::Status GetFieldByName(
absl::string_view name, ProtoWrapperTypeOptions unboxing_options,
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 StructValueMixin::GetFieldByName;
absl::Status GetFieldByNumber(
int64_t number, ProtoWrapperTypeOptions unboxing_options,
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 StructValueMixin::GetFieldByNumber;
absl::StatusOr<bool> HasFieldByName(absl::string_view name) const;
absl::StatusOr<bool> HasFieldByNumber(int64_t number) const;
using ForEachFieldCallback = CustomStructValueInterface::ForEachFieldCallback;
absl::Status ForEachField(
ForEachFieldCallback callback,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) const;
absl::Status Qualify(
absl::Span<const SelectQualifier> qualifiers, bool presence_test,
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,
int* absl_nonnull count) const;
using StructValueMixin::Qualify;
// Returns `true` if this value is an instance of a message value. If `true`
// is returned, it is implied that `IsOpaque()` would also return true.
bool IsMessage() const { return IsParsedMessage(); }
// 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>(); }
// 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
// `IsParsedMessage()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, bool> Is() const {
return IsParsedMessage();
}
// 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 AsMessage(); }
// 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());
}
// Convenience method for use with template metaprogramming. See
// `AsMessage()`.
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>,
absl::optional<MessageValue>>
As() & {
return AsMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>,
absl::optional<MessageValue>>
As() const& {
return AsMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>,
absl::optional<MessageValue>>
As() && {
return std::move(*this).AsMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>,
absl::optional<MessageValue>>
As() const&& {
return std::move(*this).AsMessage();
}
// Convenience method for use with template metaprogramming. See
// `AsParsedMessage()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>,
optional_ref<const ParsedMessageValue>>
As() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsParsedMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>,
optional_ref<const ParsedMessageValue>>
As() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AsParsedMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>,
absl::optional<ParsedMessageValue>>
As() && {
return std::move(*this).AsParsedMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>,
absl::optional<ParsedMessageValue>>
As() const&& {
return std::move(*this).AsParsedMessage();
}
// Performs an unchecked cast from a value to a message value. In
// debug builds a best effort is made to crash. If `IsMessage()` would return
// false, calling this method is undefined behavior.
MessageValue GetMessage() & { return std::as_const(*this).GetMessage(); }
MessageValue GetMessage() const&;
MessageValue GetMessage() &&;
MessageValue GetMessage() const&& { return GetMessage(); }
// Performs an unchecked cast from a value to a parsed message value. In
// debug builds a best effort is made to crash. If `IsParsedMessage()` would
// return false, calling this method is undefined behavior.
const ParsedMessageValue& GetParsedMessage() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return std::as_const(*this).GetParsedMessage();
}
const ParsedMessageValue& GetParsedMessage()
const& ABSL_ATTRIBUTE_LIFETIME_BOUND;
ParsedMessageValue GetParsedMessage() &&;
ParsedMessageValue GetParsedMessage() const&& { return GetParsedMessage(); }
// Convenience method for use with template metaprogramming. See
// `GetMessage()`.
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get() & {
return GetMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get() const& {
return GetMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get() && {
return std::move(*this).GetMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<MessageValue, T>, MessageValue> Get()
const&& {
return std::move(*this).GetMessage();
}
// Convenience method for use with template metaprogramming. See
// `GetParsedMessage()`.
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>,
const ParsedMessageValue&>
Get() & ABSL_ATTRIBUTE_LIFETIME_BOUND {
return GetParsedMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>,
const ParsedMessageValue&>
Get() const& ABSL_ATTRIBUTE_LIFETIME_BOUND {
return GetParsedMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, ParsedMessageValue>
Get() && {
return std::move(*this).GetParsedMessage();
}
template <typename T>
std::enable_if_t<std::is_same_v<ParsedMessageValue, T>, ParsedMessageValue>
Get() const&& {
return std::move(*this).GetParsedMessage();
}
friend void swap(StructValue& lhs, StructValue& rhs) noexcept {
using std::swap;
swap(lhs.variant_, rhs.variant_);
}
private:
friend class Value;
friend class common_internal::ValueMixin<StructValue>;
friend class common_internal::StructValueMixin<StructValue>;
common_internal::ValueVariant ToValueVariant() const&;
common_internal::ValueVariant ToValueVariant() &&;
// Unlike many of the other derived values, `StructValue` is itself a composed
// type. This is to avoid making `StructValue` too big and by extension
// `Value` too big. Instead we store the derived `StructValue` values in
// `Value` and not `StructValue` itself.
common_internal::StructValueVariant variant_;
};
inline std::ostream& operator<<(std::ostream& out, const StructValue& value) {
return out << value.DebugString();
}
template <>
struct NativeTypeTraits<StructValue> final {
static NativeTypeId Id(const StructValue& value) { return value.GetTypeId(); }
};
class StructValueBuilder {
public:
virtual ~StructValueBuilder() = default;
virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByName(
absl::string_view name, Value value) = 0;
virtual absl::StatusOr<absl::optional<ErrorValue>> SetFieldByNumber(
int64_t number, Value value) = 0;
virtual absl::StatusOr<StructValue> Build() && = 0;
};
using StructValueBuilderPtr = std::unique_ptr<StructValueBuilder>;
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_STRUCT_VALUE_H_