Skip to content

Commit

Permalink
Handle lists in the response of INFO (#3278)
Browse files Browse the repository at this point in the history
Parse lists in the response of INFO, and even lines where list items are
mixed with key=value items, in which case the overall structure will be
a dict, and the items without value get `True` as their value.
  • Loading branch information
gerzse committed Jun 14, 2024
1 parent 9ae3d97 commit 893efc3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
11 changes: 9 additions & 2 deletions redis/_parsers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ def get_value(value):
return int(value)
except ValueError:
return value
elif "=" not in value:
return [get_value(v) for v in value.split(",") if v]
else:
sub_dict = {}
for item in value.split(","):
k, v = item.rsplit("=", 1)
sub_dict[k] = get_value(v)
if not item:
continue
if "=" in item:
k, v = item.rsplit("=", 1)
sub_dict[k] = get_value(v)
else:
sub_dict[item] = True
return sub_dict

for line in response.splitlines():
Expand Down
28 changes: 28 additions & 0 deletions tests/test_parsers/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,31 @@ def test_parse_info():
assert info["search_version"] == "99.99.99"
assert info["search_redis_version"] == "7.2.2 - oss"
assert info["search_query_timeout_ms"] == 500


def test_parse_info_list():
info_output = """
list_one:a,
list_two:a b,,c,10,1.1
"""
info = parse_info(info_output)

assert isinstance(info["list_one"], list)
assert info["list_one"] == ["a"]

assert isinstance(info["list_two"], list)
assert info["list_two"] == ["a b", "c", 10, 1.1]


def test_parse_info_list_dict_mixed():
info_output = """
list_one:a,b=1
list_two:a b=foo,,c,d=bar,e,
"""
info = parse_info(info_output)

assert isinstance(info["list_one"], dict)
assert info["list_one"] == {"a": True, "b": 1}

assert isinstance(info["list_two"], dict)
assert info["list_two"] == {"a b": "foo", "c": True, "d": "bar", "e": True}

0 comments on commit 893efc3

Please sign in to comment.