Skip to content

Commit

Permalink
Add support for tuples in get_attr (boto#3302)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRemis authored and hswong3i committed Nov 22, 2024
1 parent 39564c7 commit afe82e2
Show file tree
Hide file tree
Showing 2 changed files with 34 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,7 +178,8 @@ def get_attr(self, value, path):
if match is not None:
name, index = match.groups()
index = int(index)
value = value.get(name)
if name:
value = value.get(name)
if value is None or index >= len(value):
return None
return value[index]
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,33 @@ def test_aws_is_virtual_hostable_s3_bucket_allow_subdomains(
rule_lib.aws_is_virtual_hostable_s3_bucket(bucket, True)
== expected_value
)


@pytest.mark.parametrize(
"value, path, expected_value",
[
({"foo": ['bar']}, 'baz[0]', None), # Missing index
({"foo": ['bar']}, 'foo[1]', None), # Out of range index
({"foo": ['bar']}, 'foo[0]', "bar"), # Named index
(("foo",), '[0]', "foo"), # Bare index
({"foo": {}}, 'foo.bar[0]', None), # Missing index from split path
(
{"foo": {'bar': []}},
'foo.bar[0]',
None,
), # Out of range from split path
(
{"foo": {"bar": "baz"}},
'foo.bar',
"baz",
), # Split path with named index
(
{"foo": {"bar": ["baz"]}},
'foo.bar[0]',
"baz",
), # Split path with numeric index
],
)
def test_get_attr(rule_lib, value, path, expected_value):
result = rule_lib.get_attr(value, path)
assert result == expected_value

0 comments on commit afe82e2

Please sign in to comment.