-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCelExtensions.java
More file actions
408 lines (375 loc) · 14 KB
/
CelExtensions.java
File metadata and controls
408 lines (375 loc) · 14 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
// 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.
package dev.cel.extensions;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Arrays.stream;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.errorprone.annotations.InlineMe;
import dev.cel.common.CelOptions;
import dev.cel.extensions.CelMathExtensions.Function;
import java.util.Set;
/**
* Collections of CEL Extensions.
*
* <p>To use, supply the desired extensions into {@code CelCompiler} and {@code CelRuntime} through
* their builders: {@code CelCompilerBuilder#addLibraries} and {@code
* CelRuntimeBuilder#addLibraries}.
*/
public final class CelExtensions {
private static final CelStringExtensions STRING_EXTENSIONS_ALL = new CelStringExtensions();
private static final CelProtoExtensions PROTO_EXTENSIONS = new CelProtoExtensions();
private static final CelBindingsExtensions BINDINGS_EXTENSIONS = new CelBindingsExtensions();
private static final CelRegexExtensions REGEX_EXTENSIONS = new CelRegexExtensions();
private static final CelComprehensionsExtensions COMPREHENSIONS_EXTENSIONS =
new CelComprehensionsExtensions();
/**
* Implementation of optional values.
*
* <p>Refer to README.md for available functions.
*/
public static CelOptionalLibrary optional() {
return CelOptionalLibrary.library().latest();
}
/**
* Implementation of optional values.
*
* <p>Refer to README.md for available functions for each supported version.
*/
public static CelOptionalLibrary optional(int version) {
return CelOptionalLibrary.library().version(version);
}
/**
* Extended functions for string manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include all functions denoted in {@link CelStringExtensions.Function}, including
* any future additions. To expose only a subset of functions, use {@link
* #strings(CelStringExtensions.Function...)} instead.
*/
public static CelStringExtensions strings() {
return STRING_EXTENSIONS_ALL;
}
/**
* Extended functions for string manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include only the specific functions denoted by {@link
* CelStringExtensions.Function}.
*/
public static CelStringExtensions strings(CelStringExtensions.Function... functions) {
return strings(ImmutableSet.copyOf(functions));
}
/**
* Extended functions for string manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include only the specific functions denoted by {@link
* CelStringExtensions.Function}.
*/
public static CelStringExtensions strings(Set<CelStringExtensions.Function> functions) {
return new CelStringExtensions(functions);
}
/**
* Extended macros for proto manipulation.
*
* <p>This adds {@code proto.getExt} and {@code proto.hasExt}. See README.md for their
* documentation.
*
* <p>Note, these functions use the 'proto' namespace; however, at the time of macro expansion the
* namespace looks just like any other identifier. If you are currently using a variable named
* 'proto', the macro will likely work just as intended; however, there is some chance for
* collision.
*/
public static CelProtoExtensions protos() {
return PROTO_EXTENSIONS;
}
/**
* Extended math helper macros and functions.
*
* <p>This adds {@code math.greatest} and {@code math.least}. See README.md for their
* documentation.
*
* <p>Note, all macros use the 'math' namespace; however, at the time of macro expansion the
* namespace looks just like any other identifier. If you are currently using a variable named
* 'math', the macro will likely work just as intended; however, there is some chance for
* collision.
*
* <p>This will include all functions denoted in {@link CelMathExtensions.Function}, including any
* future additions. To expose only a subset of these, use {@link #math(CelOptions,
* CelMathExtensions.Function...)} or {@link #math(CelOptions,int)} instead.
*/
public static CelMathExtensions math() {
return CelMathExtensions.library().latest();
}
/**
* Returns the specified version of the 'math' extension.
*
* <p>Refer to README.md for functions available in each version.
*/
public static CelMathExtensions math(int version) {
return CelMathExtensions.library().version(version);
}
/**
* Extended math helper macros and functions.
*
* <p>This adds {@code math.greatest} and {@code math.least}. See README.md for their
* documentation.
*
* <p>Note, all macros use the 'math' namespace; however, at the time of macro * expansion the
* namespace looks just like any other identifier. If you are currently using a variable named
* 'math', the macro will likely work just as intended; however, there is some chance for
* collision.
*
* <p>This will include only the specific functions denoted by {@link CelMathExtensions.Function}.
*/
public static CelMathExtensions math(CelMathExtensions.Function... functions) {
return math(ImmutableSet.copyOf(functions));
}
/**
* Extended math helper macros and functions.
*
* <p>This adds {@code math.greatest} and {@code math.least}. See README.md for their
* documentation.
*
* <p>Note, all macros use the 'math' namespace; however, at the time of macro * expansion the
* namespace looks just like any other identifier. If you are currently using a variable named
* 'math', the macro will likely work just as intended; however, there is some chance for
* collision.
*
* <p>This will include only the specific functions denoted by {@link CelMathExtensions.Function}.
*/
public static CelMathExtensions math(Set<CelMathExtensions.Function> functions) {
return new CelMathExtensions(functions);
}
/**
* @deprecated Use {@link #math()} instead.
*/
@Deprecated
@InlineMe(replacement = "CelExtensions.math()", imports = "dev.cel.extensions.CelExtensions")
public static CelMathExtensions math(CelOptions unused) {
return math();
}
/**
* @deprecated Use {@link #math(int)} instead.
*/
@Deprecated
@InlineMe(
replacement = "CelExtensions.math(version)",
imports = "dev.cel.extensions.CelExtensions")
public static CelMathExtensions math(CelOptions unused, int version) {
return math(version);
}
/**
* @deprecated Use {@link #math(Function...)} instead.
*/
@Deprecated
public static CelMathExtensions math(CelOptions unused, CelMathExtensions.Function... functions) {
return math(ImmutableSet.copyOf(functions));
}
/**
* @deprecated Use {@link #math(Set)} instead.
*/
@Deprecated
@InlineMe(
replacement = "CelExtensions.math(functions)",
imports = "dev.cel.extensions.CelExtensions")
public static CelMathExtensions math(
CelOptions unused, Set<CelMathExtensions.Function> functions) {
return math(functions);
}
/**
* Local variable binding macro(s).
*
* <p>This exposes {@code cel.bind} as a macro for use within expressions. See README.md for its
* documentation.
*
* <p>This change does not introduce any new functions, but rather leverages the comprehension AST
* to produce an expression which will introduce a new variable into the expression.
*/
public static CelBindingsExtensions bindings() {
return BINDINGS_EXTENSIONS;
}
/**
* @deprecated Use {@link #encoders(CelOptions) instead.}
*/
@Deprecated
public static CelEncoderExtensions encoders() {
return new CelEncoderExtensions(CelOptions.DEFAULT);
}
/**
* Extended functions for string, byte and object encodings.
*
* <p>This adds {@code base64.encode} and {@code base64.decode} functions. See README.md for their
* documentation.
*
* @param celOptions This should be the same {@link CelOptions} object used to configure
* compilation/runtime environments.
*/
public static CelEncoderExtensions encoders(CelOptions celOptions) {
return new CelEncoderExtensions(celOptions);
}
/**
* Extended functions for Set manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include all functions denoted in {@link SetsFunction}, including any future
* additions. To expose only a subset of functions, use {@link #sets(CelOptions, SetsFunction...)}
* instead.
*/
public static CelSetsExtensions sets(CelOptions celOptions) {
return new CelSetsExtensions(celOptions);
}
/**
* Extended functions for Set manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include only the specific functions denoted by {@link SetsFunction}.
*/
public static CelSetsExtensions sets(CelOptions celOptions, SetsFunction... functions) {
return sets(celOptions, ImmutableSet.copyOf(functions));
}
/**
* Extended functions for Set manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include only the specific functions denoted by {@link SetsFunction}.
*/
public static CelSetsExtensions sets(CelOptions celOptions, Set<SetsFunction> functions) {
return new CelSetsExtensions(celOptions, functions);
}
/**
* Extended functions for List manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include only the specific functions denoted by {@link
* CelListsExtensions.Function}.
*/
public static CelListsExtensions lists() {
return CelListsExtensions.library().latest();
}
/**
* Extended functions for List manipulation.
*
* <p>Refer to README.md for functions available in each version.
*/
public static CelListsExtensions lists(int version) {
return CelListsExtensions.library().version(version);
}
/**
* Extended functions for List manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include only the specific functions denoted by {@link
* CelListsExtensions.Function}.
*/
public static CelListsExtensions lists(CelListsExtensions.Function... functions) {
return lists(ImmutableSet.copyOf(functions));
}
/**
* Extended functions for List manipulation.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include all functions denoted in {@link CelListsExtensions.Function}, including
* any future additions. To expose only a subset of functions, use {@link
* #lists(CelListsExtensions.Function...)} instead.
*/
public static CelListsExtensions lists(Set<CelListsExtensions.Function> functions) {
return new CelListsExtensions(functions);
}
/**
* Extended functions for Regular Expressions.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include all functions denoted in {@link CelRegexExtensions.Function}, including
* any future additions.
*/
public static CelRegexExtensions regex() {
return REGEX_EXTENSIONS;
}
/**
* Extended functions for Two Variable Comprehensions Expressions.
*
* <p>Refer to README.md for available functions.
*
* <p>This will include all functions denoted in {@link CelComprehensionsExtensions.Function},
* including any future additions.
*/
public static CelComprehensionsExtensions comprehensions() {
return COMPREHENSIONS_EXTENSIONS;
}
/**
* Retrieves all function names used by every extension libraries.
*
* <p>Note: Certain extensions such as {@link CelProtoExtensions} and {@link
* CelBindingsExtensions} are implemented via macros, not functions, and those are not included
* here.
*/
public static ImmutableSet<String> getAllFunctionNames() {
return Streams.concat(
stream(CelMathExtensions.Function.values())
.map(CelMathExtensions.Function::getFunction),
stream(CelStringExtensions.Function.values())
.map(CelStringExtensions.Function::getFunction),
stream(SetsFunction.values()).map(SetsFunction::getFunction),
stream(CelEncoderExtensions.Function.values())
.map(CelEncoderExtensions.Function::getFunction),
stream(CelListsExtensions.Function.values())
.map(CelListsExtensions.Function::getFunction),
stream(CelRegexExtensions.Function.values())
.map(CelRegexExtensions.Function::getFunction),
stream(CelComprehensionsExtensions.Function.values())
.map(CelComprehensionsExtensions.Function::getFunction))
.collect(toImmutableSet());
}
public static CelExtensionLibrary<? extends CelExtensionLibrary.FeatureSet> getExtensionLibrary(
String name, CelOptions options) {
switch (name) {
case "bindings":
return CelBindingsExtensions.library();
case "encoders":
return CelEncoderExtensions.library(options);
case "lists":
return CelListsExtensions.library();
case "math":
return CelMathExtensions.library();
case "optional":
return CelOptionalLibrary.library();
case "protos":
return CelProtoExtensions.library();
case "regex":
return CelRegexExtensions.library();
case "sets":
return CelSetsExtensions.library(options);
case "strings":
return CelStringExtensions.library();
case "comprehensions":
return CelComprehensionsExtensions.library();
// TODO: add support for remaining standard extensions
default:
throw new IllegalArgumentException("Unknown standard extension '" + name + "'");
}
}
private CelExtensions() {}
}