forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.cc
More file actions
414 lines (370 loc) · 17.2 KB
/
Copy pathstrings.cc
File metadata and controls
414 lines (370 loc) · 17.2 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2024 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 "extensions/strings.h"
#include <cstdint>
#include <string>
#include <utility>
#include "absl/base/no_destructor.h"
#include "absl/base/nullability.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "checker/internal/builtins_arena.h"
#include "checker/type_checker_builder.h"
#include "common/decl.h"
#include "common/type.h"
#include "common/value.h"
#include "eval/public/cel_function_registry.h"
#include "eval/public/cel_options.h"
#include "extensions/formatting.h"
#include "internal/status_macros.h"
#include "runtime/function_adapter.h"
#include "runtime/function_registry.h"
#include "runtime/runtime_options.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel::extensions {
namespace {
using ::cel::checker_internal::BuiltinsArena;
struct AppendToStringVisitor {
std::string& append_to;
void operator()(absl::string_view string) const { append_to.append(string); }
void operator()(const absl::Cord& cord) const {
append_to.append(static_cast<std::string>(cord));
}
};
absl::StatusOr<Value> Join2(
const ListValue& value, const StringValue& separator,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) {
return separator.Join(value, descriptor_pool, message_factory, arena);
}
absl::StatusOr<Value> Join1(
const ListValue& value,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) {
return StringValue().Join(value, descriptor_pool, message_factory, arena);
}
absl::StatusOr<Value> Split3(
const StringValue& string, const StringValue& delimiter, int64_t limit,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) {
return string.Split(delimiter, limit, arena);
}
absl::StatusOr<Value> Split2(
const StringValue& string, const StringValue& delimiter,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) {
return string.Split(delimiter, arena);
}
absl::StatusOr<Value> Replace2(const StringValue& string,
const StringValue& old_sub,
const StringValue& new_sub, int64_t limit,
const google::protobuf::DescriptorPool* absl_nonnull,
google::protobuf::MessageFactory* absl_nonnull,
google::protobuf::Arena* absl_nonnull arena) {
return string.Replace(old_sub, new_sub, limit, arena);
}
absl::StatusOr<Value> Replace1(
const StringValue& string, const StringValue& old_sub,
const StringValue& new_sub,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) {
return string.Replace(old_sub, new_sub, -1, arena);
}
Value CharAt(const StringValue& string, int64_t pos) {
return string.CharAt(pos);
}
int64_t IndexOf2(const StringValue& haystack, const StringValue& needle) {
return haystack.IndexOf(needle).value_or(-1);
}
Value IndexOf3(const StringValue& haystack, const StringValue& needle,
int64_t pos) {
if (pos > haystack.Size()) {
return ErrorValue{
absl::InvalidArgumentError(absl::StrCat("index out of range: ", pos))};
}
return IntValue(haystack.IndexOf(needle, pos).value_or(-1));
}
int64_t LastIndexOf2(const StringValue& haystack, const StringValue& needle) {
return haystack.LastIndexOf(needle).value_or(-1);
}
Value LastIndexOf3(const StringValue& haystack, const StringValue& needle,
int64_t pos) {
if (pos < 0 || pos > haystack.Size()) {
return ErrorValue{
absl::InvalidArgumentError(absl::StrCat("index out of range: ", pos))};
}
return IntValue(haystack.LastIndexOf(needle, pos).value_or(-1));
}
Value Substring2(const StringValue& string, int64_t start) {
return string.Substring(start);
}
Value Substring3(const StringValue& string, int64_t start, int64_t end) {
return string.Substring(start, end);
}
StringValue Trim(const StringValue& string) { return string.Trim(); }
StringValue LowerAscii(const StringValue& string,
const google::protobuf::DescriptorPool* absl_nonnull,
google::protobuf::MessageFactory* absl_nonnull,
google::protobuf::Arena* absl_nonnull arena) {
return string.LowerAscii(arena);
}
StringValue UpperAscii(const StringValue& string,
const google::protobuf::DescriptorPool* absl_nonnull,
google::protobuf::MessageFactory* absl_nonnull,
google::protobuf::Arena* absl_nonnull arena) {
return string.UpperAscii(arena);
}
StringValue Quote(const StringValue& string,
const google::protobuf::DescriptorPool* absl_nonnull,
google::protobuf::MessageFactory* absl_nonnull,
google::protobuf::Arena* absl_nonnull arena) {
return string.Quote(arena);
}
StringValue Reverse(const StringValue& string,
const google::protobuf::DescriptorPool* absl_nonnull,
google::protobuf::MessageFactory* absl_nonnull,
google::protobuf::Arena* absl_nonnull arena) {
return string.Reverse(arena);
}
const Type& ListStringType() {
static absl::NoDestructor<Type> kInstance(
ListType(BuiltinsArena(), StringType()));
return *kInstance;
}
absl::Status RegisterStringsDecls(TypeCheckerBuilder& builder, int version) {
// Runtime Supported functions.
CEL_ASSIGN_OR_RETURN(
auto join_decl,
MakeFunctionDecl(
"join",
MakeMemberOverloadDecl("list_join", StringType(), ListStringType()),
MakeMemberOverloadDecl("list_join_string", StringType(),
ListStringType(), StringType())));
CEL_ASSIGN_OR_RETURN(
auto split_decl,
MakeFunctionDecl(
"split",
MakeMemberOverloadDecl("string_split_string", ListStringType(),
StringType(), StringType()),
MakeMemberOverloadDecl("string_split_string_int", ListStringType(),
StringType(), StringType(), IntType())));
CEL_ASSIGN_OR_RETURN(
auto lower_decl,
MakeFunctionDecl("lowerAscii",
MakeMemberOverloadDecl("string_lower_ascii",
StringType(), StringType())));
CEL_ASSIGN_OR_RETURN(
auto replace_decl,
MakeFunctionDecl(
"replace",
MakeMemberOverloadDecl("string_replace_string_string", StringType(),
StringType(), StringType(), StringType()),
MakeMemberOverloadDecl("string_replace_string_string_int",
StringType(), StringType(), StringType(),
StringType(), IntType())));
// Additional functions described in the spec.
CEL_ASSIGN_OR_RETURN(
auto char_at_decl,
MakeFunctionDecl(
"charAt", MakeMemberOverloadDecl("string_char_at_int", StringType(),
StringType(), IntType())));
CEL_ASSIGN_OR_RETURN(
auto index_of_decl,
MakeFunctionDecl(
"indexOf",
MakeMemberOverloadDecl("string_index_of_string", IntType(),
StringType(), StringType()),
MakeMemberOverloadDecl("string_index_of_string_int", IntType(),
StringType(), StringType(), IntType())));
CEL_ASSIGN_OR_RETURN(
auto last_index_of_decl,
MakeFunctionDecl(
"lastIndexOf",
MakeMemberOverloadDecl("string_last_index_of_string", IntType(),
StringType(), StringType()),
MakeMemberOverloadDecl("string_last_index_of_string_int", IntType(),
StringType(), StringType(), IntType())));
CEL_ASSIGN_OR_RETURN(
auto substring_decl,
MakeFunctionDecl(
"substring",
MakeMemberOverloadDecl("string_substring_int", StringType(),
StringType(), IntType()),
MakeMemberOverloadDecl("string_substring_int_int", StringType(),
StringType(), IntType(), IntType())));
CEL_ASSIGN_OR_RETURN(
auto upper_ascii_decl,
MakeFunctionDecl("upperAscii",
MakeMemberOverloadDecl("string_upper_ascii",
StringType(), StringType())));
CEL_ASSIGN_OR_RETURN(
auto format_decl,
MakeFunctionDecl("format",
MakeMemberOverloadDecl("string_format", StringType(),
StringType(), ListType())));
CEL_ASSIGN_OR_RETURN(
auto quote_decl,
MakeFunctionDecl(
"strings.quote",
MakeOverloadDecl("strings_quote", StringType(), StringType())));
CEL_ASSIGN_OR_RETURN(
auto reverse_decl,
MakeFunctionDecl("reverse",
MakeMemberOverloadDecl("string_reverse", StringType(),
StringType())));
CEL_ASSIGN_OR_RETURN(
auto trim_decl,
MakeFunctionDecl("trim", MakeMemberOverloadDecl(
"string_trim", StringType(), StringType())));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(split_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(lower_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(replace_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(char_at_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(index_of_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(last_index_of_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(substring_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(upper_ascii_decl)));
CEL_RETURN_IF_ERROR(builder.MergeFunction(std::move(trim_decl)));
if (version == 0) {
return absl::OkStatus();
}
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(format_decl)));
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(quote_decl)));
if (version == 1) {
return absl::OkStatus();
}
CEL_RETURN_IF_ERROR(builder.AddFunction(std::move(join_decl)));
if (version == 2) {
return absl::OkStatus();
}
// MergeFunction is used to combine with the reverse function
// defined in cel.lib.ext.lists extension.
CEL_RETURN_IF_ERROR(builder.MergeFunction(std::move(reverse_decl)));
return absl::OkStatus();
}
} // namespace
absl::Status RegisterStringsFunctions(FunctionRegistry& registry,
const RuntimeOptions& options) {
CEL_RETURN_IF_ERROR(registry.Register(
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::CreateDescriptor(
"join", /*receiver_style=*/true),
UnaryFunctionAdapter<absl::StatusOr<Value>, ListValue>::WrapFunction(
Join1)));
CEL_RETURN_IF_ERROR(registry.Register(
BinaryFunctionAdapter<absl::StatusOr<Value>, ListValue, StringValue>::
CreateDescriptor("join", /*receiver_style=*/true),
BinaryFunctionAdapter<absl::StatusOr<Value>, ListValue,
StringValue>::WrapFunction(Join2)));
CEL_RETURN_IF_ERROR(registry.Register(
BinaryFunctionAdapter<absl::StatusOr<Value>, StringValue, StringValue>::
CreateDescriptor("split", /*receiver_style=*/true),
BinaryFunctionAdapter<absl::StatusOr<Value>, StringValue,
StringValue>::WrapFunction(Split2)));
CEL_RETURN_IF_ERROR(registry.Register(
TernaryFunctionAdapter<
absl::StatusOr<Value>, StringValue, StringValue,
int64_t>::CreateDescriptor("split", /*receiver_style=*/true),
TernaryFunctionAdapter<absl::StatusOr<Value>, StringValue, StringValue,
int64_t>::WrapFunction(Split3)));
CEL_RETURN_IF_ERROR(registry.Register(
UnaryFunctionAdapter<absl::StatusOr<Value>, StringValue>::
CreateDescriptor("lowerAscii", /*receiver_style=*/true),
UnaryFunctionAdapter<absl::StatusOr<Value>, StringValue>::WrapFunction(
LowerAscii)));
CEL_RETURN_IF_ERROR(registry.Register(
UnaryFunctionAdapter<absl::StatusOr<Value>, StringValue>::
CreateDescriptor("upperAscii", /*receiver_style=*/true),
UnaryFunctionAdapter<absl::StatusOr<Value>, StringValue>::WrapFunction(
UpperAscii)));
CEL_RETURN_IF_ERROR(registry.Register(
TernaryFunctionAdapter<
absl::StatusOr<Value>, StringValue, StringValue,
StringValue>::CreateDescriptor("replace", /*receiver_style=*/true),
TernaryFunctionAdapter<absl::StatusOr<Value>, StringValue, StringValue,
StringValue>::WrapFunction(Replace1)));
CEL_RETURN_IF_ERROR(registry.Register(
QuaternaryFunctionAdapter<
absl::StatusOr<Value>, StringValue, StringValue, StringValue,
int64_t>::CreateDescriptor("replace", /*receiver_style=*/true),
QuaternaryFunctionAdapter<absl::StatusOr<Value>, StringValue, StringValue,
StringValue, int64_t>::WrapFunction(Replace2)));
CEL_RETURN_IF_ERROR(RegisterStringFormattingFunctions(registry, options));
CEL_RETURN_IF_ERROR(
(BinaryFunctionAdapter<Value, StringValue,
int64_t>::RegisterMemberOverload("charAt", &CharAt,
registry)));
CEL_RETURN_IF_ERROR(
(BinaryFunctionAdapter<int64_t, StringValue,
StringValue>::RegisterMemberOverload("indexOf",
&IndexOf2,
registry)));
CEL_RETURN_IF_ERROR(
(TernaryFunctionAdapter<Value, StringValue, StringValue,
int64_t>::RegisterMemberOverload("indexOf",
&IndexOf3,
registry)));
CEL_RETURN_IF_ERROR(
(BinaryFunctionAdapter<int64_t, StringValue,
StringValue>::RegisterMemberOverload("lastIndexOf",
&LastIndexOf2,
registry)));
CEL_RETURN_IF_ERROR(
(TernaryFunctionAdapter<Value, StringValue, StringValue,
int64_t>::RegisterMemberOverload("lastIndexOf",
&LastIndexOf3,
registry)));
CEL_RETURN_IF_ERROR(
(BinaryFunctionAdapter<Value, StringValue,
int64_t>::RegisterMemberOverload("substring",
&Substring2,
registry)));
CEL_RETURN_IF_ERROR(
(TernaryFunctionAdapter<Value, StringValue, int64_t,
int64_t>::RegisterMemberOverload("substring",
&Substring3,
registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<StringValue, StringValue>::RegisterMemberOverload(
"trim", &Trim, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<StringValue, StringValue>::RegisterGlobalOverload(
"strings.quote", &Quote, registry)));
CEL_RETURN_IF_ERROR(
(UnaryFunctionAdapter<StringValue, StringValue>::RegisterMemberOverload(
"reverse", &Reverse, registry)));
return absl::OkStatus();
}
absl::Status RegisterStringsFunctions(
google::api::expr::runtime::CelFunctionRegistry* registry,
const google::api::expr::runtime::InterpreterOptions& options) {
return RegisterStringsFunctions(
registry->InternalGetRegistry(),
google::api::expr::runtime::ConvertToRuntimeOptions(options));
}
CheckerLibrary StringsCheckerLibrary(int version) {
return {"strings", [version](TypeCheckerBuilder& builder) {
return RegisterStringsDecls(builder, version);
}};
}
} // namespace cel::extensions