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

Fix putting nulls in maps #17

Merged
merged 4 commits into from
Dec 12, 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
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

install: ## Install dependencies in local virtualenv folder
(test `which virtualenv` || $(PIP_CMD) install --user virtualenv) && \
(test -e $(VENV_DIR) || virtualenv $(VENV_OPTS) $(VENV_DIR)) && \
($(VENV_RUN) && $(PIP_CMD) install --upgrade pip) && \
($(VENV_RUN); $(PIP_CMD) install .[dev])
(test -e $(VENV_DIR) || python -m venv $(VENV_OPTS) $(VENV_DIR)) && \
($(VENV_RUN) && $(PIP_CMD) install --upgrade pip) && \
($(VENV_RUN); $(PIP_CMD) install .[dev])

publish: ## Publish the library to the central PyPi repository
# build and upload archive
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.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
* v0.6.2: add support to contains and toString functions
Expand Down
12 changes: 12 additions & 0 deletions airspeed/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,17 @@ def evaluate_raw(self, stream, namespace, loader):
stream.write(self.text)


class NullLiteral(_Element):
NULL = re.compile(r"(?:null)(.*)", re.S)

def parse(self):
self.identity_match(self.NULL)
self.value = None

def calculate(self, namespace, loader):
return self.value


class IntegerLiteral(_Element):
INTEGER = re.compile(r"(-?\d+)(.*)", re.S)

Expand Down Expand Up @@ -576,6 +587,7 @@ class Value(_Element):
def parse(self):
self.expression = self.next_element(
(
NullLiteral,
FormalReference,
FloatingPointLiteral,
IntegerLiteral,
Expand Down
4 changes: 2 additions & 2 deletions 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.4",
version="0.6.5",
description=(
"Airspeed is a powerful and easy-to-use templating engine "
"for Python that aims for a high level of compatibility "
Expand All @@ -28,7 +28,7 @@
"flake8",
"flake8-black",
"flake8-isort",
"pytest==6.2.4",
"pytest",
"pytest-httpserver",
]},
test_suite="tests",
Expand Down
12 changes: 12 additions & 0 deletions tests/test_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,18 @@ def test_dict_put_item(self, test_render):
)
test_render(template, {"test_dict": {"k": "initial value"}})

def test_put_null_in_map(self, test_render):
template = r"""
#set( $myMap = {} )
#set($ignore = $myMap.put('k', null))
#if("$myMap.k" == "")
"does-not-have-value"
#else
"has-value"
#end
"""
test_render(template, ignore_whitespaces=True)

def test_dict_putall_items(self, test_render):
template = (
"#set( $ignore = $test_dict.putAll({'k1': 'v3', 'k2': 'v2'}))"
Expand Down
7 changes: 7 additions & 0 deletions tests/test_templating.snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -1061,5 +1061,12 @@
"render-result-1-cli": " eth",
"render-result-1": " eth"
}
},
"tests/test_templating.py::TestTemplating::test_put_null_in_map": {
"recorded-date": "08-12-2023, 17:07:36",
"recorded-content": {
"render-result-1-cli": "\n \"has-value\"\n ",
"render-result-1": "\"does-not-have-value\""
}
}
}
Loading