Skip to content

Commit

Permalink
add contains and toString (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvernaz authored Sep 19, 2023
1 parent b4f7bd4 commit 0981c66
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +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.2: add support to contains and toString functions
* v0.6.1: improve handling of multi-line dict expressions
* v0.6.0: add initial setup for snapshot testing against AWS and Java VTL; enhance AWS parity

Expand Down
6 changes: 6 additions & 0 deletions airspeed/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ def dict_put(self, key, value):
return existing


def dict_to_string(obj: dict) -> str:
return "{" + ", ".join([f"{k}={v}" for k, v in obj.items()]) + "}"


__additional_methods__ = {
str: {
"length": lambda self: len(self),
"replaceAll": lambda self, pattern, repl: re.sub(pattern, repl, self),
"startsWith": lambda self, prefix: self.startswith(prefix),
"contains": lambda self, value: value in self,
},
list: {
"size": lambda self: len(self),
Expand All @@ -43,6 +48,7 @@ def dict_put(self, key, value):
"put": dict_put,
"putAll": lambda self, values: self.update(values),
"keySet": lambda self: self.keys(),
"toString": lambda self: dict_to_string(self),
},
}

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.1",
version="0.6.2",
description=(
"Airspeed is a powerful and easy-to-use templating engine "
"for Python that aims for a high level of compatibility "
Expand Down
10 changes: 10 additions & 0 deletions tests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@ def test_string_starts_with_true(self, test_render):
def test_string_starts_with_false(self, test_render):
test_render("#set($foo = 'nofoobar123') #if($foo.startsWith('foo'))yes!#end")

def test_string_contains_true(self, test_render):
test_render("#set($foo = 'foobar123') #if($foo.contains('foo'))yes!#end")

def test_string_contains_false(self, test_render):
test_render("#set($foo = 'nofoobar123') #if($foo.contains('foo'))yes!#end")

def test_dict_put_item(self, test_render):
template = (
"#set( $ignore = $test_dict.put('k', 'new value') )"
Expand Down Expand Up @@ -811,6 +817,10 @@ def test_multiline_dict_in_set_statement(self, test_render):
"""
test_render(template, {"test_dict": {"k1": "v1"}})

def test_dict_to_string(self, test_render):
template = "#set( $myObject = {'k1': 'v1', 'k2': 'v2'} )$myObject.toString()"
test_render(template)


class TestInternals:
"""
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 @@ -1012,5 +1012,26 @@
"render-result-1-cli": "\n {\n \"operation\": \"PutItem\",\n \"key\": \"user1\",\n \"domain\": \"domain1\"\n }\n ",
"render-result-1": "\n {\n \"operation\": \"PutItem\",\n \"key\": \"user1\",\n \"domain\": \"domain1\"\n }\n "
}
},
"tests/test_templating.py::TestTemplating::test_dict_to_string": {
"recorded-date": "17-09-2023, 16:17:52",
"recorded-content": {
"render-result-1-cli": "{k1=v1, k2=v2}",
"render-result-1": "{k1=v1, k2=v2}"
}
},
"tests/test_templating.py::TestTemplating::test_string_contains_true": {
"recorded-date": "17-09-2023, 12:45:26",
"recorded-content": {
"render-result-1-cli": " yes!",
"render-result-1": " yes!"
}
},
"tests/test_templating.py::TestTemplating::test_string_contains_false": {
"recorded-date": "17-09-2023, 12:46:09",
"recorded-content": {
"render-result-1-cli": " yes!",
"render-result-1": " yes!"
}
}
}

0 comments on commit 0981c66

Please sign in to comment.