/proc/self/cwd/common/values/bytes_value.cc
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 | | #include <cstddef> |
16 | | #include <string> |
17 | | |
18 | | #include "google/protobuf/wrappers.pb.h" |
19 | | #include "absl/base/nullability.h" |
20 | | #include "absl/functional/overload.h" |
21 | | #include "absl/log/absl_check.h" |
22 | | #include "absl/status/status.h" |
23 | | #include "absl/strings/cord.h" |
24 | | #include "absl/strings/str_cat.h" |
25 | | #include "absl/strings/string_view.h" |
26 | | #include "common/internal/byte_string.h" |
27 | | #include "common/value.h" |
28 | | #include "internal/status_macros.h" |
29 | | #include "internal/strings.h" |
30 | | #include "internal/well_known_types.h" |
31 | | #include "google/protobuf/arena.h" |
32 | | #include "google/protobuf/descriptor.h" |
33 | | #include "google/protobuf/io/zero_copy_stream.h" |
34 | | #include "google/protobuf/message.h" |
35 | | |
36 | | namespace cel { |
37 | | |
38 | | namespace { |
39 | | |
40 | | using ::cel::well_known_types::ValueReflection; |
41 | | |
42 | | template <typename Bytes> |
43 | 0 | std::string BytesDebugString(const Bytes& value) { |
44 | 0 | return value.NativeValue(absl::Overload( |
45 | 0 | [](absl::string_view string) -> std::string { |
46 | 0 | return internal::FormatBytesLiteral(string); |
47 | 0 | }, |
48 | 0 | [](const absl::Cord& cord) -> std::string { |
49 | 0 | if (auto flat = cord.TryFlat(); flat.has_value()) { |
50 | 0 | return internal::FormatBytesLiteral(*flat); |
51 | 0 | } |
52 | 0 | return internal::FormatBytesLiteral(static_cast<std::string>(cord)); |
53 | 0 | })); |
54 | 0 | } |
55 | | |
56 | | } // namespace |
57 | | |
58 | | BytesValue BytesValue::Concat(const BytesValue& lhs, const BytesValue& rhs, |
59 | 558 | google::protobuf::Arena* absl_nonnull arena) { |
60 | 558 | return BytesValue( |
61 | 558 | common_internal::ByteString::Concat(lhs.value_, rhs.value_, arena)); |
62 | 558 | } |
63 | | |
64 | 0 | std::string BytesValue::DebugString() const { return BytesDebugString(*this); } |
65 | | |
66 | | absl::Status BytesValue::SerializeTo( |
67 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
68 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
69 | 0 | google::protobuf::io::ZeroCopyOutputStream* absl_nonnull output) const { |
70 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
71 | 0 | ABSL_DCHECK(message_factory != nullptr); |
72 | 0 | ABSL_DCHECK(output != nullptr); |
73 | |
|
74 | 0 | google::protobuf::BytesValue message; |
75 | 0 | message.set_value(NativeString()); |
76 | 0 | if (!message.SerializePartialToZeroCopyStream(output)) { |
77 | 0 | return absl::UnknownError( |
78 | 0 | absl::StrCat("failed to serialize message: ", message.GetTypeName())); |
79 | 0 | } |
80 | | |
81 | 0 | return absl::OkStatus(); |
82 | 0 | } |
83 | | |
84 | | absl::Status BytesValue::ConvertToJson( |
85 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
86 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
87 | 0 | google::protobuf::Message* absl_nonnull json) const { |
88 | 0 | ABSL_DCHECK(descriptor_pool != nullptr); |
89 | 0 | ABSL_DCHECK(message_factory != nullptr); |
90 | 0 | ABSL_DCHECK(json != nullptr); |
91 | 0 | ABSL_DCHECK_EQ(json->GetDescriptor()->well_known_type(), |
92 | 0 | google::protobuf::Descriptor::WELLKNOWNTYPE_VALUE); |
93 | |
|
94 | 0 | ValueReflection value_reflection; |
95 | 0 | CEL_RETURN_IF_ERROR(value_reflection.Initialize(json->GetDescriptor())); |
96 | 0 | NativeValue([&](const auto& value) { |
97 | 0 | value_reflection.SetStringValueFromBytes(json, value); |
98 | 0 | }); Unexecuted instantiation: bytes_value.cc:auto cel::BytesValue::ConvertToJson(google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Message*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Unexecuted instantiation: bytes_value.cc:auto cel::BytesValue::ConvertToJson(google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Message*) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
99 | |
|
100 | 0 | return absl::OkStatus(); |
101 | 0 | } |
102 | | |
103 | | absl::Status BytesValue::Equal( |
104 | | const Value& other, |
105 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
106 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
107 | 333 | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const { |
108 | 333 | ABSL_DCHECK(descriptor_pool != nullptr); |
109 | 333 | ABSL_DCHECK(message_factory != nullptr); |
110 | 333 | ABSL_DCHECK(arena != nullptr); |
111 | 333 | ABSL_DCHECK(result != nullptr); |
112 | | |
113 | 333 | if (auto other_value = other.AsBytes(); other_value.has_value()) { |
114 | 152 | *result = NativeValue([other_value](const auto& value) -> BoolValue { |
115 | 152 | return other_value->NativeValue( |
116 | 152 | [&value](const auto& other_value) -> BoolValue { |
117 | 152 | return BoolValue{value == other_value}; |
118 | 152 | }); bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const::{lambda(auto:1 const&)#1}::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) constLine | Count | Source | 116 | 152 | [&value](const auto& other_value) -> BoolValue { | 117 | 152 | return BoolValue{value == other_value}; | 118 | 152 | }); |
Unexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const::{lambda(auto:1 const&)#1}::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) constUnexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const::{lambda(auto:1 const&)#1}::operator()<std::__1::basic_string_view<char, {lambda(auto:1 const&)#1}::operator()::char_traits<char> > >(std::__1::basic_string_view<char, {lambda(auto:1 const&)#1}::operator()::char_traits<char> > const&) constUnexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const::{lambda(auto:1 const&)#1}::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
119 | 152 | }); bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Line | Count | Source | 114 | 152 | *result = NativeValue([other_value](const auto& value) -> BoolValue { | 115 | 152 | return other_value->NativeValue( | 116 | 152 | [&value](const auto& other_value) -> BoolValue { | 117 | 152 | return BoolValue{value == other_value}; | 118 | 152 | }); | 119 | 152 | }); |
Unexecuted instantiation: bytes_value.cc:cel::BoolValue cel::BytesValue::Equal(cel::Value const&, google::protobuf::DescriptorPool const*, google::protobuf::MessageFactory*, google::protobuf::Arena*, cel::Value*) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
120 | 152 | return absl::OkStatus(); |
121 | 152 | } |
122 | 181 | *result = FalseValue(); |
123 | 181 | return absl::OkStatus(); |
124 | 333 | } |
125 | | |
126 | 0 | BytesValue BytesValue::Clone(google::protobuf::Arena* absl_nonnull arena) const { |
127 | 0 | return BytesValue(value_.Clone(arena)); |
128 | 0 | } |
129 | | |
130 | 2 | size_t BytesValue::Size() const { |
131 | 2 | return NativeValue( |
132 | 2 | [](const auto& alternative) -> size_t { return alternative.size(); });bytes_value.cc:unsigned long cel::BytesValue::Size() const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Line | Count | Source | 132 | 2 | [](const auto& alternative) -> size_t { return alternative.size(); }); |
Unexecuted instantiation: bytes_value.cc:unsigned long cel::BytesValue::Size() const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
133 | 2 | } |
134 | | |
135 | 0 | bool BytesValue::IsEmpty() const { |
136 | 0 | return NativeValue( |
137 | 0 | [](const auto& alternative) -> bool { return alternative.empty(); });Unexecuted instantiation: bytes_value.cc:bool cel::BytesValue::IsEmpty() const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Unexecuted instantiation: bytes_value.cc:bool cel::BytesValue::IsEmpty() const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
138 | 0 | } |
139 | | |
140 | 18 | bool BytesValue::Equals(absl::string_view bytes) const { |
141 | 18 | return NativeValue([bytes](const auto& alternative) -> bool { |
142 | 18 | return alternative == bytes; |
143 | 18 | }); bytes_value.cc:bool cel::BytesValue::Equals(std::__1::basic_string_view<char, std::__1::char_traits<char> >) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Line | Count | Source | 141 | 18 | return NativeValue([bytes](const auto& alternative) -> bool { | 142 | 18 | return alternative == bytes; | 143 | 18 | }); |
Unexecuted instantiation: bytes_value.cc:bool cel::BytesValue::Equals(std::__1::basic_string_view<char, std::__1::char_traits<char> >) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
144 | 18 | } |
145 | | |
146 | 0 | bool BytesValue::Equals(const absl::Cord& bytes) const { |
147 | 0 | return NativeValue([&bytes](const auto& alternative) -> bool { |
148 | 0 | return alternative == bytes; |
149 | 0 | }); Unexecuted instantiation: bytes_value.cc:bool cel::BytesValue::Equals(absl::lts_20260107::Cord const&) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Unexecuted instantiation: bytes_value.cc:bool cel::BytesValue::Equals(absl::lts_20260107::Cord const&) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
150 | 0 | } |
151 | | |
152 | 18 | bool BytesValue::Equals(const BytesValue& bytes) const { |
153 | 18 | return bytes.NativeValue( |
154 | 18 | [this](const auto& alternative) -> bool { return Equals(alternative); });bytes_value.cc:bool cel::BytesValue::Equals(cel::BytesValue const&) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Line | Count | Source | 154 | 18 | [this](const auto& alternative) -> bool { return Equals(alternative); }); |
Unexecuted instantiation: bytes_value.cc:bool cel::BytesValue::Equals(cel::BytesValue const&) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
155 | 18 | } |
156 | | |
157 | | namespace { |
158 | | |
159 | 400 | int CompareImpl(absl::string_view lhs, absl::string_view rhs) { |
160 | 400 | return lhs.compare(rhs); |
161 | 400 | } |
162 | | |
163 | 0 | int CompareImpl(absl::string_view lhs, const absl::Cord& rhs) { |
164 | 0 | return -rhs.Compare(lhs); |
165 | 0 | } |
166 | | |
167 | 0 | int CompareImpl(const absl::Cord& lhs, absl::string_view rhs) { |
168 | 0 | return lhs.Compare(rhs); |
169 | 0 | } |
170 | | |
171 | 0 | int CompareImpl(const absl::Cord& lhs, const absl::Cord& rhs) { |
172 | 0 | return lhs.Compare(rhs); |
173 | 0 | } |
174 | | |
175 | | } // namespace |
176 | | |
177 | 400 | int BytesValue::Compare(absl::string_view bytes) const { |
178 | 400 | return NativeValue([bytes](const auto& alternative) -> int { |
179 | 400 | return CompareImpl(alternative, bytes); |
180 | 400 | }); bytes_value.cc:int cel::BytesValue::Compare(std::__1::basic_string_view<char, std::__1::char_traits<char> >) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Line | Count | Source | 178 | 400 | return NativeValue([bytes](const auto& alternative) -> int { | 179 | 400 | return CompareImpl(alternative, bytes); | 180 | 400 | }); |
Unexecuted instantiation: bytes_value.cc:int cel::BytesValue::Compare(std::__1::basic_string_view<char, std::__1::char_traits<char> >) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
181 | 400 | } |
182 | | |
183 | 0 | int BytesValue::Compare(const absl::Cord& bytes) const { |
184 | 0 | return NativeValue([&bytes](const auto& alternative) -> int { |
185 | 0 | return CompareImpl(alternative, bytes); |
186 | 0 | }); Unexecuted instantiation: bytes_value.cc:int cel::BytesValue::Compare(absl::lts_20260107::Cord const&) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Unexecuted instantiation: bytes_value.cc:int cel::BytesValue::Compare(absl::lts_20260107::Cord const&) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
187 | 0 | } |
188 | | |
189 | 400 | int BytesValue::Compare(const BytesValue& bytes) const { |
190 | 400 | return bytes.NativeValue( |
191 | 400 | [this](const auto& alternative) -> int { return Compare(alternative); });bytes_value.cc:int cel::BytesValue::Compare(cel::BytesValue const&) const::$_0::operator()<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) const Line | Count | Source | 191 | 400 | [this](const auto& alternative) -> int { return Compare(alternative); }); |
Unexecuted instantiation: bytes_value.cc:int cel::BytesValue::Compare(cel::BytesValue const&) const::$_0::operator()<absl::lts_20260107::Cord>(absl::lts_20260107::Cord const&) const |
192 | 400 | } |
193 | | |
194 | | } // namespace cel |