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 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: 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
Loading