|
29 | 29 | ) |
30 | 30 |
|
31 | 31 |
|
| 32 | +# Currently cel-python does not support re2. So we are overriding their `matches` |
| 33 | +# function with our own that leverages re2. Note there is a PR in cel-python to |
| 34 | +# add re2 support. See https://github.com/cloud-custodian/cel-python/pull/67. |
| 35 | +# Once that lands, this `matches` override can be removed. |
| 36 | +def cel_matches(text: str, pattern: str) -> celpy.Result: |
| 37 | + try: |
| 38 | + m = re2.search(pattern, text) |
| 39 | + except re2.error as ex: |
| 40 | + msg = "match error" |
| 41 | + raise celpy.CELEvalError(msg, ex.__class__, ex.args) from ex |
| 42 | + return celtypes.BoolType(m is not None) |
| 43 | + |
| 44 | + |
32 | 45 | def cel_get_field(message: celtypes.Value, field_name: celtypes.Value) -> celpy.Result: |
33 | 46 | if not isinstance(message, MessageType): |
34 | 47 | msg = "invalid argument, expected message" |
@@ -1553,22 +1566,14 @@ def __peek(self, char: str) -> bool: |
1553 | 1566 | return self._index < len(self._string) and self._string[self._index] == char |
1554 | 1567 |
|
1555 | 1568 |
|
1556 | | -def matches(text: str, pattern: str) -> celpy.Result: |
1557 | | - try: |
1558 | | - m = re2.search(pattern, text) |
1559 | | - except re2.error as ex: |
1560 | | - return celpy.CELEvalError("match error", ex.__class__, ex.args) |
1561 | | - return celtypes.BoolType(m is not None) |
1562 | | - |
1563 | | - |
1564 | 1569 | def make_extra_funcs(locale: str) -> dict[str, celpy.CELFunction]: |
1565 | 1570 | # TODO(#257): Fix types and add tests for StringFormat. |
1566 | 1571 | # For now, ignoring the type. |
1567 | 1572 | string_fmt = string_format.StringFormat(locale) # type: ignore |
1568 | 1573 | return { |
1569 | 1574 | # Missing standard functions |
1570 | 1575 | "format": string_fmt.format, |
1571 | | - "matches": matches, |
| 1576 | + "matches": cel_matches, |
1572 | 1577 | # protovalidate specific functions |
1573 | 1578 | "getField": cel_get_field, |
1574 | 1579 | "isNan": cel_is_nan, |
|
0 commit comments