Skip to content

Commit

Permalink
Make C++ CEL Regex Extension return map with int index and string names
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 691555991
  • Loading branch information
CEL Dev Team authored and copybara-github committed Oct 30, 2024
1 parent 883d727 commit f989928
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
13 changes: 7 additions & 6 deletions extensions/regex_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ CelValue CaptureStringN(Arena* arena, CelValue::StringHolder target,
std::vector<std::pair<CelValue, CelValue>> cel_values;
for (int index = 1; index <= capturing_groups_count; index++) {
auto it = named_capturing_groups_map.find(index);
std::string name = it != named_capturing_groups_map.end()
? it->second
: std::to_string(index);
CelValue name =
it != named_capturing_groups_map.end()
? CelValue::CreateString(
google::protobuf::Arena::Create<std::string>(arena, it->second))
: CelValue::CreateInt64(index);
cel_values.emplace_back(
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(arena, name)),
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
arena, captured_strings[index - 1])));
name, CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
arena, captured_strings[index - 1])));
}
auto container_map = google::api::expr::runtime::CreateContainerBackedMap(
absl::MakeSpan(cel_values));
Expand Down
24 changes: 12 additions & 12 deletions extensions/regex_functions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ class RegexFunctionsTest : public ::testing::TestWithParam<TestCase> {
TEST_F(RegexFunctionsTest, CaptureStringSuccessWithCombinationOfGroups) {
// combination of named and unnamed groups should return a celmap
std::vector<std::pair<CelValue, CelValue>> cel_values;
cel_values.emplace_back(std::make_pair(
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(&arena_, "1")),
CelValue::CreateString(
google::protobuf::Arena::Create<std::string>(&arena_, "user"))));
cel_values.emplace_back(
std::make_pair(CelValue::CreateInt64(1),
CelValue::CreateString(
google::protobuf::Arena::Create<std::string>(&arena_, "user"))));
cel_values.emplace_back(std::make_pair(
CelValue::CreateString(
google::protobuf::Arena::Create<std::string>(&arena_, "Username")),
Expand Down Expand Up @@ -131,14 +131,14 @@ TEST_F(RegexFunctionsTest, CaptureStringSuccessWithSingleNamedGroup) {
TEST_F(RegexFunctionsTest, CaptureStringSuccessWithMultipleUnamedGroups) {
// Regex containing all unnamed groups should return a map
std::vector<std::pair<CelValue, CelValue>> cel_values;
cel_values.emplace_back(std::make_pair(
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(&arena_, "1")),
CelValue::CreateString(
google::protobuf::Arena::Create<std::string>(&arena_, "testuser"))));
cel_values.emplace_back(std::make_pair(
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(&arena_, "2")),
CelValue::CreateString(
google::protobuf::Arena::Create<std::string>(&arena_, "testdomain"))));
cel_values.emplace_back(
std::make_pair(CelValue::CreateInt64(1),
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
&arena_, "testuser"))));
cel_values.emplace_back(
std::make_pair(CelValue::CreateInt64(2),
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
&arena_, "testdomain"))));
auto container_map = google::api::expr::runtime::CreateContainerBackedMap(
absl::MakeSpan(cel_values));
// Release ownership of container_map to Arena.
Expand Down

0 comments on commit f989928

Please sign in to comment.