/proc/self/cwd/common/legacy_value.h
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 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_LEGACY_VALUE_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_COMMON_LEGACY_VALUE_H_ |
17 | | |
18 | | #include <cstdint> |
19 | | #include <vector> |
20 | | |
21 | | #include "absl/base/nullability.h" |
22 | | #include "absl/status/status.h" |
23 | | #include "absl/status/statusor.h" |
24 | | #include "absl/strings/string_view.h" |
25 | | #include "absl/time/time.h" |
26 | | #include "absl/types/span.h" |
27 | | #include "common/value.h" |
28 | | #include "eval/public/cel_value.h" |
29 | | #include "internal/status_macros.h" |
30 | | #include "runtime/runtime_options.h" |
31 | | #include "google/protobuf/arena.h" |
32 | | #include "google/protobuf/descriptor.h" |
33 | | |
34 | | namespace cel { |
35 | | |
36 | | absl::Status ModernValue(google::protobuf::Arena* arena, |
37 | | google::api::expr::runtime::CelValue legacy_value, |
38 | | Value& result); |
39 | | inline absl::StatusOr<Value> ModernValue( |
40 | 245k | google::protobuf::Arena* arena, google::api::expr::runtime::CelValue legacy_value) { |
41 | 245k | Value result; |
42 | 245k | CEL_RETURN_IF_ERROR(ModernValue(arena, legacy_value, result)); |
43 | 245k | return result; |
44 | 245k | } |
45 | | |
46 | | absl::StatusOr<google::api::expr::runtime::CelValue> LegacyValue( |
47 | | google::protobuf::Arena* arena, const Value& modern_value); |
48 | | |
49 | | namespace common_internal { |
50 | | |
51 | | // Convert a `cel::Value` to `google::api::expr::runtime::CelValue`, using |
52 | | // `arena` to make memory allocations if necessary. `stable` indicates whether |
53 | | // `cel::Value` is in a location where it will not be moved, so that inline |
54 | | // string/bytes storage can be referenced. |
55 | | google::api::expr::runtime::CelValue UnsafeLegacyValue( |
56 | | const Value& value, bool stable, google::protobuf::Arena* absl_nonnull arena); |
57 | | |
58 | | } // namespace common_internal |
59 | | |
60 | | } // namespace cel |
61 | | |
62 | | namespace proto2 { |
63 | | class MessageFactory; |
64 | | } // namespace proto2 |
65 | | |
66 | | namespace cel::interop_internal { |
67 | | |
68 | 245k | inline bool IsUnsafeParsedMessageValue(const cel::ParsedMessageValue& value) { |
69 | 245k | return value.is_unsafe(); |
70 | 245k | } |
71 | | |
72 | | // Returns the underlying `google::protobuf::Message` of a `cel::Value` if it is a legacy |
73 | | // message with the default type info, or `nullptr` otherwise. |
74 | | const google::protobuf::Message* absl_nullable GetLegacyMessage(const Value& value); |
75 | | |
76 | | // Helper for wrapping a field accesses for the legacy runtime. |
77 | | // |
78 | | // Adapts the output to avoid further allocations when converting to a legacy |
79 | | // value when possible. |
80 | | void WrapLegacyFieldAccessResult(google::protobuf::Arena* absl_nonnull arena, |
81 | | Value* absl_nonnull result); |
82 | | |
83 | | // Access a field on a legacy message value, writing the result to `out`. |
84 | | // Prefers wrapping legacy values instead of using the modern value |
85 | | // representation. |
86 | | absl::Status WrapLegacyMessageField( |
87 | | const google::protobuf::Message* absl_nonnull message, |
88 | | const google::protobuf::FieldDescriptor* absl_nonnull field_descriptor, |
89 | | ProtoWrapperTypeOptions unboxing_option, |
90 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
91 | | google::protobuf::MessageFactory* absl_nonnull message_factory, google::protobuf::Arena* arena, |
92 | | Value* absl_nonnull out); |
93 | | |
94 | | absl::StatusOr<Value> FromLegacyValue( |
95 | | google::protobuf::Arena* arena, |
96 | | const google::api::expr::runtime::CelValue& legacy_value, |
97 | | bool unchecked = false); |
98 | | |
99 | | absl::StatusOr<google::api::expr::runtime::CelValue> ToLegacyValue( |
100 | | google::protobuf::Arena* arena, const Value& value, bool unchecked = false); |
101 | | |
102 | 0 | inline NullValue CreateNullValue() { return NullValue{}; } |
103 | | |
104 | 0 | inline BoolValue CreateBoolValue(bool value) { return BoolValue{value}; } |
105 | | |
106 | 0 | inline IntValue CreateIntValue(int64_t value) { return IntValue{value}; } |
107 | | |
108 | 0 | inline UintValue CreateUintValue(uint64_t value) { return UintValue{value}; } |
109 | | |
110 | 0 | inline DoubleValue CreateDoubleValue(double value) { |
111 | 0 | return DoubleValue{value}; |
112 | 0 | } |
113 | | |
114 | | inline ListValue CreateLegacyListValue( |
115 | 0 | const google::api::expr::runtime::CelList* value) { |
116 | 0 | return common_internal::LegacyListValue(value); |
117 | 0 | } |
118 | | |
119 | | inline MapValue CreateLegacyMapValue( |
120 | 0 | const google::api::expr::runtime::CelMap* value) { |
121 | 0 | return common_internal::LegacyMapValue(value); |
122 | 0 | } |
123 | | |
124 | | Value LegacyValueToModernValueOrDie( |
125 | | google::protobuf::Arena* arena, const google::api::expr::runtime::CelValue& value, |
126 | | bool unchecked = false); |
127 | | std::vector<Value> LegacyValueToModernValueOrDie( |
128 | | google::protobuf::Arena* arena, |
129 | | absl::Span<const google::api::expr::runtime::CelValue> values, |
130 | | bool unchecked = false); |
131 | | |
132 | | google::api::expr::runtime::CelValue ModernValueToLegacyValueOrDie( |
133 | | google::protobuf::Arena* arena, const Value& value, bool unchecked = false); |
134 | | |
135 | | TypeValue CreateTypeValueFromView(google::protobuf::Arena* arena, |
136 | | absl::string_view input); |
137 | | |
138 | | } // namespace cel::interop_internal |
139 | | |
140 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_LEGACY_VALUE_H_ |