-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathCelOptions.java
More file actions
523 lines (439 loc) · 20.4 KB
/
CelOptions.java
File metadata and controls
523 lines (439 loc) · 20.4 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// Copyright 2022 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.common;
import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.Immutable;
/**
* Options to configure how the CEL parser, type-checker, and evaluator behave.
*
* <p>Users are strongly encouraged to use {@link #current} to ensure that the overall CEL stack
* behaves in the manner most consistent with the CEL specification.
*/
@CheckReturnValue
@AutoValue
@Immutable
public abstract class CelOptions {
/**
* ProtoUnsetFieldOptions describes how to handle Activation.fromProto() calls where proto message
* fields may be unset and should either be handled perhaps as absent or as the default proto
* value.
*/
public enum ProtoUnsetFieldOptions {
// Do not bind a field if it is unset. Repeated fields are bound as empty list.
SKIP,
// Bind the (proto api) default value for a field.
BIND_DEFAULT
}
public static final CelOptions DEFAULT = current().build();
public static final CelOptions LEGACY = newBuilder().disableCelStandardEquality(true).build();
// Package-private constructor to prevent extension.
CelOptions() {}
// Parser related options
public abstract boolean enableReservedIds();
public abstract boolean enableOptionalSyntax();
public abstract int maxExpressionCodePointSize();
public abstract int maxParseErrorRecoveryLimit();
public abstract int maxParseRecursionDepth();
public abstract boolean populateMacroCalls();
public abstract boolean retainRepeatedUnaryOperators();
public abstract boolean retainUnbalancedLogicalExpressions();
public abstract boolean enableHiddenAccumulatorVar();
public abstract boolean enableQuotedIdentifierSyntax();
// Type-Checker related options
public abstract boolean enableCompileTimeOverloadResolution();
public abstract boolean enableHomogeneousLiterals();
public abstract boolean enableTimestampEpoch();
public abstract boolean enableHeterogeneousNumericComparisons();
public abstract boolean enableNamespacedDeclarations();
public abstract boolean enableJsonFieldNames();
// Evaluation related options
public abstract boolean disableCelStandardEquality();
public abstract boolean enableShortCircuiting();
public abstract boolean enableRegexPartialMatch();
public abstract boolean enableUnsignedComparisonAndArithmeticIsUnsigned();
public abstract boolean enableUnsignedLongs();
public abstract boolean enableProtoDifferencerEquality();
public abstract boolean errorOnDuplicateMapKeys();
public abstract boolean errorOnIntWrap();
public abstract boolean resolveTypeDependencies();
public abstract boolean enableUnknownTracking();
public abstract boolean enableCelValue();
public abstract int comprehensionMaxIterations();
public abstract boolean evaluateCanonicalTypesToNativeValues();
public abstract boolean unwrapWellKnownTypesOnFunctionDispatch();
public abstract ProtoUnsetFieldOptions fromProtoUnsetFieldOption();
public abstract boolean enableComprehension();
public abstract int maxRegexProgramSize();
public abstract Builder toBuilder();
/**
* Return an unconfigured {@code Builder}. This is equivalent to preserving all legacy behaviors,
* both good and bad, of the original CEL implementation.
*/
public static Builder newBuilder() {
return new AutoValue_CelOptions.Builder()
// Parser options
.enableReservedIds(false)
.enableOptionalSyntax(false)
.maxExpressionCodePointSize(100_000)
.maxParseErrorRecoveryLimit(30)
.maxParseRecursionDepth(250)
.populateMacroCalls(false)
.retainRepeatedUnaryOperators(false)
.retainUnbalancedLogicalExpressions(false)
.enableHiddenAccumulatorVar(true)
.enableQuotedIdentifierSyntax(true)
// Type-Checker options
.enableCompileTimeOverloadResolution(false)
.enableHomogeneousLiterals(false)
.enableTimestampEpoch(false)
.enableHeterogeneousNumericComparisons(false)
.enableNamespacedDeclarations(true)
.enableJsonFieldNames(false)
// Evaluation options
.disableCelStandardEquality(true)
.evaluateCanonicalTypesToNativeValues(false)
.enableShortCircuiting(true)
.enableRegexPartialMatch(false)
.enableUnsignedComparisonAndArithmeticIsUnsigned(false)
.enableUnsignedLongs(false)
.enableProtoDifferencerEquality(false)
.errorOnIntWrap(false)
.errorOnDuplicateMapKeys(false)
.resolveTypeDependencies(true)
.enableUnknownTracking(false)
.enableCelValue(false)
.comprehensionMaxIterations(-1)
.unwrapWellKnownTypesOnFunctionDispatch(true)
.fromProtoUnsetFieldOption(ProtoUnsetFieldOptions.BIND_DEFAULT)
.enableComprehension(true)
.maxRegexProgramSize(-1);
}
/**
* Return a {@code Builder} configured with the most current set of {@code CelOptions}
* (recommended).
*/
public static Builder current() {
return newBuilder()
.enableReservedIds(true)
.enableUnsignedComparisonAndArithmeticIsUnsigned(true)
.enableUnsignedLongs(true)
.enableRegexPartialMatch(true)
.enableTimestampEpoch(true)
.errorOnDuplicateMapKeys(true)
.evaluateCanonicalTypesToNativeValues(true)
.errorOnIntWrap(true)
.resolveTypeDependencies(true)
.disableCelStandardEquality(false);
}
/** Builder for configuring the {@code CelOptions}. */
@AutoValue.Builder
public abstract static class Builder {
// Package-private constructor to prevent extension.
Builder() {}
// Parser related builder options.
/**
* Check for use of reserved identifiers during parsing.
*
* <p>See <a href="https://github.com/google/cel-spec/blob/master/doc/langdef.md">the language
* spec</a> for a list of reserved identifiers.
*/
public abstract Builder enableReservedIds(boolean value);
/**
* EnableOptionalSyntax enables syntax for optional field and index selection (e.g: msg.?field).
*
* <p>Note: This option is automatically enabled for the parser by adding {@code
* CelOptionalLibrary} to the environment.
*/
public abstract Builder enableOptionalSyntax(boolean value);
/**
* Set a limit on the size of the expression string which may be parsed in terms of the number
* of code points contained within the string.
*/
public abstract Builder maxExpressionCodePointSize(int value);
/**
* Limit the number of error recovery attempts which may be made by the parser for a given
* syntax error before terminating the parse completely.
*/
public abstract Builder maxParseErrorRecoveryLimit(int value);
/** Limit the amount of recursion within parse expressions. */
public abstract Builder maxParseRecursionDepth(int value);
/** Populate macro_calls map in source_info with macro calls parsed from the expression. */
public abstract Builder populateMacroCalls(boolean value);
/**
* Retain all invocations of unary '-' and '!' that occur in source in the abstract syntax.
*
* <p>By default the parser collapses towers of repeated unary '-' and '!' into zero or one
* instance by assuming these operators to be inverses of themselves. This behavior may not
* always be desirable.
*/
public abstract Builder retainRepeatedUnaryOperators(boolean value);
/**
* Retain the original grouping of logical connectives '&&' and '||' without attempting to
* rebalance them in the abstract syntax.
*
* <p>The default rebalancing can reduce the overall nesting depth of the generated protos
* representing abstract syntax, but it relies on associativity of the operations themselves.
* This behavior may not always be desirable.
*/
public abstract Builder retainUnbalancedLogicalExpressions(boolean value);
/**
* Enable the use of a hidden accumulator variable name.
*
* <p>This is a temporary option to transition to using an internal identifier for the
* accumulator variable used by builtin comprehension macros. When enabled, parses result in a
* semantically equivalent AST, but with a different accumulator variable that can't be directly
* referenced in the source expression.
*/
public abstract Builder enableHiddenAccumulatorVar(boolean value);
/**
* Enable quoted identifier syntax.
*
* <p>This enables the use of quoted identifier syntax when parsing CEL expressions. When
* enabled, the parser will accept identifiers that are surrounded by backticks (`) and will
* treat them as a single identifier. Currently, this is only supported for field specifiers
* over a limited character set.
*/
public abstract Builder enableQuotedIdentifierSyntax(boolean value);
// Type-Checker related options
/**
* Require overloads to resolve (narrow to a single candidate) during type checking.
*
* <p>This eliminates run-time overload dispatch and avoids implicit coercions of the result
* type to type dyn. However, this configuration should only be used if your application is not
* handling any form of dynamic data such as JSON or {@code google.protobuf.Any}.
*/
public abstract Builder enableCompileTimeOverloadResolution(boolean value);
/**
* During type checking require list-, and map literals to be type-homogeneous in their
* element-, key-, and value types, respectively.
*
* <p>Without this flag one can use type-mismatched elements, keys, and values, and the type
* checker will implicitly coerce them to type dyn.
*
* <p>This flag is recommended for all new uses of CEL.
*
* @deprecated Use standalone {@code dev.cel.validators.validator.HomogeneousLiteralValidator}
* instead.
*/
@Deprecated
public abstract Builder enableHomogeneousLiterals(boolean value);
/**
* Enable the {@code int64_to_timestamp} overload which creates a timestamp from Unix epoch
* seconds.
*
* <p>Historically used to opt-in to this feature, this option is now enabled by default across
* all runtimes.
*
* <p>TODO: Remove this feature once it has been auto-enabled.
*
* @deprecated This option is now enabled by default. If you are passing {@code true}, simply
* remove this method call. If you are passing {@code false} to disable this feature, subset
* the environment instead using {@code dev.cel.checker.CelStandardDeclarations} and {@code
* dev.cel.runtime.CelStandardFunctions}.
*/
@Deprecated
public abstract Builder enableTimestampEpoch(boolean value);
/**
* Enable the {@code less_equals_double_int64} and other cross-type numeric comparisons for
* {@code double}, {@code int64}, and {@code uint64} values.
*
* <p>This feature adds the declarations for these overloads to the CEL standard environment,
* but the runtime functions are enabled by default.
*
* <p>TODO: Remove this feature once it has been auto-enabled.
*/
public abstract Builder enableHeterogeneousNumericComparisons(boolean value);
/**
* Enables the usage of namespaced functions and identifiers. This causes the type-checker to
* rewrite the AST to support namespacing (e.g: a field selector of form `namespace.msg.field`
* gets rewritten as a fully qualified identifier name of `namespace.msg.field`).
*
* <p>TODO: Remove this feature once it has been auto-enabled.
*
* @deprecated This will be removed in the future. Please update your codebase to not rely on
* existence of certain expression nodes that would be collapsed/removed with this feature
* enabled.
*/
@Deprecated
public abstract Builder enableNamespacedDeclarations(boolean value);
// Evaluation related options
/**
* Disable standard CEL equality in favor of the legacy Java equality calls for (in)equality
* tests.
*
* <p>This feature is how the legacy CEL-Java APIs were originally configured, and in most cases
* will yield identical results to CEL equality, with the exception that equality between
* well-known protobuf types (wrapper types, {@code protobuf.Value}, {@code protobuf.Any}) may
* not compare correctly to simple and aggregate CEL types.
*
* <p>Additionally, Java equality across numeric types such as {@code double} and {@code long},
* will be trivially false, whereas CEL equality will compare the values as though they exist on
* a continuous number line.
*/
public abstract Builder disableCelStandardEquality(boolean value);
/**
* Enable short-circuiting of the logical operator evaluation. If enabled, AND, OR, and TERNARY
* do not evaluate the entire expression once the resulting value is known from the left-hand
* side.
*
* <p>This option is enabled by default. In most cases, this should not be disabled except for
* debugging purposes or collecting results for all evaluated branches through {@link
* dev.cel.runtime.CelEvaluationListener}.
*/
public abstract Builder enableShortCircuiting(boolean value);
/**
* Treat regex {@code matches} calls as substring (unanchored) match patterns.
*
* <p>The default treatment for pattern matching within RE2 is full match within Java; however,
* the CEL standard specifies that the matches() function is a substring match.
*/
public abstract Builder enableRegexPartialMatch(boolean value);
/**
* Treat unsigned integers as unsigned when doing arithmetic and comparisons.
*
* <p>Prior to turning on this feature, attempts to perform arithmetic or comparisons on
* unsigned integers larger than 2^63-1 may result in a runtime exception in the form of an
* {@link java.lang.IllegalArgumentException}.
*/
public abstract Builder enableUnsignedComparisonAndArithmeticIsUnsigned(boolean value);
/**
* Use {@code UnsignedLong} values to represent unsigned integers within CEL instead of the
* nearest Java equivalent of {@code Long}.
*
* @deprecated Do not use. This option is enabled by default in the currently supported feature
* set {@link CelOptions#DEFAULT}. This flag will be removed in the future.
*/
@Deprecated
public abstract Builder enableUnsignedLongs(boolean value);
/**
* Perform equality using {@code ProtoEquality} proto equality.
*
* <p>CEL's alternative implementation to the {@code Message#equals} comes with a few key
* differences:
*
* <ul>
* <li/>NaN is not equal to itself.
* <li/>Any values are unpacked before comparison.
* <li/>If two Any values cannot be unpacked, they are compared by bytes.
* </ul>
*/
public abstract Builder enableProtoDifferencerEquality(boolean value);
/** Throw errors on duplicate keys in map literals. */
public abstract Builder errorOnDuplicateMapKeys(boolean value);
/**
* Throw errors when ints or uints wrap.
*
* <p>Prior to this feature, int and uint arithmetic wrapped, i.e. was coerced into range via
* mod 2^64. The spec settled on throwing an error instead. Note that this makes arithmetic non-
* associative.
*/
public abstract Builder errorOnIntWrap(boolean value);
/**
* Enable or disable the resolution of {@code Descriptor} type dependencies as part of the CEL
* environment setup. Defaults to disabled.
*
* <p>Disabling this feature should only be done when you know that only the types provided will
* be referenced within the CEL expression. This means that either the type set provided was
* complete, or that the type set is only what is referenced within expressions.
*/
public abstract Builder resolveTypeDependencies(boolean value);
/**
* Enable tracking unknown attributes and function invocations encountered during evaluation.
*
* <p>When enabled, the evaluator will track unknown attributes (leaf values in the activations)
* and function results (a particular invocation that was identified as unknown).
*/
public abstract Builder enableUnknownTracking(boolean value);
/**
* @deprecated Do not use, this flag will be removed in the future. Use the planner based
* runtime instead, which supports CelValue by default.
*/
@Deprecated
public abstract Builder enableCelValue(boolean value);
/**
* Limit the total number of iterations permitted within comprehension loops.
*
* <p>If the limit is reached, then an evaluation exception will be thrown. This limit also
* affects nested comprehension interactions as well.
*
* <p>A negative {@code value} will disable max iteration checks.
*
* <p>Note: comprehension limits are not supported within the async CEL interpreter.
*/
public abstract Builder comprehensionMaxIterations(int value);
/**
* If set, canonical CEL types such as bytes and CEL null will return their native value
* equivalents instead of protobuf based values. Specifically:
*
* <ul>
* <li>Bytes: {@code dev.cel.common.values.CelByteString} instead of {@code
* com.google.protobuf.ByteString}.
* <li>CEL null: {@code dev.cel.common.values.NullValue} instead of {@code
* com.google.protobuf.NullValue}.
* <li>Timestamp: {@code java.time.Instant} instead of {@code com.google.protobuf.Timestamp}.
* <li>Duration: {@code java.time.Duration} instead of {@code com.google.protobuf.Duration}.
* </ul>
*
* @deprecated Do not use. This flag is enabled by default and will be removed in the future.
*/
@Deprecated
public abstract Builder evaluateCanonicalTypesToNativeValues(boolean value);
/**
* If disabled, CEL runtime will no longer adapt the function dispatch results for protobuf's
* well known types to other types. This option is enabled by default.
*
* @deprecated This will be removed in the future. Please update your codebase to be conformant
* with CEL specification.
*/
@Deprecated
public abstract Builder unwrapWellKnownTypesOnFunctionDispatch(boolean value);
/**
* Configure how unset proto fields are handled when evaluating over a protobuf message where
* fields are intended to be treated as top-level variables. Defaults to binding all fields to
* their default value if unset.
*
* @see ProtoUnsetFieldOptions
*/
public abstract Builder fromProtoUnsetFieldOption(ProtoUnsetFieldOptions value);
/**
* Enables comprehension (macros) for the runtime. Setting false has the same effect with
* assigning 0 for {@link #comprehensionMaxIterations()}. This option exists to maintain parity
* with cel-cpp interpreter options.
*/
public abstract Builder enableComprehension(boolean value);
/**
* Set maximum program size for RE2J regex.
*
* <p>The program size is a very approximate measure of a regexp's "cost". Larger numbers are
* more expensive than smaller numbers.
*
* <p>A negative {@code value} will disable the check.
*
* <p>There's no guarantee that RE2 program size has the exact same value across other CEL
* implementations (C++ and Go).
*/
public abstract Builder maxRegexProgramSize(int value);
/**
* Use the `json_name` field option on a protobuf message as the name of the field.
*
* <p>If enabled, the compiler will only accept the `json_name` and no longer recognize the
* original protobuf field name. Use with caution as this may break existing expressions during
* compilation. The runtime continues to support both names for maintaining backwards
* compatibility.
*/
public abstract Builder enableJsonFieldNames(boolean value);
public abstract CelOptions build();
}
}