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
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user