Skip to content

Commit 26c949c

Browse files
committed
refactor: modernize Go patterns across codebase
- Replace sort.Slice with slices.SortFunc and cmp.Compare - Upgrade yaml.v2 to yaml.v3 - Replace gotest.tools with testify (already a dependency) - Use context.Background() instead of context.TODO() in tests - Use idiomatic errors.As with typed variables instead of new() - Use %w for proper error wrapping in rollback handling - Remove gotest.tools and yaml.v2 from go.mod
1 parent ca7d89e commit 26c949c

File tree

9 files changed

+21
-21
lines changed

9 files changed

+21
-21
lines changed

cli/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/raystack/compass/internal/config"
99
saltconfig "github.com/raystack/salt/config"
1010
"github.com/spf13/cobra"
11-
"gopkg.in/yaml.v2"
11+
"gopkg.in/yaml.v3"
1212
)
1313

1414
const configFlag = "config"

core/entity/search.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package entity
22

33
import (
4+
"cmp"
45
"context"
5-
"sort"
6+
"slices"
67

78
"github.com/raystack/compass/core/namespace"
89
)
@@ -155,7 +156,7 @@ func reciprocalRankFusion(lists ...[]SearchResult) []SearchResult {
155156
for _, s := range scores {
156157
all = append(all, *s)
157158
}
158-
sort.Slice(all, func(i, j int) bool { return all[i].score > all[j].score })
159+
slices.SortFunc(all, func(a, b scored) int { return cmp.Compare(b.score, a.score) })
159160

160161
results := make([]SearchResult, len(all))
161162
for i, s := range all {

core/namespace/service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestService_Create(t *testing.T) {
14-
ctx := context.TODO()
14+
ctx := context.Background()
1515
mockedNamespace := &namespace.Namespace{
1616
ID: uuid.New(),
1717
Name: "tenant-1",
@@ -78,7 +78,7 @@ func TestService_Create(t *testing.T) {
7878
}
7979

8080
func TestService_Update(t *testing.T) {
81-
ctx := context.TODO()
81+
ctx := context.Background()
8282
mockedNamespace := &namespace.Namespace{
8383
ID: uuid.New(),
8484
Name: "tenant-1",

core/user/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestValidateUser(t *testing.T) {
8383

8484
for _, tc := range testCases {
8585
t.Run(tc.Description, func(t *testing.T) {
86-
ctx := context.TODO()
86+
ctx := context.Background()
8787
mockUserRepo := new(mocks.UserRepository)
8888

8989
if tc.Setup != nil {

core/validator/validator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55

66
"github.com/raystack/compass/core/validator"
7-
"gotest.tools/assert"
7+
"github.com/stretchr/testify/assert"
88
)
99

1010
func TestValidateStruct(t *testing.T) {
@@ -15,7 +15,7 @@ func TestValidateStruct(t *testing.T) {
1515

1616
type TestCase struct {
1717
Description string
18-
Struct interface{}
18+
Struct any
1919
ErrString string
2020
}
2121

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ require (
3333
go.opentelemetry.io/otel/sdk/metric v1.42.0
3434
golang.org/x/net v0.51.0
3535
google.golang.org/protobuf v1.36.11
36-
gopkg.in/yaml.v2 v2.4.0
37-
gotest.tools v2.2.0+incompatible
3836
)
3937

4038
require (
@@ -90,7 +88,7 @@ require (
9088
golang.org/x/sys v0.41.0 // indirect
9189
golang.org/x/term v0.40.0 // indirect
9290
golang.org/x/text v0.34.0 // indirect
93-
gopkg.in/yaml.v3 v3.0.1 // indirect
91+
gopkg.in/yaml.v3 v3.0.1
9492
)
9593

9694
require (

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,6 @@ gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXa
374374
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
375375
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
376376
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
377-
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
378-
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
379377
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
380378
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
381379
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
382-
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
383-
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=

handler/user.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,16 @@ func (server *Handler) StarEntity(ctx context.Context, req *connect.Request[comp
7878

7979
id, err := server.starService.Stars(ctx, ns, userID, req.Msg.GetEntityId())
8080
if err != nil {
81-
if errors.Is(err, star.ErrEmptyUserID) || errors.Is(err, star.ErrEmptyEntityID) || errors.As(err, new(star.InvalidError)) {
81+
var invalidErr star.InvalidError
82+
var userNotFoundErr star.UserNotFoundError
83+
var duplicateErr star.DuplicateRecordError
84+
if errors.Is(err, star.ErrEmptyUserID) || errors.Is(err, star.ErrEmptyEntityID) || errors.As(err, &invalidErr) {
8285
return nil, connect.NewError(connect.CodeInvalidArgument, err)
8386
}
84-
if errors.As(err, new(star.UserNotFoundError)) {
87+
if errors.As(err, &userNotFoundErr) {
8588
return nil, connect.NewError(connect.CodeNotFound, err)
8689
}
87-
if errors.As(err, new(star.DuplicateRecordError)) {
90+
if errors.As(err, &duplicateErr) {
8891
return nil, connect.NewError(connect.CodeAlreadyExists, err)
8992
}
9093
return nil, internalServerError(ctx, "error starring entity", err)
@@ -101,10 +104,12 @@ func (server *Handler) UnstarEntity(ctx context.Context, req *connect.Request[co
101104
}
102105

103106
if err := server.starService.Unstars(ctx, userID, req.Msg.GetEntityId()); err != nil {
104-
if errors.Is(err, star.ErrEmptyUserID) || errors.Is(err, star.ErrEmptyEntityID) || errors.As(err, new(star.InvalidError)) {
107+
var invalidErr star.InvalidError
108+
var notFoundErr star.NotFoundError
109+
if errors.Is(err, star.ErrEmptyUserID) || errors.Is(err, star.ErrEmptyEntityID) || errors.As(err, &invalidErr) {
105110
return nil, connect.NewError(connect.CodeInvalidArgument, err)
106111
}
107-
if errors.As(err, new(star.NotFoundError)) {
112+
if errors.As(err, &notFoundErr) {
108113
return nil, connect.NewError(connect.CodeNotFound, err)
109114
}
110115
return nil, internalServerError(ctx, "error unstarring entity", err)

store/postgres/postgres.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (c *Client) RunWithinTx(ctx context.Context, f func(tx *sqlx.Tx) error) err
151151
}
152152
if err = f(tx); err != nil {
153153
if txErr := tx.Rollback(); txErr != nil {
154-
err = fmt.Errorf("rollback transaction error: %v (original error: %w)", txErr, err)
154+
err = fmt.Errorf("rollback transaction error: %w (original error: %w)", txErr, err)
155155
}
156156

157157
// reset context

0 commit comments

Comments
 (0)