Skip to content

Commit 0b6e8fe

Browse files
l46kokcopybara-github
authored andcommitted
Include identity functions in the standard definitions
PiperOrigin-RevId: 657756541
1 parent 5b85b76 commit 0b6e8fe

11 files changed

Lines changed: 147 additions & 1 deletion

File tree

checker/src/main/java/dev/cel/checker/Env.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,10 @@ boolean enableTimestampEpoch() {
540540
return celOptions.enableTimestampEpoch();
541541
}
542542

543+
boolean enableUnsignedLongs() {
544+
return celOptions.enableUnsignedLongs();
545+
}
546+
543547
/** Add an identifier {@code decl} to the environment. */
544548
@CanIgnoreReturnValue
545549
private Env addIdent(CelIdentDecl celIdentDecl) {

checker/src/main/java/dev/cel/checker/Standard.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ public static Env add(Env env) {
9999
timestampConversionDeclarations(env.enableTimestampEpoch()).forEach(env::add);
100100
numericComparisonDeclarations(env.enableHeterogeneousNumericComparisons()).forEach(env::add);
101101

102+
if (env.enableUnsignedLongs()) {
103+
env.add(
104+
CelFunctionDecl.newFunctionDeclaration(
105+
Function.INT.getFunction(),
106+
CelOverloadDecl.newGlobalOverload(
107+
"int64_to_int64", "type conversion (identity)", SimpleType.INT, SimpleType.INT)));
108+
}
109+
102110
return env;
103111
}
104112

@@ -384,6 +392,8 @@ private static ImmutableList<CelFunctionDecl> coreFunctionDeclarations() {
384392
celFunctionDeclBuilder.add(
385393
CelFunctionDecl.newFunctionDeclaration(
386394
Function.UINT.getFunction(),
395+
CelOverloadDecl.newGlobalOverload(
396+
"uint64_to_uint64", "type conversion (identity)", SimpleType.UINT, SimpleType.UINT),
387397
CelOverloadDecl.newGlobalOverload(
388398
"int64_to_uint64", "type conversion", SimpleType.UINT, SimpleType.INT),
389399
CelOverloadDecl.newGlobalOverload(
@@ -395,6 +405,11 @@ private static ImmutableList<CelFunctionDecl> coreFunctionDeclarations() {
395405
celFunctionDeclBuilder.add(
396406
CelFunctionDecl.newFunctionDeclaration(
397407
Function.DOUBLE.getFunction(),
408+
CelOverloadDecl.newGlobalOverload(
409+
"double_to_double",
410+
"type conversion (identity)",
411+
SimpleType.DOUBLE,
412+
SimpleType.DOUBLE),
398413
CelOverloadDecl.newGlobalOverload(
399414
"int64_to_double", "type conversion", SimpleType.DOUBLE, SimpleType.INT),
400415
CelOverloadDecl.newGlobalOverload(
@@ -406,6 +421,11 @@ private static ImmutableList<CelFunctionDecl> coreFunctionDeclarations() {
406421
celFunctionDeclBuilder.add(
407422
CelFunctionDecl.newFunctionDeclaration(
408423
Function.STRING.getFunction(),
424+
CelOverloadDecl.newGlobalOverload(
425+
"string_to_string",
426+
"type conversion (identity)",
427+
SimpleType.STRING,
428+
SimpleType.STRING),
409429
CelOverloadDecl.newGlobalOverload(
410430
"int64_to_string", "type conversion", SimpleType.STRING, SimpleType.INT),
411431
CelOverloadDecl.newGlobalOverload(
@@ -423,6 +443,8 @@ private static ImmutableList<CelFunctionDecl> coreFunctionDeclarations() {
423443
celFunctionDeclBuilder.add(
424444
CelFunctionDecl.newFunctionDeclaration(
425445
Function.BYTES.getFunction(),
446+
CelOverloadDecl.newGlobalOverload(
447+
"bytes_to_bytes", "type conversion (identity)", SimpleType.BYTES, SimpleType.BYTES),
426448
CelOverloadDecl.newGlobalOverload(
427449
"string_to_bytes", "type conversion", SimpleType.BYTES, SimpleType.STRING)));
428450

@@ -437,12 +459,24 @@ private static ImmutableList<CelFunctionDecl> coreFunctionDeclarations() {
437459
celFunctionDeclBuilder.add(
438460
CelFunctionDecl.newFunctionDeclaration(
439461
Function.DURATION.getFunction(),
462+
CelOverloadDecl.newGlobalOverload(
463+
"duration_to_duration",
464+
"type conversion (identity)",
465+
SimpleType.DURATION,
466+
SimpleType.DURATION),
440467
CelOverloadDecl.newGlobalOverload(
441468
"string_to_duration",
442469
"type conversion, duration should be end with \"s\", which stands for seconds",
443470
SimpleType.DURATION,
444471
SimpleType.STRING)));
445472

473+
// Conversions to boolean
474+
celFunctionDeclBuilder.add(
475+
CelFunctionDecl.newFunctionDeclaration(
476+
Function.BOOL.getFunction(),
477+
CelOverloadDecl.newGlobalOverload(
478+
"bool_to_bool", "type conversion (identity)", SimpleType.BOOL, SimpleType.BOOL)));
479+
446480
// String functions
447481
celFunctionDeclBuilder.add(
448482
CelFunctionDecl.newFunctionDeclaration(
@@ -674,7 +708,12 @@ private static ImmutableList<CelFunctionDecl> timestampConversionDeclarations(bo
674708
"Type conversion of strings to timestamps according to RFC3339. Example:"
675709
+ " \"1972-01-01T10:00:20.021-05:00\".",
676710
SimpleType.TIMESTAMP,
677-
SimpleType.STRING));
711+
SimpleType.STRING),
712+
CelOverloadDecl.newGlobalOverload(
713+
"timestamp_to_timestamp",
714+
"type conversion (identity)",
715+
SimpleType.TIMESTAMP,
716+
SimpleType.TIMESTAMP));
678717
if (withEpoch) {
679718
timestampBuilder.addOverloads(
680719
CelOverloadDecl.newGlobalOverload(

checker/src/test/resources/standardEnvDump.baseline

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,25 @@ declare _||_ {
143143
}
144144
declare bool {
145145
value type(bool)
146+
function bool_to_bool (bool) -> bool
146147
}
147148
declare bytes {
148149
value type(bytes)
150+
function bytes_to_bytes (bytes) -> bytes
149151
function string_to_bytes (string) -> bytes
150152
}
151153
declare contains {
152154
function contains_string string.(string) -> bool
153155
}
154156
declare double {
155157
value type(double)
158+
function double_to_double (double) -> double
156159
function int64_to_double (int) -> double
157160
function uint64_to_double (uint) -> double
158161
function string_to_double (string) -> double
159162
}
160163
declare duration {
164+
function duration_to_duration (google.protobuf.Duration) -> google.protobuf.Duration
161165
function string_to_duration (string) -> google.protobuf.Duration
162166
}
163167
declare dyn {
@@ -217,6 +221,7 @@ declare int {
217221
function double_to_int64 (double) -> int
218222
function string_to_int64 (string) -> int
219223
function timestamp_to_int64 (google.protobuf.Timestamp) -> int
224+
function int64_to_int64 (int) -> int
220225
}
221226
declare list {
222227
value type(list(dyn))
@@ -246,6 +251,7 @@ declare startsWith {
246251
}
247252
declare string {
248253
value type(string)
254+
function string_to_string (string) -> string
249255
function int64_to_string (int) -> string
250256
function uint64_to_string (uint) -> string
251257
function double_to_string (double) -> string
@@ -255,6 +261,7 @@ declare string {
255261
}
256262
declare timestamp {
257263
function string_to_timestamp (string) -> google.protobuf.Timestamp
264+
function timestamp_to_timestamp (google.protobuf.Timestamp) -> google.protobuf.Timestamp
258265
function int64_to_timestamp (int) -> google.protobuf.Timestamp
259266
}
260267
declare type {
@@ -263,6 +270,7 @@ declare type {
263270
}
264271
declare uint {
265272
value type(uint)
273+
function uint64_to_uint64 (uint) -> uint
266274
function int64_to_uint64 (int) -> uint
267275
function double_to_uint64 (double) -> uint
268276
function string_to_uint64 (string) -> uint

runtime/src/main/java/dev/cel/runtime/StandardFunctions.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public static void addNonInlined(
154154
}
155155

156156
private static void addBoolFunctions(Registrar registrar) {
157+
// Identity
158+
registrar.add("bool_to_bool", Boolean.class, (Boolean x) -> x);
157159
// The conditional, logical_or, logical_and, and not_strictly_false functions are special-cased.
158160
registrar.add("logical_not", Boolean.class, (Boolean x) -> !x);
159161

@@ -167,6 +169,8 @@ private static void addBoolFunctions(Registrar registrar) {
167169
}
168170

169171
private static void addBytesFunctions(Registrar registrar) {
172+
// Identity
173+
registrar.add("bytes_to_bytes", ByteString.class, (ByteString x) -> x);
170174
// Bytes ordering functions: <, <=, >=, >
171175
registrar.add(
172176
"less_bytes",
@@ -205,6 +209,8 @@ private static void addBytesFunctions(Registrar registrar) {
205209
}
206210

207211
private static void addDoubleFunctions(Registrar registrar, CelOptions celOptions) {
212+
// Identity
213+
registrar.add("double_to_double", Double.class, (Double x) -> x);
208214
// Double ordering functions.
209215
registrar.add("less_double", Double.class, Double.class, (Double x, Double y) -> x < y);
210216
registrar.add("less_equals_double", Double.class, Double.class, (Double x, Double y) -> x <= y);
@@ -245,6 +251,8 @@ private static void addDoubleFunctions(Registrar registrar, CelOptions celOption
245251
}
246252

247253
private static void addDurationFunctions(Registrar registrar) {
254+
// Identity
255+
registrar.add("duration_to_duration", Duration.class, (Duration x) -> x);
248256
// Duration ordering functions: <, <=, >=, >
249257
registrar.add(
250258
"less_duration",
@@ -307,6 +315,12 @@ private static void addDurationFunctions(Registrar registrar) {
307315
}
308316

309317
private static void addIntFunctions(Registrar registrar, CelOptions celOptions) {
318+
// Identity
319+
if (celOptions.enableUnsignedLongs()) {
320+
// Note that we require UnsignedLong flag here to avoid ambiguous overloads against
321+
// "uint64_to_int64", because they both use the same Java Long class.
322+
registrar.add("int64_to_int64", Long.class, (Long x) -> x);
323+
}
310324
// Comparison functions.
311325
registrar.add("less_int64", Long.class, Long.class, (Long x, Long y) -> x < y);
312326
registrar.add("less_equals_int64", Long.class, Long.class, (Long x, Long y) -> x <= y);
@@ -499,6 +513,8 @@ private static void addMapFunctions(
499513
}
500514

501515
private static void addStringFunctions(Registrar registrar, CelOptions celOptions) {
516+
// Identity
517+
registrar.add("string_to_string", String.class, (String x) -> x);
502518
// String ordering functions: <, <=, >=, >.
503519
registrar.add(
504520
"less_string", String.class, String.class, (String x, String y) -> x.compareTo(y) < 0);
@@ -547,6 +563,8 @@ private static void addStringFunctions(Registrar registrar, CelOptions celOption
547563
// timestamp_to_milliseconds overload
548564
@SuppressWarnings("JavaLocalDateTimeGetNano")
549565
private static void addTimestampFunctions(Registrar registrar) {
566+
// Identity
567+
registrar.add("timestamp_to_timestamp", Timestamp.class, (Timestamp x) -> x);
550568
// Timestamp relation operators: <, <=, >=, >
551569
registrar.add(
552570
"less_timestamp",
@@ -714,6 +732,8 @@ private static void addTimestampFunctions(Registrar registrar) {
714732
}
715733

716734
private static void addSignedUintFunctions(Registrar registrar, CelOptions celOptions) {
735+
// Identity
736+
registrar.add("uint64_to_uint64", Long.class, (Long x) -> x);
717737
// Uint relation operators: <, <=, >=, >
718738
registrar.add(
719739
"less_uint64",
@@ -833,6 +853,8 @@ private static void addSignedUintFunctions(Registrar registrar, CelOptions celOp
833853
}
834854

835855
private static void addUintFunctions(Registrar registrar, CelOptions celOptions) {
856+
// Identity
857+
registrar.add("uint64_to_uint64", UnsignedLong.class, (UnsignedLong x) -> x);
836858
registrar.add(
837859
"less_uint64",
838860
UnsignedLong.class,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Source: bool(true)
2+
=====>
3+
bindings: {}
4+
result: true

runtime/src/test/resources/bytesConversions.baseline

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ Source: bytes('abc\303')
22
=====>
33
bindings: {}
44
result: abcÃ
5+
6+
Source: bytes(bytes('abc\303'))
7+
=====>
8+
bindings: {}
9+
result: abcÃ

runtime/src/test/resources/doubleConversions.baseline

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ Source: double('bad')
1818
bindings: {}
1919
error: evaluation error: For input string: "bad"
2020
error_code: BAD_FORMAT
21+
22+
Source: double(1.5)
23+
=====>
24+
bindings: {}
25+
result: 1.5

runtime/src/test/resources/stringConversions.baseline

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ Source: string(duration('1000000s'))
3232
=====>
3333
bindings: {}
3434
result: 1000000s
35+
36+
Source: string('hello')
37+
=====>
38+
bindings: {}
39+
result: hello

runtime/src/test/resources/timeConversions.baseline

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ declare t1 {
5454
bindings: {}
5555
error: evaluation error: invalid duration format
5656
error_code: BAD_FORMAT
57+
58+
Source: duration(duration('15.0s'))
59+
declare t1 {
60+
value google.protobuf.Timestamp
61+
}
62+
=====>
63+
bindings: {}
64+
result: seconds: 15
65+
66+
67+
Source: timestamp(timestamp(123))
68+
declare t1 {
69+
value google.protobuf.Timestamp
70+
}
71+
=====>
72+
bindings: {}
73+
result: seconds: 123

runtime/src/test/resources/uint64Conversions.baseline

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,13 @@ Source: uint('f1')
3535
bindings: {}
3636
error: evaluation error: f1
3737
error_code: BAD_FORMAT
38+
39+
Source: uint(1u)
40+
=====>
41+
bindings: {}
42+
result: 1
43+
44+
Source: uint(dyn(1u))
45+
=====>
46+
bindings: {}
47+
result: 1

0 commit comments

Comments
 (0)