Repository: googlemaps/google-maps-services-python

Issue:
Allow user to append several values for same component filter type
It would be nice to allow several values for same component type. 
Use case: You may want to filter sublocalities via locality component type ("locality matches against both locality and sublocality types") and in addition you may restrict result set by another locality.

Maybe we should extend converting components by defaultdict containing lists?



Discussion:

Patch:
diff --git a/googlemaps/convert.py b/googlemaps/convert.py
index 1c2264e..6206cfa 100644
--- a/googlemaps/convert.py
+++ b/googlemaps/convert.py
@@ -220,9 +220,17 @@ def components(arg):
 
     :rtype: basestring
     """
+
+    # Components may have multiple values per type, here we
+    # expand them into individual key/value items, eg:
+    # {"country": ["US", "AU"], "foo": 1} -> "country:AU", "country:US", "foo:1"
+    def expand(arg):
+        for k, v in arg.items():
+            for item in as_list(v):
+                yield "%s:%s" % (k, item)
+
     if isinstance(arg, dict):
-        arg = sorted(["%s:%s" % (k, arg[k]) for k in arg])
-        return "|".join(arg)
+        return "|".join(sorted(expand(arg)))
 
     raise TypeError(
         "Expected a dict for components, "


Test patch:
diff --git a/test/test_convert.py b/test/test_convert.py
index 851eda1..090a95f 100644
--- a/test/test_convert.py
+++ b/test/test_convert.py
@@ -91,6 +91,9 @@ class ConvertTest(unittest.TestCase):
         c = {"country": "US", "foo": 1}
         self.assertEqual("country:US|foo:1", convert.components(c))
 
+        c = {"country": ["US", "AU"], "foo": 1}
+        self.assertEqual("country:AU|country:US|foo:1", convert.components(c))
+
         with self.assertRaises(TypeError):
             convert.components("test")