Skip to content

Commit

Permalink
Add support for tuples in get_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRemis committed Nov 15, 2024
1 parent 7af79ea commit 9d473ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 4 additions & 3 deletions botocore/endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
logger = logging.getLogger(__name__)

TEMPLATE_STRING_RE = re.compile(r"\{[a-zA-Z#]+\}")
GET_ATTR_RE = re.compile(r"(\w+)\[(\d+)\]")
GET_ATTR_RE = re.compile(r"(\w*)\[(\d+)\]")
VALID_HOST_LABEL_RE = re.compile(
r"^(?!-)[a-zA-Z\d-]{1,63}(?<!-)$",
)
Expand Down Expand Up @@ -169,7 +169,7 @@ def get_attr(self, value, path):
names indicates the one to the right is nested. The index will always occur at
the end of the path.
:type value: dict or list
:type value: dict or tuple
:type path: str
:rtype: Any
"""
Expand All @@ -178,9 +178,10 @@ def get_attr(self, value, path):
if match is not None:
name, index = match.groups()
index = int(index)
value = value.get(name)
if value is None or index >= len(value):
return None
if name:
value = value.get(name)
return value[index]
else:
value = value[part]
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,11 @@ def test_aws_is_virtual_hostable_s3_bucket_allow_subdomains(
rule_lib.aws_is_virtual_hostable_s3_bucket(bucket, True)
== expected_value
)

def test_get_attr_can_get_dictionary_index(rule_lib):
result = rule_lib.get_attr({"foo": ['bar']}, 'foo[0]')
assert result == "bar"

def test_get_attr_can_get_list_index(rule_lib):
result = rule_lib.get_attr(("foo"), '[0]')
assert result == "foo"

0 comments on commit 9d473ac

Please sign in to comment.