Skip to content

Commit d2b36e7

Browse files
aurbina83gosom
andauthored
fix: initialize nil patterns map in extractPlaceID to prevent panic (#261)
* fix: initialize nil patterns map in extractPlaceID to prevent panic The patterns map was declared but never initialized before being written to in the sync.Once block, causing a nil map assignment panic at runtime. Added unit tests for extractPlaceID covering various URL formats. * fix: fix lint errors --------- Co-authored-by: Giorgos Komninos <admin@gkomninos.com>
1 parent 7a6a337 commit d2b36e7

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

gmaps/reviews.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func extractPlaceID(mapURL string) (string, error) {
207207
hexMatchPattern, // Hex format place ID
208208
}
209209

210+
patterns = make(map[string]*regexp.Regexp)
210211
for _, p := range avail {
211212
patterns[p] = regexp.MustCompile(p)
212213
}

gmaps/reviews_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//nolint:testpackage // we need to test unexported functions in the same package
2+
package gmaps
3+
4+
import (
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func Test_extractPlaceID(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
url string
14+
want string
15+
wantErr bool
16+
}{
17+
{
18+
name: "standard hex format with exclamation prefix",
19+
url: "https://www.google.com/maps/place/Joe's+Pizza+Broadway/@40.7546795,-73.9870291,17z/data=!4m7!3m6!1s0x89c259ab3c1ef289:0x3b67a41175949f55!8m2!3d40.7546795!4d-73.9870291!16s%2Fg%2F11bw4ws2mt?hl=en&entry=ttu",
20+
want: "0x89c259ab3c1ef289:0x3b67a41175949f55",
21+
wantErr: false,
22+
},
23+
{
24+
name: "place_id query parameter format",
25+
url: "https://www.google.com/maps/place/Joe's+Pizza/@40.7546795,-73.9870291,17z?place_id=ChIJDdnwdv0y5xQRRytw1ihZQeU&hl=en",
26+
want: "ChIJDdnwdv0y5xQRRytw1ihZQeU",
27+
wantErr: false,
28+
},
29+
{
30+
name: "full place URL with data and hex ID",
31+
url: "https://www.google.com/maps/place/Coffee+Project+New+York/data=!4m7!3m6!1s0x89c2599b5a24d7fd:0x9e354f6cf514b9fc!8m2!3d40.7270884!4d-73.989382!16s%2Fg%2F11c3svpqld!19sChIJ_dckWptZwokR_LkU9WxPNZ4",
32+
want: "0x89c2599b5a24d7fd:0x9e354f6cf514b9fc",
33+
wantErr: false,
34+
},
35+
{
36+
name: "maps search URL (no place ID)",
37+
url: "https://www.google.com/maps/search/pizza+in+Brooklyn+NY",
38+
want: "",
39+
wantErr: true,
40+
},
41+
{
42+
name: "empty URL",
43+
url: "",
44+
want: "",
45+
wantErr: true,
46+
},
47+
}
48+
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
got, err := extractPlaceID(tt.url)
52+
if (err != nil) != tt.wantErr {
53+
t.Errorf("extractPlaceID() error = %v, wantErr %v", err, tt.wantErr)
54+
return
55+
}
56+
57+
assert.Equal(t, tt.want, got, "extractPlaceID() = %v, want %v", got, tt.want)
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)