Skip to content

Commit

Permalink
Use re2 instead of re for match expressions
Browse files Browse the repository at this point in the history
CEL specifies that match should use re2 pattern matching and not re
semantics.
  • Loading branch information
ddn0 committed May 24, 2024
1 parent a27498e commit 79cbff5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
65 changes: 62 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ python-dateutil = "^2.9.0.post0"
pyyaml = "^6.0.1"
types-pyyaml = "^6.0.12.20240311"
types-python-dateutil = "^2.9.0.20240316"
google-re2 = "^1.1.20240501"

[tool.poetry.group.dev.dependencies]
behave = "^1.2.6"
Expand Down
13 changes: 11 additions & 2 deletions src/celpy/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import logging
import operator
import re
import re2
import sys
from functools import reduce, wraps
from typing import (Any, Callable, Dict, Iterable, Iterator, List, Mapping,
Expand All @@ -62,7 +63,6 @@

logger = logging.getLogger("evaluation")


class CELSyntaxError(Exception):
"""CEL Syntax error -- the AST did not have the expected structure."""
def __init__(self, arg: Any, line: Optional[int] = None, column: Optional[int] = None) -> None:
Expand Down Expand Up @@ -293,6 +293,15 @@ def operator_in(item: Result, container: Result) -> Result:
return result


def function_matches(text: str, pattern: str):
try:
m = re2.search(pattern, text)
except re2.error as ex:
return CELEvalError("match error", ex.__class__, ex.args)

return celpy.celtypes.BoolType(m is not None)


def function_size(container: Result) -> Result:
"""
The size() function applied to a Value. Delegate to Python's :py:func:`len`.
Expand Down Expand Up @@ -340,7 +349,7 @@ def function_size(container: Result) -> Result:
# StringType methods
"endsWith": lambda s, text: celpy.celtypes.BoolType(s.endswith(text)),
"startsWith": lambda s, text: celpy.celtypes.BoolType(s.startswith(text)),
"matches": lambda s, pattern: celpy.celtypes.BoolType(re.search(pattern, s) is not None),
"matches": function_matches,
"contains": lambda s, text: celpy.celtypes.BoolType(text in s),
# TimestampType methods. Type details are redundant, but required because of the lambdas
"getDate": lambda ts, tz_name=None: celpy.celtypes.IntType(ts.getDate(tz_name)),
Expand Down
7 changes: 7 additions & 0 deletions tests/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ def test_operator_in():
assert isinstance(operator_in(celtypes.IntType(-1), container_2), CELEvalError)


def test_function_matches():
empty_string = celtypes.StringType("")
# re2-specific patterns which behave differently than standard re
assert function_matches(empty_string, "^\\z")
assert isinstance(function_matches(empty_string, "^\\Z"), CELEvalError)


def test_function_size():
container_1 = celtypes.ListType([
celtypes.IntType(42),
Expand Down

0 comments on commit 79cbff5

Please sign in to comment.