Skip to content

Commit 9544020

Browse files
Apply Go modernizations with 1.26 (#840)
1 parent 66f7d2e commit 9544020

File tree

12 files changed

+63
-70
lines changed

12 files changed

+63
-70
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/StevenWeathers/thunderdome-planning-poker
22

3-
go 1.24.0
3+
go 1.26.0
44

55
require (
66
github.com/XSAM/otelsql v0.37.0

internal/db/alert/alerts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type Service struct {
1818
}
1919

2020
// GetActiveAlerts gets a list of active global alerts
21-
func (d *Service) GetActiveAlerts(ctx context.Context) []interface{} {
22-
alerts := make([]interface{}, 0)
21+
func (d *Service) GetActiveAlerts(ctx context.Context) []any {
22+
alerts := make([]any, 0)
2323

2424
rows, err := d.DB.QueryContext(ctx,
2525
`SELECT id, name, type, content, active, allow_dismiss, registered_only FROM thunderdome.alert WHERE active IS TRUE;`,

internal/db/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ type gooseLogger struct {
3737
logger *otelzap.Logger
3838
}
3939

40-
func (l *gooseLogger) Fatalf(format string, v ...interface{}) {
40+
func (l *gooseLogger) Fatalf(format string, v ...any) {
4141
l.logger.Ctx(context.Background()).Fatal(fmt.Sprintf(format, v...))
4242
}
43-
func (l *gooseLogger) Printf(format string, v ...interface{}) {
43+
func (l *gooseLogger) Printf(format string, v ...any) {
4444
l.logger.Ctx(context.Background()).Info(fmt.Sprintf(format, v...))
4545
}
4646

internal/db/util.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io"
1313
"math/big"
1414
"regexp"
15+
"slices"
1516
"strings"
1617

1718
"golang.org/x/crypto/bcrypt"
@@ -28,13 +29,7 @@ func SanitizeEmail(email string) string {
2829

2930
// Contains checks if a string is present in a slice
3031
func Contains(s []string, str string) bool {
31-
for _, v := range s {
32-
if v == str {
33-
return true
34-
}
35-
}
36-
37-
return false
32+
return slices.Contains(s, str)
3833
}
3934

4035
// random generates a random secure byte of X length
@@ -46,7 +41,7 @@ func random(length int) ([]byte, error) {
4641
return nil, err // out of randomness, should never happen
4742
}
4843

49-
for i := 0; i < length; i++ {
44+
for i := range length {
5045
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
5146
if err != nil {
5247
return nil, err

internal/email/retro.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package email
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/StevenWeathers/thunderdome-planning-poker/thunderdome"
78
"github.com/matcornic/hermes/v2"
@@ -11,22 +12,22 @@ import (
1112
// SendRetroOverview sends the retro overview (items, action items) email to attendees
1213
func (s *Service) SendRetroOverview(retro *thunderdome.Retro, template *thunderdome.RetroTemplate, userName string, userEmail string) error {
1314
columnMap := make(map[string]string)
14-
var columnsList string
15+
var columnsList strings.Builder
1516
for _, column := range template.Format.Columns {
1617
columnMap[column.Name] = fmt.Sprintf(`
1718
## %s
1819
1920
`, column.Label)
2021
}
21-
var retroActionsList string
22+
var retroActionsList strings.Builder
2223
for _, action := range retro.ActionItems {
23-
retroActionsList += formatRetroActionWithAssignee(action)
24+
retroActionsList.WriteString(formatRetroActionWithAssignee(action))
2425
}
2526
for _, item := range retro.Items {
2627
columnMap[item.Type] += formatRetroItemForMarkdownList(item.Content)
2728
}
2829
for _, column := range template.Format.Columns {
29-
columnsList += fmt.Sprintf(`
30+
fmt.Fprintf(&columnsList, `
3031
%s
3132
3233
`, columnMap[column.Name])
@@ -41,8 +42,8 @@ func (s *Service) SendRetroOverview(retro *thunderdome.Retro, template *thunderd
4142
},
4243
FreeMarkdown: `
4344
## Action Items
44-
` + hermes.Markdown(retroActionsList) + `
45-
` + hermes.Markdown(columnsList) + `
45+
` + hermes.Markdown(retroActionsList.String()) + `
46+
` + hermes.Markdown(columnsList.String()) + `
4647
4748
`,
4849
},

internal/fracindex/fracindex_test.go

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,34 @@ func TestKeyBetween(t *testing.T) {
1313
exp *string
1414
err error
1515
}{
16-
{nil, nil, strPtr("a "), nil},
17-
{nil, strPtr("a "), strPtr("Z~"), nil},
18-
{nil, strPtr("Z~"), strPtr("Z}"), nil},
19-
{strPtr("a "), nil, strPtr("a!"), nil},
20-
{strPtr("a!"), nil, strPtr("a\""), nil},
21-
{strPtr("a0"), strPtr("a1"), strPtr("a0P"), nil},
22-
{strPtr("a1"), strPtr("a2"), strPtr("a1P"), nil},
23-
{strPtr("a0V"), strPtr("a1"), strPtr("a0k"), nil},
24-
{strPtr("Z~"), strPtr("a "), strPtr("Z~P"), nil},
25-
{strPtr("Z~"), strPtr("a!"), strPtr("a "), nil},
26-
{nil, strPtr("Y "), strPtr("X~~~"), nil},
27-
{strPtr("b~~"), nil, strPtr("c "), nil},
28-
{strPtr("a0"), strPtr("a0V"), strPtr("a0;"), nil},
29-
{strPtr("a0"), strPtr("a0G"), strPtr("a04"), nil},
30-
{strPtr("b125"), strPtr("b129"), strPtr("b127"), nil},
31-
{strPtr("a0"), strPtr("a1V"), strPtr("a1"), nil},
32-
{strPtr("Z~"), strPtr("a 1"), strPtr("a "), nil},
33-
{nil, strPtr("a0V"), strPtr("a0"), nil},
34-
{nil, strPtr("b999"), strPtr("b99"), nil},
35-
{nil, strPtr("A "), nil, errors.New("Key is too small")},
16+
{nil, nil, new("a "), nil},
17+
{nil, new("a "), new("Z~"), nil},
18+
{nil, new("Z~"), new("Z}"), nil},
19+
{new("a "), nil, new("a!"), nil},
20+
{new("a!"), nil, new("a\""), nil},
21+
{new("a0"), new("a1"), new("a0P"), nil},
22+
{new("a1"), new("a2"), new("a1P"), nil},
23+
{new("a0V"), new("a1"), new("a0k"), nil},
24+
{new("Z~"), new("a "), new("Z~P"), nil},
25+
{new("Z~"), new("a!"), new("a "), nil},
26+
{nil, new("Y "), new("X~~~"), nil},
27+
{new("b~~"), nil, new("c "), nil},
28+
{new("a0"), new("a0V"), new("a0;"), nil},
29+
{new("a0"), new("a0G"), new("a04"), nil},
30+
{new("b125"), new("b129"), new("b127"), nil},
31+
{new("a0"), new("a1V"), new("a1"), nil},
32+
{new("Z~"), new("a 1"), new("a "), nil},
33+
{nil, new("a0V"), new("a0"), nil},
34+
{nil, new("b999"), new("b99"), nil},
35+
{nil, new("A "), nil, errors.New("Key is too small")},
3636
// @TODO - fix the implementation to handle this case
3737
//{nil, strPtr("A !"), strPtr("A P"), nil},
38-
{strPtr("zzzzzzzzzzzzzzzzzzzzzzzzzzy"), nil, strPtr("zzzzzzzzzzzzzzzzzzzzzzzzzzz"), nil},
39-
{strPtr("z~~~~~~~~~~~~~~~~~~~~~~~~~~"), nil, strPtr("z~~~~~~~~~~~~~~~~~~~~~~~~~~P"), nil},
40-
{strPtr("a0 "), nil, nil, errors.New("Fractional part should not end with ' ' (space)")},
41-
{strPtr("a0 "), strPtr("a1"), nil, errors.New("Fractional part should not end with ' ' (space)")},
42-
{strPtr("0"), strPtr("1"), nil, errors.New("head is out of range")},
43-
{strPtr("a1"), strPtr("a0"), nil, errors.New("key_between - a must be before b")},
38+
{new("zzzzzzzzzzzzzzzzzzzzzzzzzzy"), nil, new("zzzzzzzzzzzzzzzzzzzzzzzzzzz"), nil},
39+
{new("z~~~~~~~~~~~~~~~~~~~~~~~~~~"), nil, new("z~~~~~~~~~~~~~~~~~~~~~~~~~~P"), nil},
40+
{new("a0 "), nil, nil, errors.New("Fractional part should not end with ' ' (space)")},
41+
{new("a0 "), new("a1"), nil, errors.New("Fractional part should not end with ' ' (space)")},
42+
{new("0"), new("1"), nil, errors.New("head is out of range")},
43+
{new("a1"), new("a0"), nil, errors.New("key_between - a must be before b")},
4444
}
4545

4646
for _, tt := range tests {
@@ -66,7 +66,7 @@ func TestGenerateInsertOrder(t *testing.T) {
6666

6767
var prev *string
6868
var indices []string
69-
for i := 0; i < 5; i++ {
69+
for range 5 {
7070
prev, _ = KeyBetween(prev, nil)
7171
indices = append(indices, *prev)
7272
}
@@ -90,9 +90,9 @@ func TestGenerateInsertOrder(t *testing.T) {
9090

9191
var fractIndex *string
9292
if toIndex == 0 {
93-
fractIndex, _ = KeyBetween(nil, strPtr(indices[toIndex]))
93+
fractIndex, _ = KeyBetween(nil, new(indices[toIndex]))
9494
} else {
95-
fractIndex, _ = KeyBetween(strPtr(indices[toIndex-1]), strPtr(indices[toIndex]))
95+
fractIndex, _ = KeyBetween(new(indices[toIndex-1]), new(indices[toIndex]))
9696
}
9797

9898
indices = append(indices[:toIndex], append([]string{*fractIndex}, indices[toIndex:]...)...)
@@ -119,7 +119,3 @@ func vecCompare(va, vb []string) bool {
119119
}
120120
return true
121121
}
122-
123-
func strPtr(s string) *string {
124-
return &s
125-
}

internal/http/alert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"go.uber.org/zap"
99
)
1010

11-
var ActiveAlerts []interface{}
11+
var ActiveAlerts []any
1212

1313
type alertRequestBody struct {
1414
Name string `json:"name" validate:"required"`

internal/http/jira.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ func (s *Service) logErrorWithJSONResponse(err error, errorTitle string, w http.
350350
result := &standardJsonResponse{
351351
Success: false,
352352
Error: err.Error(),
353-
Data: map[string]interface{}{},
354-
Meta: map[string]interface{}{},
353+
Data: map[string]any{},
354+
Meta: map[string]any{},
355355
}
356356

357357
response, _ := json.Marshal(result)

internal/http/middleware_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
"go.uber.org/zap"
2121
)
2222

23+
//go:fix inline
2324
func ptr[T any](v T) *T {
24-
return &v
25+
return new(v)
2526
}
2627

2728
// MockTeamDataSvc is a mock implementation of the TeamDataSvc
@@ -2283,7 +2284,7 @@ func TestAdminOnly(t *testing.T) {
22832284

22842285
// If the status is Forbidden, check for the correct error message
22852286
if tt.expectedStatus == http.StatusForbidden {
2286-
var responseBody map[string]interface{}
2287+
var responseBody map[string]any
22872288
err = json.NewDecoder(rr.Body).Decode(&responseBody)
22882289
assert.NoError(t, err)
22892290
assert.Equal(t, "REQUIRES_ADMIN", responseBody["error"])
@@ -2352,7 +2353,7 @@ func TestRegisteredUserOnly(t *testing.T) {
23522353

23532354
// If the status is Forbidden, check for the correct error message
23542355
if tt.expectedStatus == http.StatusForbidden {
2355-
var responseBody map[string]interface{}
2356+
var responseBody map[string]any
23562357
err = json.NewDecoder(rr.Body).Decode(&responseBody)
23572358
assert.NoError(t, err)
23582359
assert.Equal(t, "REGISTERED_USER_ONLY", responseBody["error"])
@@ -2435,7 +2436,7 @@ func TestEntityUserOnly(t *testing.T) {
24352436

24362437
// If the status is not OK, check for the correct error message
24372438
if tt.expectedStatus != http.StatusOK {
2438-
var responseBody map[string]interface{}
2439+
var responseBody map[string]any
24392440
err = json.NewDecoder(rr.Body).Decode(&responseBody)
24402441
assert.NoError(t, err)
24412442

internal/http/types.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ type Service struct {
133133

134134
// standardJsonResponse structure used for all restful APIs response body
135135
type standardJsonResponse struct {
136-
Success bool `json:"success"`
137-
Error string `json:"error"`
138-
Data interface{} `json:"data" swaggertype:"object"`
139-
Meta interface{} `json:"meta" swaggertype:"object"`
136+
Success bool `json:"success"`
137+
Error string `json:"error"`
138+
Data any `json:"data" swaggertype:"object"`
139+
Meta any `json:"meta" swaggertype:"object"`
140140
}
141141

142142
// pagination meta structure for query result pagination
@@ -173,7 +173,7 @@ type AdminDataSvc interface {
173173
}
174174

175175
type AlertDataSvc interface {
176-
GetActiveAlerts(ctx context.Context) []interface{}
176+
GetActiveAlerts(ctx context.Context) []any
177177
AlertsList(ctx context.Context, limit int, offset int) ([]*thunderdome.Alert, int, error)
178178
AlertsCreate(ctx context.Context, name string, alertType string, content string, active bool, allowDismiss bool, registeredOnly bool) error
179179
AlertsUpdate(ctx context.Context, alertID string, name string, alertType string, content string, active bool, allowDismiss bool, registeredOnly bool) error

0 commit comments

Comments
 (0)