diff --git a/README.md b/README.md index c3c8dbb..aa0fcdb 100644 --- a/README.md +++ b/README.md @@ -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.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 * v0.6.2: add support to contains and toString functions * v0.6.1: improve handling of multi-line dict expressions diff --git a/airspeed/operators.py b/airspeed/operators.py index 53e53d3..8aee97e 100644 --- a/airspeed/operators.py +++ b/airspeed/operators.py @@ -38,12 +38,15 @@ def dict_to_string(obj: dict) -> str: "replaceAll": lambda self, pattern, repl: re.sub(pattern, repl, self), "startsWith": lambda self, prefix: self.startswith(prefix), "contains": lambda self, value: value in self, + "indexOf": lambda self, ch: self.index(ch), + "substring": lambda self, start, end=None: self[start:end], }, list: { "size": lambda self: len(self), "get": lambda self, index: self[index], "contains": lambda self, value: value in self, "add": lambda self, value: self.append(value), + "isEmpty": lambda self: len(self) == 0, }, dict: { "put": dict_put, diff --git a/setup.py b/setup.py index a8a5149..9280dff 100755 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name="airspeed-ext", - version="0.6.3", + version="0.6.4", description=( "Airspeed is a powerful and easy-to-use templating engine " "for Python that aims for a high level of compatibility " diff --git a/tests/test_templating.py b/tests/test_templating.py index 3641b20..f345ffc 100644 --- a/tests/test_templating.py +++ b/tests/test_templating.py @@ -774,6 +774,9 @@ def test_array_add_item(self, test_render): ) test_render(template) + def test_array_is_empty(self, test_render): + test_render("#set($foo = [1, 2, 3]) $foo.isEmpty()") + def test_string_length(self, test_render): test_render("#set($foo = 'foobar123') $foo.length()") @@ -792,6 +795,12 @@ def test_string_contains_true(self, test_render): def test_string_contains_false(self, test_render): test_render("#set($foo = 'nofoobar123') #if($foo.contains('foo'))yes!#end") + def test_string_index_of(self, test_render): + test_render("#set($foo = 'something') $foo.indexOf('e')") + + def test_string_substring(self, test_render): + test_render("#set($foo = 'something') $foo.substring(3, 6)") + def test_dict_put_item(self, test_render): template = ( "#set( $ignore = $test_dict.put('k', 'new value') )" diff --git a/tests/test_templating.snapshot.json b/tests/test_templating.snapshot.json index 5c19728..c24ca2d 100644 --- a/tests/test_templating.snapshot.json +++ b/tests/test_templating.snapshot.json @@ -1040,5 +1040,26 @@ "render-result-1-cli": "$a.b.c['foo:bar']", "render-result-1": "baz" } + }, + "tests/test_templating.py::TestTemplating::test_array_is_empty": { + "recorded-date": "14-11-2023, 13:05:28", + "recorded-content": { + "render-result-1-cli": " false", + "render-result-1": " false" + } + }, + "tests/test_templating.py::TestTemplating::test_string_index_of": { + "recorded-date": "14-11-2023, 13:10:39", + "recorded-content": { + "render-result-1-cli": " 3", + "render-result-1": " 3" + } + }, + "tests/test_templating.py::TestTemplating::test_string_substring": { + "recorded-date": "14-11-2023, 15:43:22", + "recorded-content": { + "render-result-1-cli": " eth", + "render-result-1": " eth" + } } }