Skip to content

Commit 414f730

Browse files
TristonianJoneskyessenov
authored andcommitted
Implement bytes() conversion function
PiperOrigin-RevId: 346894277
1 parent 53e58a7 commit 414f730

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

conformance/BUILD

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ cc_binary(
8787
# TODO(issues/94): Missing timestamp conversion functions (type / string)
8888
"--skip_test=timestamps/timestamp_conversions/toType_timestamp,toString_timestamp",
8989
"--skip_test=timestamps/duration_conversions/toType_duration,toString_duration",
90-
# TODO(issues/78): Missing bytes() conversion functions
91-
"--skip_test=conversions/bytes",
9290
# TODO(issues/81): Conversion functions for int(), uint() which can be
9391
# uncommented when the spec changes to truncation rather than rounding.
9492
"--skip_test=conversions/int/double_nearest,double_nearest_neg,double_half_away_neg,double_half_away_pos",

eval/public/builtin_func_registrar.cc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,30 @@ absl::Status RegisterTimestampFunctions(CelFunctionRegistry* registry,
10131013
return absl::OkStatus();
10141014
}
10151015

1016+
absl::Status RegisterBytesConversionFunctions(CelFunctionRegistry* registry,
1017+
const InterpreterOptions&) {
1018+
// bytes -> bytes
1019+
auto status = FunctionAdapter<CelValue::BytesHolder, CelValue::BytesHolder>::
1020+
CreateAndRegister(
1021+
builtin::kBytes, false,
1022+
[](Arena*, CelValue::BytesHolder value) -> CelValue::BytesHolder {
1023+
return value;
1024+
},
1025+
registry);
1026+
if (!status.ok()) return status;
1027+
1028+
// string -> bytes
1029+
status = FunctionAdapter<CelValue, CelValue::StringHolder>::CreateAndRegister(
1030+
builtin::kBytes, false,
1031+
[](Arena* arena, CelValue::StringHolder value) -> CelValue {
1032+
return CelValue::CreateBytesView(value.value());
1033+
},
1034+
registry);
1035+
if (!status.ok()) return status;
1036+
1037+
return absl::OkStatus();
1038+
}
1039+
10161040
absl::Status RegisterDoubleConversionFunctions(CelFunctionRegistry* registry,
10171041
const InterpreterOptions&) {
10181042
// double -> double
@@ -1121,6 +1145,8 @@ absl::Status RegisterStringConversionFunctions(
11211145
return absl::OkStatus();
11221146
}
11231147

1148+
// TODO(issues/82): ensure the bytes conversion to string handles UTF-8
1149+
// properly, and avoids unncessary allocations.
11241150
// bytes -> string
11251151
auto status = FunctionAdapter<CelValue::StringHolder, CelValue::BytesHolder>::
11261152
CreateAndRegister(
@@ -1229,7 +1255,10 @@ absl::Status RegisterUintConversionFunctions(CelFunctionRegistry* registry,
12291255

12301256
absl::Status RegisterConversionFunctions(CelFunctionRegistry* registry,
12311257
const InterpreterOptions& options) {
1232-
auto status = RegisterDoubleConversionFunctions(registry, options);
1258+
auto status = RegisterBytesConversionFunctions(registry, options);
1259+
if (!status.ok()) return status;
1260+
1261+
status = RegisterDoubleConversionFunctions(registry, options);
12331262
if (!status.ok()) return status;
12341263

12351264
// duration() conversion from string.

eval/public/builtin_func_test.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ class BuiltinsTest : public ::testing::Test {
121121
}
122122

123123
// Helper method. Looks up in registry and tests Type conversions.
124+
void TestTypeConverts(absl::string_view operation, const CelValue& ref,
125+
CelValue::BytesHolder result) {
126+
CelValue result_value;
127+
128+
ASSERT_NO_FATAL_FAILURE(PerformRun(operation, {}, {ref}, &result_value));
129+
130+
ASSERT_EQ(result_value.IsBytes(), true);
131+
ASSERT_EQ(result_value.BytesOrDie(), result)
132+
<< operation << " for " << CelValue::TypeName(ref.type());
133+
}
134+
124135
void TestTypeConverts(absl::string_view operation, const CelValue& ref,
125136
double result) {
126137
CelValue result_value;
@@ -693,6 +704,18 @@ TEST_F(BuiltinsTest, TestTimestampFunctions) {
693704
3L);
694705
}
695706

707+
TEST_F(BuiltinsTest, TestBytesConversions_bytes) {
708+
std::string txt = "hello";
709+
CelValue::BytesHolder result = CelValue::BytesHolder(&txt);
710+
TestTypeConverts(builtin::kBytes, CelValue::CreateBytes(&txt), result);
711+
}
712+
713+
TEST_F(BuiltinsTest, TestBytesConversions_string) {
714+
std::string txt = "hello";
715+
CelValue::BytesHolder result = CelValue::BytesHolder(&txt);
716+
TestTypeConverts(builtin::kBytes, CelValue::CreateString(&txt), result);
717+
}
718+
696719
TEST_F(BuiltinsTest, TestDoubleConversions_double) {
697720
double ref = 100.1;
698721
TestTypeConverts(builtin::kDouble, CelValue::CreateDouble(ref), 100.1);

eval/public/cel_builtins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ constexpr char kMilliseconds[] = "getMilliseconds";
7171

7272
// Type conversions
7373
// TODO(issues/23): Add other type conversion methods.
74+
constexpr char kBytes[] = "bytes";
7475
constexpr char kDouble[] = "double";
7576
constexpr char kDyn[] = "dyn";
7677
constexpr char kInt[] = "int";

0 commit comments

Comments
 (0)