Skip to content

Commit

Permalink
chore: fix golint casting lints
Browse files Browse the repository at this point in the history
  • Loading branch information
avallete committed Oct 14, 2024
1 parent bf03ef7 commit cfada5b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion internal/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/afero"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/internal/utils/cast"
"github.com/supabase/cli/internal/utils/credentials"
"github.com/supabase/cli/internal/utils/diff"
"github.com/supabase/cli/internal/utils/flags"
Expand Down Expand Up @@ -136,7 +137,7 @@ func linkPostgrestVersion(ctx context.Context, api tenant.TenantAPI, fsys afero.
}

func updateApiConfig(config api.PostgrestConfigWithJWTSecretResponse) {
utils.Config.Api.MaxRows = uint(config.MaxRows)
utils.Config.Api.MaxRows = cast.IntToUint(config.MaxRows)
utils.Config.Api.ExtraSearchPath = readCsv(config.DbExtraSearchPath)
utils.Config.Api.Schemas = readCsv(config.DbSchema)
}
Expand Down
21 changes: 21 additions & 0 deletions internal/utils/cast/cast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cast

import "math"

// UintToInt converts a uint to an *int, handling potential overflow
func UintToInt(value uint) int {
if value <= math.MaxInt {
result := int(value)
return result
}
maxInt := math.MaxInt
return maxInt
}

// IntToUint converts an int to a uint, handling negative values
func IntToUint(value int) uint {
if value < 0 {
return 0
}
return uint(value)
}
7 changes: 4 additions & 3 deletions pkg/config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"strings"

"github.com/supabase/cli/internal/utils/cast"
"github.com/supabase/cli/internal/utils/diff"
v1API "github.com/supabase/cli/pkg/api"
)
Expand Down Expand Up @@ -53,8 +54,8 @@ func (a *RemoteApi) ToUpdatePostgrestConfigBody() v1API.UpdatePostgrestConfigBod

// Convert MaxRows to int pointer
if a.MaxRows > 0 {
maxRows := int(a.MaxRows)
body.MaxRows = &maxRows
intValue := cast.UintToInt(a.MaxRows)
body.MaxRows = &intValue
}

// Note: DbPool is not present in the Api struct, so it's not set here
Expand Down Expand Up @@ -84,7 +85,7 @@ func (a *RemoteApi) fromRemoteApiConfig(remoteConfig v1API.PostgrestConfigWithJW
}

// Update MaxRows if present in remoteConfig
result.MaxRows = uint(remoteConfig.MaxRows)
result.MaxRows = cast.IntToUint(remoteConfig.MaxRows)

return result
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/config/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestApiToUpdatePostgrestConfigBody(t *testing.T) {
t.Run("converts all fields correctly", func(t *testing.T) {
api := &RemoteApi{
Enabled: true,
Enabled: true,
Schemas: []string{"public", "private"},
ExtraSearchPath: []string{"extensions", "public"},
MaxRows: 1000,
Expand All @@ -37,7 +37,7 @@ func TestApiToUpdatePostgrestConfigBody(t *testing.T) {
func TestApiDiffWithRemote(t *testing.T) {
t.Run("detects differences", func(t *testing.T) {
api := &RemoteApi{
Enabled: true,
Enabled: true,
Schemas: []string{"public", "private"},
ExtraSearchPath: []string{"extensions", "public"},
MaxRows: 1000,
Expand All @@ -61,7 +61,7 @@ func TestApiDiffWithRemote(t *testing.T) {

t.Run("handles no differences", func(t *testing.T) {
api := &RemoteApi{
Enabled: true,
Enabled: true,
Schemas: []string{"public"},
ExtraSearchPath: []string{"public"},
MaxRows: 500,
Expand All @@ -79,7 +79,7 @@ func TestApiDiffWithRemote(t *testing.T) {
})
t.Run("handles multiple schemas and search paths with spaces", func(t *testing.T) {
api := &RemoteApi{
Enabled: true,
Enabled: true,
Schemas: []string{"public", "private"},
ExtraSearchPath: []string{"extensions", "public"},
MaxRows: 500,
Expand All @@ -97,7 +97,7 @@ func TestApiDiffWithRemote(t *testing.T) {
})
t.Run("handles api disabled on remote side", func(t *testing.T) {
api := &RemoteApi{
Enabled: true,
Enabled: true,
Schemas: []string{"public", "private"},
ExtraSearchPath: []string{"extensions", "public"},
MaxRows: 500,
Expand Down
3 changes: 2 additions & 1 deletion pkg/parser/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/go-errors/errors"
"github.com/spf13/viper"
"github.com/supabase/cli/internal/utils/cast"
)

// Equal to `startBufSize` from `bufio/scan.go`
Expand Down Expand Up @@ -83,7 +84,7 @@ func Split(sql io.Reader, transform ...func(string) string) (stats []string, err

// Increase scanner capacity to support very long lines containing e.g. geodata
buf := make([]byte, startBufSize)
maxbuf := int(viper.GetSizeInBytes("SCANNER_BUFFER_SIZE"))
maxbuf := cast.UintToInt(viper.GetSizeInBytes("SCANNER_BUFFER_SIZE"))
if maxbuf == 0 {
maxbuf = MaxScannerCapacity
}
Expand Down

0 comments on commit cfada5b

Please sign in to comment.