Skip to content

Commit 7aa42e8

Browse files
committed
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.
1 parent e9a6507 commit 7aa42e8

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

gmaps/reviews.go

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

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

gmaps/reviews_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package gmaps
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func Test_extractPlaceID(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
url string
11+
want string
12+
wantErr bool
13+
}{
14+
{
15+
name: "standard hex format with exclamation prefix",
16+
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",
17+
want: "0x89c259ab3c1ef289:0x3b67a41175949f55",
18+
wantErr: false,
19+
},
20+
{
21+
name: "place_id query parameter format",
22+
url: "https://www.google.com/maps/place/Joe's+Pizza/@40.7546795,-73.9870291,17z?place_id=ChIJDdnwdv0y5xQRRytw1ihZQeU&hl=en",
23+
want: "ChIJDdnwdv0y5xQRRytw1ihZQeU",
24+
wantErr: false,
25+
},
26+
{
27+
name: "full place URL with data and hex ID",
28+
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",
29+
want: "0x89c2599b5a24d7fd:0x9e354f6cf514b9fc",
30+
wantErr: false,
31+
},
32+
{
33+
name: "maps search URL (no place ID)",
34+
url: "https://www.google.com/maps/search/pizza+in+Brooklyn+NY",
35+
want: "",
36+
wantErr: true,
37+
},
38+
{
39+
name: "empty URL",
40+
url: "",
41+
want: "",
42+
wantErr: true,
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
got, err := extractPlaceID(tt.url)
49+
if (err != nil) != tt.wantErr {
50+
t.Errorf("extractPlaceID() error = %v, wantErr %v", err, tt.wantErr)
51+
return
52+
}
53+
if got != tt.want {
54+
t.Errorf("extractPlaceID() = %v, want %v", got, tt.want)
55+
}
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)