Skip to content

Commit f989928

Browse files
CEL Dev Teamcopybara-github
CEL Dev Team
authored andcommitted
Make C++ CEL Regex Extension return map with int index and string names
PiperOrigin-RevId: 691555991
1 parent 883d727 commit f989928

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

extensions/regex_functions.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,14 @@ CelValue CaptureStringN(Arena* arena, CelValue::StringHolder target,
119119
std::vector<std::pair<CelValue, CelValue>> cel_values;
120120
for (int index = 1; index <= capturing_groups_count; index++) {
121121
auto it = named_capturing_groups_map.find(index);
122-
std::string name = it != named_capturing_groups_map.end()
123-
? it->second
124-
: std::to_string(index);
122+
CelValue name =
123+
it != named_capturing_groups_map.end()
124+
? CelValue::CreateString(
125+
google::protobuf::Arena::Create<std::string>(arena, it->second))
126+
: CelValue::CreateInt64(index);
125127
cel_values.emplace_back(
126-
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(arena, name)),
127-
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
128-
arena, captured_strings[index - 1])));
128+
name, CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
129+
arena, captured_strings[index - 1])));
129130
}
130131
auto container_map = google::api::expr::runtime::CreateContainerBackedMap(
131132
absl::MakeSpan(cel_values));

extensions/regex_functions_test.cc

+12-12
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ class RegexFunctionsTest : public ::testing::TestWithParam<TestCase> {
7777
TEST_F(RegexFunctionsTest, CaptureStringSuccessWithCombinationOfGroups) {
7878
// combination of named and unnamed groups should return a celmap
7979
std::vector<std::pair<CelValue, CelValue>> cel_values;
80-
cel_values.emplace_back(std::make_pair(
81-
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(&arena_, "1")),
82-
CelValue::CreateString(
83-
google::protobuf::Arena::Create<std::string>(&arena_, "user"))));
80+
cel_values.emplace_back(
81+
std::make_pair(CelValue::CreateInt64(1),
82+
CelValue::CreateString(
83+
google::protobuf::Arena::Create<std::string>(&arena_, "user"))));
8484
cel_values.emplace_back(std::make_pair(
8585
CelValue::CreateString(
8686
google::protobuf::Arena::Create<std::string>(&arena_, "Username")),
@@ -131,14 +131,14 @@ TEST_F(RegexFunctionsTest, CaptureStringSuccessWithSingleNamedGroup) {
131131
TEST_F(RegexFunctionsTest, CaptureStringSuccessWithMultipleUnamedGroups) {
132132
// Regex containing all unnamed groups should return a map
133133
std::vector<std::pair<CelValue, CelValue>> cel_values;
134-
cel_values.emplace_back(std::make_pair(
135-
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(&arena_, "1")),
136-
CelValue::CreateString(
137-
google::protobuf::Arena::Create<std::string>(&arena_, "testuser"))));
138-
cel_values.emplace_back(std::make_pair(
139-
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(&arena_, "2")),
140-
CelValue::CreateString(
141-
google::protobuf::Arena::Create<std::string>(&arena_, "testdomain"))));
134+
cel_values.emplace_back(
135+
std::make_pair(CelValue::CreateInt64(1),
136+
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
137+
&arena_, "testuser"))));
138+
cel_values.emplace_back(
139+
std::make_pair(CelValue::CreateInt64(2),
140+
CelValue::CreateString(google::protobuf::Arena::Create<std::string>(
141+
&arena_, "testdomain"))));
142142
auto container_map = google::api::expr::runtime::CreateContainerBackedMap(
143143
absl::MakeSpan(cel_values));
144144
// Release ownership of container_map to Arena.

0 commit comments

Comments
 (0)