From 630912560a70f5411f97b186e5eca4e7e223a6ee Mon Sep 17 00:00:00 2001 From: Robert Lucian Chiriac Date: Fri, 25 Oct 2024 12:40:28 +0300 Subject: [PATCH 1/5] Add key_value_pairs_to_dict utility fn --- localstack-core/localstack/utils/strings.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/localstack-core/localstack/utils/strings.py b/localstack-core/localstack/utils/strings.py index 65130a1b83e59..dc724152b8fa2 100644 --- a/localstack-core/localstack/utils/strings.py +++ b/localstack-core/localstack/utils/strings.py @@ -203,3 +203,19 @@ def prepend_with_slash(input: str) -> str: if not input.startswith("/"): return f"/{input}" return input + + +def key_value_pairs_to_dict(pairs: str, delimiter: str = ",", separator: str = "=") -> dict: + """ + Converts a string of key-value pairs to a dictionary. + + Args: + pairs (str): A string containing key-value pairs separated by a delimiter. + delimiter (str): The delimiter used to separate key-value pairs (default is comma ','). + separator (str): The separator between keys and values (default is '='). + + Returns: + dict: A dictionary containing the parsed key-value pairs. + """ + splits = [split_pair.partition(separator) for split_pair in pairs.split(delimiter)] + return {key: value for key, _, value in splits} From 93579ebe9fb04da22b36c3733efc9b40d652011b Mon Sep 17 00:00:00 2001 From: Robert Lucian Chiriac Date: Mon, 28 Oct 2024 20:49:59 +0200 Subject: [PATCH 2/5] Add test --- tests/unit/utils/test_strings.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit/utils/test_strings.py b/tests/unit/utils/test_strings.py index a0b3f87588e2e..af7d1d00d8972 100644 --- a/tests/unit/utils/test_strings.py +++ b/tests/unit/utils/test_strings.py @@ -1,7 +1,12 @@ -from localstack.utils.strings import prepend_with_slash +from localstack.utils.strings import prepend_with_slash, key_value_pairs_to_dict, short_uid, to_bytes def test_prepend_with_slash(): assert prepend_with_slash("hello") == "/hello" assert prepend_with_slash("/world") == "/world" assert prepend_with_slash("//world") == "//world" + +def key_value_pairs_to_dict(): + assert key_value_pairs_to_dict("a=1,b=2,c=3") == {"a": "1", "b": "2", "c": "3"} + assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator="=") == {"a": "1", "b": "2", "c": "3"} + assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator=":") == {'a=1': '', 'b=2': '', 'c=3': ''} From 2ca50d8047a73cfd78cc3280e6cca5bf48f697fc Mon Sep 17 00:00:00 2001 From: Robert Lucian Chiriac Date: Mon, 28 Oct 2024 21:11:14 +0200 Subject: [PATCH 3/5] Fix name of test fn --- tests/unit/utils/test_strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/utils/test_strings.py b/tests/unit/utils/test_strings.py index af7d1d00d8972..1024c9e513c79 100644 --- a/tests/unit/utils/test_strings.py +++ b/tests/unit/utils/test_strings.py @@ -6,7 +6,7 @@ def test_prepend_with_slash(): assert prepend_with_slash("/world") == "/world" assert prepend_with_slash("//world") == "//world" -def key_value_pairs_to_dict(): +def test_key_value_pairs_to_dict(): assert key_value_pairs_to_dict("a=1,b=2,c=3") == {"a": "1", "b": "2", "c": "3"} assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator="=") == {"a": "1", "b": "2", "c": "3"} assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator=":") == {'a=1': '', 'b=2': '', 'c=3': ''} From 263118581bda0f2a57825399a1c87d4ee83fcf4e Mon Sep 17 00:00:00 2001 From: Robert Lucian Chiriac Date: Thu, 31 Oct 2024 14:43:15 +0200 Subject: [PATCH 4/5] Format and lint --- tests/unit/utils/test_strings.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/unit/utils/test_strings.py b/tests/unit/utils/test_strings.py index 1024c9e513c79..8b550c161eb6c 100644 --- a/tests/unit/utils/test_strings.py +++ b/tests/unit/utils/test_strings.py @@ -1,4 +1,7 @@ -from localstack.utils.strings import prepend_with_slash, key_value_pairs_to_dict, short_uid, to_bytes +from localstack.utils.strings import ( + key_value_pairs_to_dict, + prepend_with_slash, +) def test_prepend_with_slash(): @@ -6,7 +9,16 @@ def test_prepend_with_slash(): assert prepend_with_slash("/world") == "/world" assert prepend_with_slash("//world") == "//world" + def test_key_value_pairs_to_dict(): assert key_value_pairs_to_dict("a=1,b=2,c=3") == {"a": "1", "b": "2", "c": "3"} - assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator="=") == {"a": "1", "b": "2", "c": "3"} - assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator=":") == {'a=1': '', 'b=2': '', 'c=3': ''} + assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator="=") == { + "a": "1", + "b": "2", + "c": "3", + } + assert key_value_pairs_to_dict("a=1;b=2;c=3", delimiter=";", separator=":") == { + "a=1": "", + "b=2": "", + "c=3": "", + } From f8a4cf4aa3f1a8b9a835c9d152ba834b4d66d5f1 Mon Sep 17 00:00:00 2001 From: Robert Lucian Chiriac Date: Thu, 31 Oct 2024 14:45:28 +0200 Subject: [PATCH 5/5] Address PR comment --- localstack-core/localstack/utils/strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/localstack-core/localstack/utils/strings.py b/localstack-core/localstack/utils/strings.py index dc724152b8fa2..b4598778277e2 100644 --- a/localstack-core/localstack/utils/strings.py +++ b/localstack-core/localstack/utils/strings.py @@ -218,4 +218,4 @@ def key_value_pairs_to_dict(pairs: str, delimiter: str = ",", separator: str = " dict: A dictionary containing the parsed key-value pairs. """ splits = [split_pair.partition(separator) for split_pair in pairs.split(delimiter)] - return {key: value for key, _, value in splits} + return {key.strip(): value.strip() for key, _, value in splits}