-
Notifications
You must be signed in to change notification settings - Fork 17
Add a cache for compiled regex patterns #459
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,24 +14,59 @@ | |
|
|
||
| package build.buf.protovalidate; | ||
|
|
||
| import com.google.re2j.Pattern; | ||
| import dev.cel.bundle.Cel; | ||
| import dev.cel.bundle.CelFactory; | ||
| import dev.cel.checker.CelCheckerBuilder; | ||
| import dev.cel.checker.CelStandardDeclarations; | ||
| import dev.cel.common.CelOptions; | ||
| import dev.cel.common.CelVarDecl; | ||
| import dev.cel.common.types.SimpleType; | ||
| import dev.cel.compiler.CelCompilerLibrary; | ||
| import dev.cel.extensions.CelExtensions; | ||
| import dev.cel.parser.CelParserBuilder; | ||
| import dev.cel.parser.CelStandardMacro; | ||
| import dev.cel.runtime.CelRuntimeBuilder; | ||
| import dev.cel.runtime.CelRuntimeLibrary; | ||
| import dev.cel.runtime.CelStandardFunctions; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
|
|
||
| /** | ||
| * Custom {@link CelCompilerLibrary} and {@link CelRuntimeLibrary}. Provides all the custom | ||
| * extension function definitions and overloads. | ||
| */ | ||
| final class ValidateLibrary implements CelCompilerLibrary, CelRuntimeLibrary { | ||
|
|
||
| private static final CelOptions CEL_OPTIONS = CelOptions.DEFAULT; | ||
|
|
||
| private final ConcurrentMap<String, Pattern> patternCache = new ConcurrentHashMap<>(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike the proposed fix in google/cel-java#1038, this cache is scoped to a Validator and not global. |
||
|
|
||
| /** Creates a ValidateLibrary with all custom declarations and overloads. */ | ||
| ValidateLibrary() {} | ||
|
|
||
| static Cel newCel() { | ||
| ValidateLibrary validateLibrary = new ValidateLibrary(); | ||
| // NOTE: CelExtensions.strings() does not implement string.reverse() or strings.quote() which | ||
| // are available in protovalidate-go. Fixed in https://github.com/google/cel-java/pull/998. | ||
| return CelFactory.standardCelBuilder() | ||
| .setOptions(CEL_OPTIONS) | ||
| // Drop stdlib matches; CustomOverload provides a caching replacement. | ||
| // Ref: https://github.com/google/cel-java/issues/1038 | ||
| .setStandardEnvironmentEnabled(false) | ||
| .setStandardDeclarations( | ||
| CelStandardDeclarations.newBuilder() | ||
| .excludeFunctions(CelStandardDeclarations.StandardFunction.MATCHES) | ||
| .build()) | ||
| .setStandardFunctions( | ||
| CelStandardFunctions.newBuilder() | ||
| .excludeFunctions(CelStandardFunctions.StandardFunction.MATCHES) | ||
| .build()) | ||
| .addCompilerLibraries(validateLibrary, CelExtensions.strings()) | ||
| .addRuntimeLibraries(validateLibrary, CelExtensions.strings()) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setParserOptions(CelParserBuilder parserBuilder) { | ||
| parserBuilder.setStandardMacros( | ||
|
|
@@ -54,6 +89,6 @@ public void setCheckerOptions(CelCheckerBuilder checkerBuilder) { | |
|
|
||
| @Override | ||
| public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) { | ||
| runtimeBuilder.addFunctionBindings(CustomOverload.create()); | ||
| runtimeBuilder.addFunctionBindings(CustomOverload.create(patternCache, CEL_OPTIONS)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,9 +18,7 @@ | |
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import dev.cel.bundle.Cel; | ||
| import dev.cel.bundle.CelFactory; | ||
| import dev.cel.common.CelAbstractSyntaxTree; | ||
| import dev.cel.common.CelOptions; | ||
| import dev.cel.common.CelValidationException; | ||
| import dev.cel.common.CelValidationResult; | ||
| import dev.cel.runtime.CelEvaluationException; | ||
|
|
@@ -31,14 +29,7 @@ | |
|
|
||
| public class CustomOverloadTest { | ||
|
|
||
| private final ValidateLibrary validateLibrary = new ValidateLibrary(); | ||
| private final Cel cel = | ||
| CelFactory.standardCelBuilder() | ||
| .addCompilerLibraries(validateLibrary) | ||
| .addRuntimeLibraries(validateLibrary) | ||
| .setOptions( | ||
| CelOptions.DEFAULT.toBuilder().evaluateCanonicalTypesToNativeValues(true).build()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we still need
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is deprecated and the default is true so I went ahead and removed it. |
||
| .build(); | ||
| private final Cel cel = ValidateLibrary.newCel(); | ||
|
|
||
| @Test | ||
| public void testIsInf() throws Exception { | ||
|
|
@@ -173,6 +164,19 @@ public void testBytesContains() throws Exception { | |
| assertThat(evalToBool("bytes('12345').contains(bytes('123456'))")).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMatchesPartialMatch() throws Exception { | ||
| // CelOptions.DEFAULT sets enableRegexPartialMatch(true), so an unanchored regex should | ||
| // match anywhere in the input (find()), not require a full-string match. | ||
| assertThat(evalToBool("'hello world'.matches('world')")).isTrue(); | ||
| assertThat(evalToBool("'hello world'.matches('ell')")).isTrue(); | ||
| // Anchored patterns still behave the same. | ||
| assertThat(evalToBool("'hello'.matches('^hello$')")).isTrue(); | ||
| assertThat(evalToBool("'hello world'.matches('^hello$')")).isFalse(); | ||
| // Global form. | ||
| assertThat(evalToBool("matches('hello world', 'world')")).isTrue(); | ||
| } | ||
|
|
||
| private Object eval(String source) throws Exception { | ||
| return eval(source, Collections.emptyMap()); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Results: