From ae50ca26574c37a69d3c24675e9a2a2a226a014a Mon Sep 17 00:00:00 2001 From: kgriffs Date: Fri, 6 Sep 2013 17:05:44 -0500 Subject: [PATCH] feat(util.to_query_str): Smart handling of lists This patch improves to_query_str so it can recognize instances of list and convert it to a comma-delimited string value. Closes #152 --- falcon/tests/test_utils.py | 12 ++++++++++++ falcon/util.py | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/falcon/tests/test_utils.py b/falcon/tests/test_utils.py index 3da461f..1387b17 100644 --- a/falcon/tests/test_utils.py +++ b/falcon/tests/test_utils.py @@ -34,6 +34,18 @@ class TestFalconUtils(testtools.TestCase): falcon.to_query_str({'limit': 10}), '?limit=10') + self.assertEquals( + falcon.to_query_str({'things': [1, 2, 3]}), + '?things=1,2,3') + + self.assertEquals( + falcon.to_query_str({'things': ['a']}), + '?things=a') + + self.assertEquals( + falcon.to_query_str({'things': ['a', 'b']}), + '?things=a,b') + def test_pack_query_params_several(self): garbage_in = { 'limit': 17, diff --git a/falcon/util.py b/falcon/util.py index cd5b992..c1c73a8 100644 --- a/falcon/util.py +++ b/falcon/util.py @@ -63,7 +63,8 @@ def to_query_str(params): Args: params: dict of simple key-value types, where key is a string and value is a string or something that can be converted into a - string. + string. If value is a list, it will be converted to a comma- + delimited string (e.g., thing=1,2,3) Returns: A URI query string starting with '?', or and empty string if there @@ -81,6 +82,8 @@ def to_query_str(params): v = 'true' elif v is False: v = 'false' + elif isinstance(v, list): + v = ','.join([str(i) for i in v]) else: v = str(v)