diff --git a/places.go b/places.go index 09e65f0..e682101 100644 --- a/places.go +++ b/places.go @@ -757,10 +757,11 @@ func (r *PlaceAutocompleteRequest) params() url.Values { var cf []string for c, f := range r.Components { + fc := make([]string, len(f)) for i, v := range f { - f[i] = string(c) + ":" + v + fc[i] = string(c) + ":" + v } - cf = append(cf, strings.Join(f, "|")) + cf = append(cf, strings.Join(fc, "|")) } if len(cf) > 0 { q.Set("components", strings.Join(cf, "|")) diff --git a/places_test.go b/places_test.go index f64f356..54e168e 100644 --- a/places_test.go +++ b/places_test.go @@ -17,6 +17,7 @@ package maps import ( "context" "encoding/json" + "reflect" "strings" "testing" "time" @@ -499,6 +500,51 @@ func TestPlaceAutocompleteMaximalRequestURL(t *testing.T) { } } +func TestPlaceAutocompleteWithMutipleComponentsWillWithoutMutation(t *testing.T) { + expectedQuery := "components=country%3AES%7Ccountry%3AAU%7Ccountry%3AHK&input=quay+resteraunt+sydney&key=AIzaNotReallyAnAPIKey&language=es&location=1%2C2&offset=5&radius=10000&types=geocode" + + server := mockServerForQuery(expectedQuery, 200, `{"status":"OK"}"`) + defer server.s.Close() + + c, _ := NewClient(WithAPIKey(apiKey), WithBaseURL(server.s.URL)) + + placeType, err := ParseAutocompletePlaceType("geocode") + if err != nil { + t.Errorf("Unexpected error in parsing place type: %v", err) + } + + r := &PlaceAutocompleteRequest{ + Input: "quay resteraunt sydney", + Offset: 5, + Location: &LatLng{1.0, 2.0}, + Radius: 10000, + Language: "es", + Types: placeType, + Components: map[Component][]string{ + ComponentCountry: {"ES", "AU", "HK"}, + }, + } + + var copiedComponents = map[Component][]string{ + ComponentCountry: {"ES", "AU", "HK"}, + } + + // copy the request + var originalReq = *r + originalReq.Components = copiedComponents + + _, err = c.PlaceAutocomplete(context.Background(), r) + + if eq := reflect.DeepEqual(*r, originalReq); !eq { + t.Errorf("Unexpected mutation at PlaceAutocompleteRequest") + } + if err != nil { + t.Errorf("Unexpected error in constructing request URL: %+v", err) + } else if server.successful != 1 { + t.Errorf("Got URL(s) %v, want %s", server.failed, expectedQuery) + } +} + func TestPlaceAutocompleteMissingInput(t *testing.T) { c, _ := NewClient(WithAPIKey(apiKey)) r := &PlaceAutocompleteRequest{}