Skip to content

Commit

Permalink
Add support for string matches (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
cloutierMat authored Oct 25, 2024
1 parent 7aced14 commit 3ca2d54
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: false
matrix:
python_version:
- "3.10"
- "3.11"
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This is a fork of the original [`airspeed`](https://github.com/purcell/airspeed)
⚠️ Note: This fork of `airspeed` focuses on providing maximum parity with AWS' implementation of Velocity templates (used in, e.g., API Gateway or AppSync). In some cases, the behavior may diverge from the VTL spec, or from the Velocity [reference implementation](https://velocity.apache.org/download.cgi).

## Change Log:
* v0.6.6: add support for `$string.matches( $pattern )`; fix bug where some escaped character would prevent string matching
* v0.6.5: handle `$map.put('key', null)` correctly
* v0.6.4: add support for string.indexOf, string.substring and array.isEmpty
* v0.6.3: array notation for dicts using string literals and merge upstream patches
Expand Down
3 changes: 2 additions & 1 deletion airspeed/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def dict_to_string(obj: dict) -> str:
"contains": lambda self, value: value in self,
"indexOf": lambda self, ch: self.index(ch),
"substring": lambda self, start, end=None: self[start:end],
"matches": lambda self, pattern: bool(re.fullmatch(pattern, self)),
},
list: {
"size": lambda self: len(self),
Expand Down Expand Up @@ -455,7 +456,7 @@ def calculate(self, namespace, loader):


class StringLiteral(_Element):
STRING = re.compile(r"'((?:\\['nrbt\\\\\\$]|[^'\\])*)'(.*)", re.S)
STRING = re.compile(r"'([^']*)'(.*)", re.S)
ESCAPED_CHAR = re.compile(r"\\([nrbt'\\])")

def parse(self):
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="airspeed-ext",
version="0.6.5",
version="0.6.6",
description=(
"Airspeed is a powerful and easy-to-use templating engine "
"for Python that aims for a high level of compatibility "
Expand All @@ -30,6 +30,7 @@
"flake8-isort",
"pytest",
"pytest-httpserver",
"localstack-snapshot",
]},
test_suite="tests",
tests_require=[],
Expand Down
11 changes: 8 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from localstack.testing.aws.util import (
base_aws_client_factory,
base_aws_session,
primary_testing_aws_client,
base_testing_aws_client,
)

pytest_plugins = [
"localstack.testing.pytest.fixtures",
"localstack.testing.pytest.snapshot",
"localstack_snapshot.pytest.snapshot",
]


Expand All @@ -23,4 +23,9 @@ def aws_client_factory(aws_session):

@pytest.fixture(scope="session")
def aws_client(aws_client_factory):
return primary_testing_aws_client(aws_client_factory)
return base_testing_aws_client(aws_client_factory)


@pytest.fixture(scope="function")
def snapshot(_snapshot_session):
return _snapshot_session
36 changes: 35 additions & 1 deletion tests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import pytest
import requests
import six
from localstack.constants import TEST_AWS_ACCOUNT_ID
from localstack.testing.aws.util import is_aws_cloud
from localstack.testing.config import TEST_AWS_ACCOUNT_ID
from localstack.testing.pytest import fixtures
from localstack.utils.archives import unzip
from localstack.utils.files import save_file
Expand Down Expand Up @@ -845,6 +845,40 @@ def test_dict_to_string(self, test_render):
template = "#set( $myObject = {'k1': 'v1', 'k2': 'v2'} )$myObject.toString()"
test_render(template)

def test_string_matches_true(self, test_render):
template = "#set( $myString = '123456789' )$myString.matches( '[0-9]*' )"
test_render(template)

def test_string_matches_false(self, test_render):
template = "#set( $myString = 'd123' )$myString.matches( '[0-9]*' )"
test_render(template)

def test_string_matches_full_date(self, test_render):
template = (
"#set( $myString = '2020-01-20T08:00:00.000Z' )"
"$myString.matches('^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00\.000Z$')" # noqa
)
test_render(template)

template = (
'#set( $myString = "2020-01-20T08:00:00.000Z" )'
'$myString.matches("^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:00\.000Z$")' # noqa
)
test_render(template)

def test_string_with_escaped_char(self, test_render):
template = "#set( $myString = '\{\n\t\r\%\5' )$myString" # noqa
test_render(template)

template = '#set( $myString = "\{\n\t\r\%\5" )$myString' # noqa
test_render(template)

template = "'\{\n\t\r\%\5'" # noqa
test_render(template)

template = '"\{\n\t\r\%\5"' # noqa
test_render(template)


class TestInternals:
"""
Expand Down
36 changes: 36 additions & 0 deletions tests/test_templating.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -1068,5 +1068,41 @@
"render-result-1-cli": "\n \"has-value\"\n ",
"render-result-1": "\"does-not-have-value\""
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_true": {
"recorded-date": "24-10-2024, 19:59:43",
"recorded-content": {
"render-result-1-cli": "true",
"render-result-1": "true"
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_false": {
"recorded-date": "24-10-2024, 19:59:21",
"recorded-content": {
"render-result-1-cli": "false",
"render-result-1": "false"
}
},
"tests/test_templating.py::TestTemplating::test_string_with_escaped_char": {
"recorded-date": "24-10-2024, 22:14:20",
"recorded-content": {
"render-result-1-cli": "\\{\n\t\r\\%\u0005",
"render-result-1": "\\{\n\t\r\\%\u0005",
"render-result-2-cli": "\\{\n\t\r\\%\u0005",
"render-result-2": "\\{\n\t\r\\%\u0005",
"render-result-3-cli": "'\\{\n\t\r\\%\u0005'",
"render-result-3": "'\\{\n\t\r\\%\u0005'",
"render-result-4-cli": "\"\\{\n\t\r\\%\u0005\"",
"render-result-4": "\"\\{\n\t\r\\%\u0005\""
}
},
"tests/test_templating.py::TestTemplating::test_string_matches_full_date": {
"recorded-date": "24-10-2024, 21:58:12",
"recorded-content": {
"render-result-1-cli": "true",
"render-result-1": "true",
"render-result-2-cli": "true",
"render-result-2": "true"
}
}
}

0 comments on commit 3ca2d54

Please sign in to comment.