Skip to content

[NaaS] Register CEL extension provided string functions in evaluator. #1569

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

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
7 changes: 7 additions & 0 deletions extensions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ cc_library(
srcs = ["regex_functions.cc"],
hdrs = ["regex_functions.h"],
deps = [
"//checker:type_checker_builder",
"//common:decl",
"//common:type",
"//common:value",
"//eval/public:cel_function_registry",
"//eval/public:cel_options",
Expand Down Expand Up @@ -215,8 +218,12 @@ cc_test(
],
deps = [
":regex_functions",
"//checker:standard_library",
"//checker:validation_result",
"//common:value",
"//common:value_testing",
"//compiler",
"//compiler:compiler_factory",
"//extensions/protobuf:runtime_adapter",
"//internal:status_macros",
"//internal:testing",
Expand Down
31 changes: 31 additions & 0 deletions extensions/regex_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "checker/type_checker_builder.h"
#include "common/decl.h"
#include "common/type.h"
#include "common/value.h"
#include "eval/public/cel_function_registry.h"
#include "eval/public/cel_options.h"
Expand Down Expand Up @@ -164,6 +167,30 @@ absl::Status RegisterRegexFunctions(FunctionRegistry& registry) {
return absl::OkStatus();
}

absl::Status RegisterRegexDecls(TypeCheckerBuilder& builder) {
CEL_ASSIGN_OR_RETURN(
FunctionDecl regex_extract_decl,
MakeFunctionDecl(
std::string(kRegexExtract),
MakeOverloadDecl("regex_extract", StringType(), StringType(),
StringType(), StringType())));
CEL_RETURN_IF_ERROR(builder.AddFunction(regex_extract_decl));

CEL_ASSIGN_OR_RETURN(
FunctionDecl regex_capture_decl,
MakeFunctionDecl(std::string(kRegexCapture),
MakeOverloadDecl("regex_capture", StringType(),
StringType(), StringType())));
CEL_RETURN_IF_ERROR(builder.AddFunction(regex_capture_decl));

CEL_ASSIGN_OR_RETURN(
FunctionDecl regex_capture_n_decl,
MakeFunctionDecl(std::string(kRegexCaptureN),
MakeOverloadDecl("regex_capture", MapType(),
StringType(), StringType())));
return builder.AddFunction(regex_capture_n_decl);
}

} // namespace

absl::Status RegisterRegexFunctions(FunctionRegistry& registry,
Expand All @@ -182,4 +209,8 @@ absl::Status RegisterRegexFunctions(CelFunctionRegistry* registry,
return absl::OkStatus();
}

CheckerLibrary RegexCheckerLibrary() {
return {.id = "regex", .configure = RegisterRegexDecls};
}

} // namespace cel::extensions
10 changes: 7 additions & 3 deletions extensions/regex_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "checker/type_checker_builder.h"
#include "eval/public/cel_function_registry.h"
#include "eval/public/cel_options.h"
#include "runtime/function_registry.h"
#include "runtime/runtime_options.h"

namespace cel::extensions {

constexpr absl::string_view kRegexExtract = "re.extract";
constexpr absl::string_view kRegexCapture = "re.capture";
constexpr absl::string_view kRegexCaptureN = "re.captureN";
inline constexpr absl::string_view kRegexExtract = "re.extract";
inline constexpr absl::string_view kRegexCapture = "re.capture";
inline constexpr absl::string_view kRegexCaptureN = "re.captureN";

// Register Extract and Capture Functions for RE2
// Requires options.enable_regex to be true
Expand All @@ -36,5 +37,8 @@ absl::Status RegisterRegexFunctions(
absl::Status RegisterRegexFunctions(FunctionRegistry& registry,
const RuntimeOptions& options);

// Declarations for the regex extension library.
CheckerLibrary RegexCheckerLibrary();

} // namespace cel::extensions
#endif // THIRD_PARTY_CEL_CPP_EXTENSIONS_REGEX_FUNCTIONS_H_
58 changes: 58 additions & 0 deletions extensions/regex_functions_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
#include "absl/status/status.h"
#include "absl/status/status_matchers.h"
#include "absl/status/statusor.h"
#include "checker/standard_library.h"
#include "checker/validation_result.h"
#include "common/value.h"
#include "common/value_testing.h"
#include "compiler/compiler.h"
#include "compiler/compiler_factory.h"
#include "extensions/protobuf/runtime_adapter.h"
#include "internal/status_macros.h"
#include "internal/testing.h"
Expand Down Expand Up @@ -226,6 +230,60 @@ TEST_P(RegexFunctionsTest, RegexFunctionsTests) {
INSTANTIATE_TEST_SUITE_P(RegexFunctionsTest, RegexFunctionsTest,
ValuesIn(createParams()));

struct RegexCheckerTestCase {
const std::string expr_string;
bool is_valid;
};

class RegexCheckerLibraryTest
: public ::testing::TestWithParam<RegexCheckerTestCase> {
public:
void SetUp() override {
// Arrange: Configure the compiler.
// Add the regex checker library to the compiler builder.
ASSERT_OK_AND_ASSIGN(std::unique_ptr<CompilerBuilder> compiler_builder,
NewCompilerBuilder(descriptor_pool_));
ASSERT_THAT(compiler_builder->AddLibrary(StandardCheckerLibrary()), IsOk());
ASSERT_THAT(compiler_builder->AddLibrary(RegexCheckerLibrary()), IsOk());
ASSERT_OK_AND_ASSIGN(compiler_, std::move(*compiler_builder).Build());
}

const google::protobuf::DescriptorPool* descriptor_pool_ =
internal::GetTestingDescriptorPool();
std::unique_ptr<Compiler> compiler_;
};

TEST_P(RegexCheckerLibraryTest, RegexFunctionsTypeCheckerSuccess) {
// Act & Assert: Compile the expression and validate the result.
ASSERT_OK_AND_ASSIGN(ValidationResult result,
compiler_->Compile(GetParam().expr_string));
EXPECT_EQ(result.IsValid(), GetParam().is_valid);
}

// Returns a vector of test cases for the RegexCheckerLibraryTest.
// Returns both positive and negative test cases for the regex functions.
std::vector<RegexCheckerTestCase> createRegexCheckerParams() {
return {
{R"(re.extract('testuser@google.com', '(.*)@([^.]*)', '\\2!\\1'))", true},
{R"(re.extract('testuser@google.com', '(.*)@([^.]*)', 2))", false},
{R"(re.extract('testuser@google.com', false, '\\2!\\1'))", false},
{R"(re.extract(['foo', 'bar'], '(.*)@([^.]*)', '\\2!\\1'))", false},
{R"(re.captureN(
'The user testuser belongs to testdomain',
'The (user|domain) (?P<Username>.*) belongs to (?P<Domain>.*)'
))",
true},
{R"(re.captureN('testuser@', '(?P<username>.*)@'))", true},
{R"(re.captureN('testuser@', 2))", false},
{R"(re.captureN(['foo', 'bar'], '(?P<username>.*)@'))", false},
{R"(re.capture('foo', 'fo(o)'))", true},
{R"(re.capture('foo', 2))", false},
{R"(re.capture(true, 'fo(o)'))", false}};
}

INSTANTIATE_TEST_SUITE_P(RegexCheckerLibraryTest, RegexCheckerLibraryTest,
ValuesIn(createRegexCheckerParams()));

} // namespace

} // namespace cel::extensions