-
Notifications
You must be signed in to change notification settings - Fork 9
Add re2 as optional dependency #346
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 3 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 |
|---|---|---|
|
|
@@ -23,6 +23,12 @@ | |
| from protovalidate.internal import string_format | ||
| from protovalidate.internal.rules import MessageType, field_to_cel | ||
|
|
||
| _USE_RE2 = True | ||
| try: | ||
| import re2 # type: ignore | ||
|
||
| except ImportError: | ||
| _USE_RE2 = False | ||
|
|
||
| # See https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address | ||
| _email_regex = re.compile( | ||
| r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" | ||
|
|
@@ -1553,11 +1559,34 @@ def __peek(self, char: str) -> bool: | |
| return self._index < len(self._string) and self._string[self._index] == char | ||
|
|
||
|
|
||
| def cel_matches_re(text: str, pattern: str) -> celpy.Result: | ||
| try: | ||
| m = re.search(pattern, text) | ||
| except re.error as ex: | ||
| return celpy.CELEvalError("match error", ex.__class__, ex.args) | ||
|
|
||
| return celtypes.BoolType(m is not None) | ||
|
|
||
|
|
||
| def cel_matches_re2(text: str, pattern: str) -> celpy.Result: | ||
| try: | ||
| m = re2.search(pattern, text) | ||
| except re2.error as ex: | ||
| return celpy.CELEvalError("match error", ex.__class__, ex.args) | ||
|
|
||
| return celtypes.BoolType(m is not None) | ||
|
|
||
|
|
||
| cel_matches = cel_matches_re2 if _USE_RE2 else cel_matches_re | ||
|
|
||
|
|
||
| def make_extra_funcs() -> dict[str, celpy.CELFunction]: | ||
| string_fmt = string_format.StringFormat() | ||
| return { | ||
| # Missing standard functions | ||
| "format": string_fmt.format, | ||
| # Overridden standard functions | ||
| "matches": cel_matches, | ||
| # protovalidate specific functions | ||
| "getField": cel_get_field, | ||
| "isNan": cel_is_nan, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright 2023-2025 Buf Technologies, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import importlib.util | ||
| import unittest | ||
|
|
||
| import celpy | ||
| from celpy import celtypes | ||
|
|
||
| from protovalidate.internal.extra_func import cel_matches_re, cel_matches_re2 | ||
|
|
||
| _USE_RE2 = True | ||
| spec = importlib.util.find_spec("re2") | ||
|
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. |
||
| if spec is None: | ||
| _USE_RE2 = False | ||
|
|
||
|
|
||
| class TestCollectViolations(unittest.TestCase): | ||
| @unittest.skipUnless(_USE_RE2, "Requires 're2'") | ||
| def test_function_matches_re2(self): | ||
| empty_string = celtypes.StringType("") | ||
| # \z is valid re2 syntax for end of text | ||
| self.assertTrue(cel_matches_re2(empty_string, "^\\z")) | ||
| # \Z is invalid re2 syntax | ||
| self.assertIsInstance(cel_matches_re2(empty_string, "^\\Z"), celpy.CELEvalError) | ||
|
|
||
| @unittest.skipUnless(_USE_RE2 is False, "Requires 're'") | ||
| def test_function_matches_re(self): | ||
| empty_string = celtypes.StringType("") | ||
| # \z is invalid re syntax | ||
| self.assertIsInstance(cel_matches_re(empty_string, "^\\z"), celpy.CELEvalError) | ||
| # \Z is valid re syntax for end of text | ||
| self.assertTrue(cel_matches_re(empty_string, "^\\Z")) | ||
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.
I think we want to avoid
uv pip, and instead do something likeuv sync --extra re2?: https://docs.astral.sh/uv/concepts/projects/sync/#syncing-optional-dependencies