Skip to content

Commit 2b0ed60

Browse files
TristonianJonescopybara-github
authored andcommitted
Introduce string(bool) support at runtime
PiperOrigin-RevId: 817382186
1 parent 033986e commit 2b0ed60

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

eval/public/builtin_func_registrar_test.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ INSTANTIATE_TEST_SUITE_P(
264264
{},
265265
CelValue::CreateBool(true),
266266
OverflowChecksEnabled()},
267+
{"StringToBool",
268+
"string(true) + string(false)",
269+
{},
270+
CelValue::CreateStringView("truefalse"),
271+
OverflowChecksEnabled()},
267272
}),
268273
[](const testing::TestParamInfo<BuiltinFuncParamsTest::ParamType>& info) {
269274
return info.param.test_name;

runtime/standard/type_conversion_functions.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ absl::Status RegisterStringConversionFunctions(FunctionRegistry& registry,
152152
registry);
153153
CEL_RETURN_IF_ERROR(status);
154154

155+
// bool -> string
156+
status = UnaryFunctionAdapter<StringValue, bool>::RegisterGlobalOverload(
157+
cel::builtin::kString,
158+
[](bool value) -> StringValue {
159+
return StringValue(value ? "true" : "false");
160+
},
161+
registry);
162+
CEL_RETURN_IF_ERROR(status);
163+
155164
// double -> string
156165
status = UnaryFunctionAdapter<StringValue, double>::RegisterGlobalOverload(
157166
cel::builtin::kString,

runtime/standard/type_conversion_functions_test.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ TEST(RegisterTypeConversionFunctions, RegisterStringConversionFunctions) {
104104
EXPECT_THAT(
105105
registry.FindStaticOverloads(builtin::kString, false, {Kind::kAny}),
106106
UnorderedElementsAre(
107+
MatchesUnaryDescriptor(builtin::kString, false, Kind::kBool),
107108
MatchesUnaryDescriptor(builtin::kString, false, Kind::kInt),
108109
MatchesUnaryDescriptor(builtin::kString, false, Kind::kDouble),
109110
MatchesUnaryDescriptor(builtin::kString, false, Kind::kUint),

0 commit comments

Comments
 (0)