Skip to content

Commit 893efc3

Browse files
authored
Handle lists in the response of INFO (#3278)
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.
1 parent 9ae3d97 commit 893efc3

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

redis/_parsers/helpers.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,18 @@ def get_value(value):
4646
return int(value)
4747
except ValueError:
4848
return value
49+
elif "=" not in value:
50+
return [get_value(v) for v in value.split(",") if v]
4951
else:
5052
sub_dict = {}
5153
for item in value.split(","):
52-
k, v = item.rsplit("=", 1)
53-
sub_dict[k] = get_value(v)
54+
if not item:
55+
continue
56+
if "=" in item:
57+
k, v = item.rsplit("=", 1)
58+
sub_dict[k] = get_value(v)
59+
else:
60+
sub_dict[item] = True
5461
return sub_dict
5562

5663
for line in response.splitlines():

tests/test_parsers/test_helpers.py

+28
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,31 @@ def test_parse_info():
3333
assert info["search_version"] == "99.99.99"
3434
assert info["search_redis_version"] == "7.2.2 - oss"
3535
assert info["search_query_timeout_ms"] == 500
36+
37+
38+
def test_parse_info_list():
39+
info_output = """
40+
list_one:a,
41+
list_two:a b,,c,10,1.1
42+
"""
43+
info = parse_info(info_output)
44+
45+
assert isinstance(info["list_one"], list)
46+
assert info["list_one"] == ["a"]
47+
48+
assert isinstance(info["list_two"], list)
49+
assert info["list_two"] == ["a b", "c", 10, 1.1]
50+
51+
52+
def test_parse_info_list_dict_mixed():
53+
info_output = """
54+
list_one:a,b=1
55+
list_two:a b=foo,,c,d=bar,e,
56+
"""
57+
info = parse_info(info_output)
58+
59+
assert isinstance(info["list_one"], dict)
60+
assert info["list_one"] == {"a": True, "b": 1}
61+
62+
assert isinstance(info["list_two"], dict)
63+
assert info["list_two"] == {"a b": "foo", "c": True, "d": "bar", "e": True}

0 commit comments

Comments
 (0)