Skip to content
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

Add more appsync methods #16

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Expand Down
3 changes: 3 additions & 0 deletions airspeed/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 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.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 "
Expand Down
9 changes: 9 additions & 0 deletions tests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()")

Expand All @@ -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') )"
Expand Down
21 changes: 21 additions & 0 deletions tests/test_templating.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Loading