Skip to content

Commit 1f22743

Browse files
Merge pull request #728 from maorwayn:string_bool_fix
PiperOrigin-RevId: 775826374
2 parents aa5cecf + a941e94 commit 1f22743

File tree

6 files changed

+18
-0
lines changed

6 files changed

+18
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ enum StandardFunction {
159159
Overload.Conversions.INT64_TO_STRING,
160160
Overload.Conversions.UINT64_TO_STRING,
161161
Overload.Conversions.DOUBLE_TO_STRING,
162+
Overload.Conversions.BOOL_TO_STRING,
162163
Overload.Conversions.BYTES_TO_STRING,
163164
Overload.Conversions.TIMESTAMP_TO_STRING,
164165
Overload.Conversions.DURATION_TO_STRING),
@@ -679,6 +680,9 @@ public enum Conversions implements StandardOverload {
679680
DOUBLE_TO_STRING(
680681
CelOverloadDecl.newGlobalOverload(
681682
"double_to_string", "type conversion", SimpleType.STRING, SimpleType.DOUBLE)),
683+
BOOL_TO_STRING(
684+
CelOverloadDecl.newGlobalOverload(
685+
"bool_to_string", "type conversion", SimpleType.STRING, SimpleType.BOOL)),
682686
BYTES_TO_STRING(
683687
CelOverloadDecl.newGlobalOverload(
684688
"bytes_to_string", "type conversion", SimpleType.STRING, SimpleType.BYTES)),

checker/src/test/resources/standardEnvDump.baseline

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ declare string {
256256
function int64_to_string (int) -> string
257257
function uint64_to_string (uint) -> string
258258
function double_to_string (double) -> string
259+
function bool_to_string (bool) -> string
259260
function bytes_to_string (bytes) -> string
260261
function timestamp_to_string (google.protobuf.Timestamp) -> string
261262
function duration_to_string (google.protobuf.Duration) -> string

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public enum StandardFunction {
223223
Conversions.STRING_TO_STRING,
224224
Conversions.INT64_TO_STRING,
225225
Conversions.DOUBLE_TO_STRING,
226+
Conversions.BOOL_TO_STRING,
226227
Conversions.BYTES_TO_STRING,
227228
Conversions.TIMESTAMP_TO_STRING,
228229
Conversions.DURATION_TO_STRING,
@@ -478,6 +479,7 @@ public enum Conversions implements StandardOverload {
478479
STRING_TO_STRING(StringOverload.STRING_TO_STRING::newFunctionBinding),
479480
INT64_TO_STRING(StringOverload.INT64_TO_STRING::newFunctionBinding),
480481
DOUBLE_TO_STRING(StringOverload.DOUBLE_TO_STRING::newFunctionBinding),
482+
BOOL_TO_STRING(StringOverload.BOOL_TO_STRING::newFunctionBinding),
481483
BYTES_TO_STRING(StringOverload.BYTES_TO_STRING::newFunctionBinding),
482484
TIMESTAMP_TO_STRING(StringOverload.TIMESTAMP_TO_STRING::newFunctionBinding),
483485
DURATION_TO_STRING(StringOverload.DURATION_TO_STRING::newFunctionBinding),

runtime/src/main/java/dev/cel/runtime/standard/StringFunction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public enum StringOverload implements CelStandardOverload {
5555
DOUBLE_TO_STRING(
5656
(celOptions, runtimeEquality) ->
5757
CelFunctionBinding.from("double_to_string", Double.class, Object::toString)),
58+
BOOL_TO_STRING(
59+
(celOptions, runtimeEquality) ->
60+
CelFunctionBinding.from("bool_to_string", Boolean.class, Object::toString)),
5861
BYTES_TO_STRING(
5962
(celOptions, runtimeEquality) ->
6063
CelFunctionBinding.from(

runtime/src/test/resources/stringConversions.baseline

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ Source: string(-1)
1313
bindings: {}
1414
result: -1
1515

16+
Source: string(true)
17+
=====>
18+
bindings: {}
19+
result: true
20+
1621
Source: string(b'abc\303\203')
1722
=====>
1823
bindings: {}

testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,9 @@ public void stringConversions() {
16571657
source = "string(-1)"; // int converts to '-1'
16581658
runTest();
16591659

1660+
source = "string(true)"; // bool converts to 'true'
1661+
runTest();
1662+
16601663
// Byte literals in Google SQL only take the leading byte of an escape character.
16611664
// This means that to translate a byte literal to a UTF-8 encoded string, all bytes must be
16621665
// encoded in the literal as they would be laid out in memory for UTF-8, hence the extra octal

0 commit comments

Comments
 (0)