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

Add support for tuples in get_attr #3302

Merged
merged 8 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 7 additions & 4 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,17 +169,20 @@ 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
"""
for part in path.split("."):
if value is None:
return None
match = GET_ATTR_RE.search(part)
if match is not None:
name, index = match.groups()
index = int(index)
value = value.get(name)
if value is None or index >= len(value):
if name:
value = value.get(name)
if index >= len(value):
return None
return value[index]
SamRemis marked this conversation as resolved.
Show resolved Hide resolved
else:
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,13 @@ 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"
Loading