Skip to content

Commit d274cc6

Browse files
authored
chore: Memory efficient string-join (#16)
Use idiomatic golang string-join. More memory efficient.
1 parent aaf1936 commit d274cc6

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

internal/oauth/oauth.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,7 @@ func (m *Manager) tokenPath(email string) string {
334334

335335
// scopesToString joins scopes with spaces.
336336
func scopesToString(scopes []string) string {
337-
result := ""
338-
for i, s := range scopes {
339-
if i > 0 {
340-
result += " "
341-
}
342-
result += s
343-
}
344-
return result
337+
return strings.Join(scopes, " ")
345338
}
346339

347340
// openBrowser opens the default browser to the given URL.

internal/oauth/oauth_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package oauth
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestScopesToString(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
scopes []string
11+
want string
12+
}{
13+
{
14+
name: "empty scopes",
15+
scopes: []string{},
16+
want: "",
17+
},
18+
{
19+
name: "single scope",
20+
scopes: []string{"https://www.googleapis.com/auth/gmail.readonly"},
21+
want: "https://www.googleapis.com/auth/gmail.readonly",
22+
},
23+
{
24+
name: "multiple scopes",
25+
scopes: []string{"https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/gmail.modify"},
26+
want: "https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/gmail.modify",
27+
},
28+
{
29+
name: "three scopes",
30+
scopes: []string{"scope1", "scope2", "scope3"},
31+
want: "scope1 scope2 scope3",
32+
},
33+
}
34+
35+
for _, tt := range tests {
36+
t.Run(tt.name, func(t *testing.T) {
37+
got := scopesToString(tt.scopes)
38+
if got != tt.want {
39+
t.Errorf("scopesToString() = %q, want %q", got, tt.want)
40+
}
41+
})
42+
}
43+
}

0 commit comments

Comments
 (0)