forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_value.cc
More file actions
271 lines (226 loc) · 8.24 KB
/
Copy pathstring_value.cc
File metadata and controls
271 lines (226 loc) · 8.24 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
// 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.
#include <cstddef>
#include <string>
#include <utility>
#include "absl/functional/overload.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "common/any.h"
#include "common/casting.h"
#include "common/json.h"
#include "common/value.h"
#include "internal/serialize.h"
#include "internal/status_macros.h"
#include "internal/strings.h"
#include "internal/utf8.h"
namespace cel {
namespace {
template <typename Bytes>
std::string StringDebugString(const Bytes& value) {
return value.NativeValue(absl::Overload(
[](absl::string_view string) -> std::string {
return internal::FormatStringLiteral(string);
},
[](const absl::Cord& cord) -> std::string {
if (auto flat = cord.TryFlat(); flat.has_value()) {
return internal::FormatStringLiteral(*flat);
}
return internal::FormatStringLiteral(static_cast<std::string>(cord));
}));
}
} // namespace
std::string StringValue::DebugString() const {
return StringDebugString(*this);
}
absl::StatusOr<size_t> StringValue::GetSerializedSize(
AnyToJsonConverter&) const {
return NativeValue([](const auto& bytes) -> size_t {
return internal::SerializedStringValueSize(bytes);
});
}
absl::Status StringValue::SerializeTo(AnyToJsonConverter&,
absl::Cord& value) const {
return NativeValue([&value](const auto& bytes) -> absl::Status {
return internal::SerializeStringValue(bytes, value);
});
}
absl::StatusOr<absl::Cord> StringValue::Serialize(
AnyToJsonConverter& value_manager) const {
absl::Cord value;
CEL_RETURN_IF_ERROR(SerializeTo(value_manager, value));
return value;
}
absl::StatusOr<std::string> StringValue::GetTypeUrl(
absl::string_view prefix) const {
return MakeTypeUrlWithPrefix(prefix, "google.protobuf.StringValue");
}
absl::StatusOr<Any> StringValue::ConvertToAny(AnyToJsonConverter& value_manager,
absl::string_view prefix) const {
CEL_ASSIGN_OR_RETURN(auto value, Serialize(value_manager));
CEL_ASSIGN_OR_RETURN(auto type_url, GetTypeUrl(prefix));
return MakeAny(std::move(type_url), std::move(value));
}
absl::StatusOr<Json> StringValue::ConvertToJson(AnyToJsonConverter&) const {
return NativeCord();
}
absl::StatusOr<ValueView> StringValue::Equal(ValueManager&, ValueView other,
Value&) const {
if (auto other_value = As<StringValueView>(other); other_value.has_value()) {
return NativeValue([other_value](const auto& value) -> BoolValueView {
return other_value->NativeValue(
[&value](const auto& other_value) -> BoolValueView {
return BoolValueView{value == other_value};
});
});
}
return BoolValueView{false};
}
size_t StringValue::Size() const {
return NativeValue([](const auto& alternative) -> size_t {
return internal::Utf8CodePointCount(alternative);
});
}
bool StringValue::IsEmpty() const {
return NativeValue(
[](const auto& alternative) -> bool { return alternative.empty(); });
}
bool StringValue::Equals(absl::string_view string) const {
return NativeValue([string](const auto& alternative) -> bool {
return alternative == string;
});
}
bool StringValue::Equals(const absl::Cord& string) const {
return NativeValue([&string](const auto& alternative) -> bool {
return alternative == string;
});
}
bool StringValue::Equals(StringValueView string) const {
return string.NativeValue(
[this](const auto& alternative) -> bool { return Equals(alternative); });
}
namespace {
int CompareImpl(absl::string_view lhs, absl::string_view rhs) {
return lhs.compare(rhs);
}
int CompareImpl(absl::string_view lhs, const absl::Cord& rhs) {
return -rhs.Compare(lhs);
}
int CompareImpl(const absl::Cord& lhs, absl::string_view rhs) {
return lhs.Compare(rhs);
}
int CompareImpl(const absl::Cord& lhs, const absl::Cord& rhs) {
return lhs.Compare(rhs);
}
} // namespace
int StringValue::Compare(absl::string_view string) const {
return NativeValue([string](const auto& alternative) -> int {
return CompareImpl(alternative, string);
});
}
int StringValue::Compare(const absl::Cord& string) const {
return NativeValue([&string](const auto& alternative) -> int {
return CompareImpl(alternative, string);
});
}
int StringValue::Compare(StringValueView string) const {
return string.NativeValue(
[this](const auto& alternative) -> int { return Compare(alternative); });
}
std::string StringValueView::DebugString() const {
return StringDebugString(*this);
}
absl::StatusOr<size_t> StringValueView::GetSerializedSize(
AnyToJsonConverter&) const {
return NativeValue([](const auto& bytes) -> size_t {
return internal::SerializedStringValueSize(bytes);
});
}
absl::Status StringValueView::SerializeTo(AnyToJsonConverter&,
absl::Cord& value) const {
return NativeValue([&value](const auto& bytes) -> absl::Status {
return internal::SerializeStringValue(bytes, value);
});
}
absl::StatusOr<absl::Cord> StringValueView::Serialize(
AnyToJsonConverter& value_manager) const {
absl::Cord value;
CEL_RETURN_IF_ERROR(SerializeTo(value_manager, value));
return value;
}
absl::StatusOr<std::string> StringValueView::GetTypeUrl(
absl::string_view prefix) const {
return MakeTypeUrlWithPrefix(prefix, "google.protobuf.StringValue");
}
absl::StatusOr<Any> StringValueView::ConvertToAny(
AnyToJsonConverter& value_manager, absl::string_view prefix) const {
CEL_ASSIGN_OR_RETURN(auto value, Serialize(value_manager));
CEL_ASSIGN_OR_RETURN(auto type_url, GetTypeUrl(prefix));
return MakeAny(std::move(type_url), std::move(value));
}
absl::StatusOr<Json> StringValueView::ConvertToJson(AnyToJsonConverter&) const {
return NativeCord();
}
absl::StatusOr<ValueView> StringValueView::Equal(ValueManager&, ValueView other,
Value&) const {
if (auto other_value = As<StringValueView>(other); other_value.has_value()) {
return NativeValue([other_value](const auto& value) -> BoolValueView {
return other_value->NativeValue(
[&value](const auto& other_value) -> BoolValueView {
return BoolValueView{value == other_value};
});
});
}
return BoolValueView{false};
}
size_t StringValueView::Size() const {
return NativeValue([](const auto& alternative) -> size_t {
return internal::Utf8CodePointCount(alternative);
});
}
bool StringValueView::IsEmpty() const {
return NativeValue(
[](const auto& alternative) -> bool { return alternative.empty(); });
}
bool StringValueView::Equals(absl::string_view string) const {
return NativeValue([string](const auto& alternative) -> bool {
return alternative == string;
});
}
bool StringValueView::Equals(const absl::Cord& string) const {
return NativeValue([&string](const auto& alternative) -> bool {
return alternative == string;
});
}
bool StringValueView::Equals(StringValueView string) const {
return string.NativeValue(
[this](const auto& alternative) -> bool { return Equals(alternative); });
}
int StringValueView::Compare(absl::string_view string) const {
return NativeValue([string](const auto& alternative) -> int {
return CompareImpl(alternative, string);
});
}
int StringValueView::Compare(const absl::Cord& string) const {
return NativeValue([&string](const auto& alternative) -> int {
return CompareImpl(alternative, string);
});
}
int StringValueView::Compare(StringValueView string) const {
return string.NativeValue(
[this](const auto& alternative) -> int { return Compare(alternative); });
}
} // namespace cel