Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make C++ CEL Regex Extension return map with int index and string names #1037

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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