Skip to content

Commit

Permalink
Merge pull request #3 from xoeye/fix_add_variables_to_expression
Browse files Browse the repository at this point in the history
Allow snake_cased attributes in add_variables_to_expression
  • Loading branch information
Peter Gaultney authored Aug 31, 2020
2 parents 18807b2 + b125a13 commit 20d0242
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
### 1.3.3

- Allow any characters for attribute names in `add_variables_to_expression`.
* We have a lot of snake_cased attribute names. We should be able to use this function with those.

### 1.3.2

- Addressed theoretical weakness in expression attribute naming by
Expand Down
16 changes: 11 additions & 5 deletions tests/xoto3/dynamodb/utils/expressions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ def test_add_variables_to_expression():


def test_add_variables_to_expression_with_bad_attribute_name():
variables = {"thingy": "THINGY", "~deleted__At": "2020-02-02T00:00:00.000000Z"}
variables = {"thingy": "THINGY", "deleted__At": "2020-02-02T00:00:00.000000Z"}

query_dict = add_variables_to_expression(dict(), variables)
assert query_dict["ExpressionAttributeNames"] == {
"#thingy": "thingy",
"#deleted__At": "deleted__At",
}
assert query_dict["ExpressionAttributeValues"] == {
":thingy": "THINGY",
":deleted__At": "2020-02-02T00:00:00.000000Z",
}

with pytest.raises(
ValueError, match="Attribute name contains invalid characters: '~deleted__At'"
):
add_variables_to_expression(dict(), variables)


def test_add_variables_to_expression_with_duplicate_attribute_name():
Expand Down
2 changes: 1 addition & 1 deletion xoto3/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""xoto3"""
__version__ = "1.3.2"
__version__ = "1.3.3"
__author__ = "Peter Gaultney"
__author_email__ = "[email protected]"
6 changes: 0 additions & 6 deletions xoto3/dynamodb/utils/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@ def make_unique_expr_attr_key(attr_name: str) -> str:
return clean + "__xoto3__" + hashed.hexdigest()[:_HASH_LEN]


def validate_attr_key(attr_name: str):
if _filter_alphanum(attr_name) != attr_name:
raise ValueError(f"Attribute name contains invalid characters: '{attr_name}'")


def add_variables_to_expression(query_dict: dict, variables: dict) -> dict:
"""Attempt to make it easier to develop a query"""
ea_names = query_dict.get("ExpressionAttributeNames", {})
ea_values = query_dict.get("ExpressionAttributeValues", {})
for k, v in variables.items():
validate_attr_key(k)
name = f"#{k}"
if name in ea_names:
raise ValueError(
Expand Down

0 comments on commit 20d0242

Please sign in to comment.