From 09c1bdba08c190001fc23c01e246bd5ac5040f89 Mon Sep 17 00:00:00 2001 From: CEL Dev Team Date: Wed, 30 Oct 2024 14:31:27 -0700 Subject: [PATCH] Make C++ CEL Regex Extension return map with int index and string names PiperOrigin-RevId: 691555991 --- extensions/regex_functions.cc | 13 +++++++------ extensions/regex_functions_test.cc | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/extensions/regex_functions.cc b/extensions/regex_functions.cc index 912fa6511..3872e6217 100644 --- a/extensions/regex_functions.cc +++ b/extensions/regex_functions.cc @@ -119,13 +119,14 @@ CelValue CaptureStringN(Arena* arena, CelValue::StringHolder target, std::vector> 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(arena, it->second)) + : CelValue::CreateInt64(index); cel_values.emplace_back( - CelValue::CreateString(google::protobuf::Arena::Create(arena, name)), - CelValue::CreateString(google::protobuf::Arena::Create( - arena, captured_strings[index - 1]))); + name, CelValue::CreateString(google::protobuf::Arena::Create( + arena, captured_strings[index - 1]))); } auto container_map = google::api::expr::runtime::CreateContainerBackedMap( absl::MakeSpan(cel_values)); diff --git a/extensions/regex_functions_test.cc b/extensions/regex_functions_test.cc index 50f02d6ba..9aef8f91c 100644 --- a/extensions/regex_functions_test.cc +++ b/extensions/regex_functions_test.cc @@ -77,10 +77,10 @@ class RegexFunctionsTest : public ::testing::TestWithParam { TEST_F(RegexFunctionsTest, CaptureStringSuccessWithCombinationOfGroups) { // combination of named and unnamed groups should return a celmap std::vector> cel_values; - cel_values.emplace_back(std::make_pair( - CelValue::CreateString(google::protobuf::Arena::Create(&arena_, "1")), - CelValue::CreateString( - google::protobuf::Arena::Create(&arena_, "user")))); + cel_values.emplace_back( + std::make_pair(CelValue::CreateInt64(1), + CelValue::CreateString( + google::protobuf::Arena::Create(&arena_, "user")))); cel_values.emplace_back(std::make_pair( CelValue::CreateString( google::protobuf::Arena::Create(&arena_, "Username")), @@ -131,14 +131,14 @@ TEST_F(RegexFunctionsTest, CaptureStringSuccessWithSingleNamedGroup) { TEST_F(RegexFunctionsTest, CaptureStringSuccessWithMultipleUnamedGroups) { // Regex containing all unnamed groups should return a map std::vector> cel_values; - cel_values.emplace_back(std::make_pair( - CelValue::CreateString(google::protobuf::Arena::Create(&arena_, "1")), - CelValue::CreateString( - google::protobuf::Arena::Create(&arena_, "testuser")))); - cel_values.emplace_back(std::make_pair( - CelValue::CreateString(google::protobuf::Arena::Create(&arena_, "2")), - CelValue::CreateString( - google::protobuf::Arena::Create(&arena_, "testdomain")))); + cel_values.emplace_back( + std::make_pair(CelValue::CreateInt64(1), + CelValue::CreateString(google::protobuf::Arena::Create( + &arena_, "testuser")))); + cel_values.emplace_back( + std::make_pair(CelValue::CreateInt64(2), + CelValue::CreateString(google::protobuf::Arena::Create( + &arena_, "testdomain")))); auto container_map = google::api::expr::runtime::CreateContainerBackedMap( absl::MakeSpan(cel_values)); // Release ownership of container_map to Arena.