Skip to content

Commit

Permalink
Add more appsync methods (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrw authored Nov 14, 2023
1 parent 792feaa commit 5d6bfef
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
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"
}
}
}

0 comments on commit 5d6bfef

Please sign in to comment.