From 893efc30b6f1d321e38d058657b757b1ec17aa99 Mon Sep 17 00:00:00 2001 From: Gabriel Erzse Date: Fri, 14 Jun 2024 11:19:04 +0300 Subject: [PATCH] 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. --- redis/_parsers/helpers.py | 11 +++++++++-- tests/test_parsers/test_helpers.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/redis/_parsers/helpers.py b/redis/_parsers/helpers.py index 57b12ab89e..56d912c157 100644 --- a/redis/_parsers/helpers.py +++ b/redis/_parsers/helpers.py @@ -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(): diff --git a/tests/test_parsers/test_helpers.py b/tests/test_parsers/test_helpers.py index 6430a237f6..383b9de794 100644 --- a/tests/test_parsers/test_helpers.py +++ b/tests/test_parsers/test_helpers.py @@ -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}