Open
Description
- If I have nested fields such that
a.b
andb
are both legal expands,is_epxanded
will not distinguish betweenb
at the top level or nested is_expanded
will return true for all keys if~all
is in expanded, but~all
only expands top level expands
Proposed fix:
def is_expanded(expand, key):
"""Determine if the given key is expanded"""
expand_fields = []
# first split on commas to get each expand
for full in expand.split(","):
# than split on dots to get each component that is expanded
parts = full.split(".")
for i in range(len(parts)):
# add each prefix, as each prefix is epxanded, ie
# a.b.c will add a, a.b and a.b.c to the expand_fields list
# we do this to differentiate a.b from b
expand_fields.append(".".join(parts[: i + 1]))
# ~all only expands top level fields
if "." not in key and "~all" in expand_fields:
return True
return key in expand_fields
Test:
import pytest
from .utils import is_expanded
data = [
("a", "a", True),
("a", "b", False),
("a,b,c", "a", True),
("a,b,c", "b", True),
("a,b,c", "c", True),
("a,b,c", "d", False),
("a.b.c", "a", True),
("a.b.c", "a.b", True),
("a.b.c", "a.b.c", True),
("a.b.c", "b", False),
("a.b.c", "c", False),
("a.b.c", "d", False),
("a.b.c,d", "a", True),
("a.b.c,d", "d", True),
("~all", "a", True),
("~all", "a.b", False),
]
class TestIsExpanded:
@pytest.mark.parametrize("expand,key,ret", data)
def test_expanded(self, expand, key, ret):
assert is_expanded(expand, key) is ret
Metadata
Metadata
Assignees
Labels
No labels