Skip to content

Commit

Permalink
staticcheck: Fix additional linter issues after upgrading to Go 1.24.…
Browse files Browse the repository at this point in the history
…1 and golangci-lint v1.64.6

Also addresses nits
  • Loading branch information
thrasher- committed Mar 5, 2025
1 parent 46aafb6 commit 070e09e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.64.5
version: v1.64.6
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
LDFLAGS = -ldflags "-w -s"
GCTPKG = github.com/thrasher-corp/gocryptotrader
LINTPKG = github.com/golangci/golangci-lint/cmd/[email protected].5
LINTPKG = github.com/golangci/golangci-lint/cmd/[email protected].6
LINTBIN = $(GOPATH)/bin/golangci-lint
GCTLISTENPORT=9050
GCTPROFILERLISTENPORT=8085
Expand Down
34 changes: 12 additions & 22 deletions backtester/eventhandlers/strategies/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,29 @@ func LoadStrategyByName(name string, useSimultaneousProcessing bool) (Handler, e

func createNewStrategy(name string, useSimultaneousProcessing bool, h Handler) (Handler, error) {
if h == nil {
return nil, fmt.Errorf("cannot load %v supported strategies contains %w", name, common.ErrNilPointer)
return nil, fmt.Errorf("cannot load strategy %q: %w", name, common.ErrNilPointer)
}

if !strings.EqualFold(name, h.Name()) {
return nil, nil
}
// create new instance so strategy is not shared across all tasks

strategyValue := reflect.ValueOf(h)
if strategyValue.IsNil() {
return nil, fmt.Errorf("cannot load %v supported strategies element is a %w", name, common.ErrNilPointer)
}
strategyElement := strategyValue.Elem()
if !strategyElement.IsValid() {
return nil, fmt.Errorf("cannot load %v strategy element is invalid %w", name, common.ErrTypeAssertFailure)
}
strategyType := strategyElement.Type()
if strategyType == nil {
return nil, fmt.Errorf("cannot load %v strategy type is a %w", name, common.ErrNilPointer)
}
newStrategy := reflect.New(strategyType)
if newStrategy.IsNil() {
return nil, fmt.Errorf("cannot load %v new instance of strategy is a %w", name, common.ErrNilPointer)
if strategyValue.Kind() != reflect.Ptr || strategyValue.IsNil() {
return nil, fmt.Errorf("cannot load strategy %q: handler must be a non-nil pointer, got %T", name, h)
}
strategyInterface := newStrategy.Interface()
if strategyInterface == nil {
return nil, fmt.Errorf("cannot load %v new instance of strategy is not an interface. %w", name, common.ErrTypeAssertFailure)
}
strategy, ok := strategyInterface.(Handler)

// create new instance so strategy is not shared across all tasks
strategy, ok := reflect.New(strategyValue.Elem().Type()).Interface().(Handler)
if !ok {
return nil, fmt.Errorf("cannot load %v new instance of strategy is not a Handler interface. %w", name, common.ErrTypeAssertFailure)
return nil, fmt.Errorf("cannot load strategy %q: type %T doesn't implement Handler interface: %w",
name, strategy, common.ErrTypeAssertFailure)
}

if useSimultaneousProcessing && !strategy.SupportsSimultaneousProcessing() {
return nil, base.ErrSimultaneousProcessingNotSupported
}

strategy.SetSimultaneousProcessing(useSimultaneousProcessing)
return strategy, nil
}
Expand Down
42 changes: 21 additions & 21 deletions backtester/eventhandlers/strategies/strategies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/thrasher-corp/gocryptotrader/backtester/data"
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio"
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/base"
Expand Down Expand Up @@ -83,34 +84,33 @@ func TestCreateNewStrategy(t *testing.T) {

// invalid Handler
resp, err := createNewStrategy(dollarcostaverage.Name, false, nil)
if !errors.Is(err, common.ErrNilPointer) {
t.Errorf("received '%v' expected '%v'", err, common.ErrNilPointer)
}
if resp != nil {
t.Errorf("received '%v' expected '%v'", resp, nil)
}
assert.ErrorIs(t, err, common.ErrNilPointer)
assert.Nil(t, resp)

// mismatched name
resp, err = createNewStrategy(dollarcostaverage.Name, false, &customStrategy{})
if !errors.Is(err, nil) {
t.Errorf("received '%v' expected '%v'", err, nil)
}
if resp != nil {
t.Errorf("received '%v' expected '%v'", resp, nil)
}
assert.NoError(t, err, "createNewStrategy should not error")
assert.Nil(t, resp)

// nil Handler
var h Handler = (*customStrategy)(nil)
_, err = createNewStrategy("custom-strategy", false, h)
assert.ErrorContains(t, err, "must be a non-nil pointer")

// valid
h := new(dollarcostaverage.Strategy)
resp, err = createNewStrategy(dollarcostaverage.Name, false, h)
if !errors.Is(err, nil) {
t.Errorf("received '%v' expected '%v'", err, nil)
}
if resp == nil {
t.Errorf("received '%v' expected '%v'", resp, h)
}
h = new(dollarcostaverage.Strategy)
resp, err = createNewStrategy(dollarcostaverage.Name, true, h)
assert.NoError(t, err, "createNewStrategy should not error")
assert.NotNil(t, resp)

// simultaneous processing desired but not supported
h = &customStrategy{allowSimultaneousProcessing: false}
_, err = createNewStrategy("custom-strategy", true, h)
assert.ErrorIs(t, err, base.ErrSimultaneousProcessingNotSupported)
}

type customStrategy struct {
allowSimultaneousProcessing bool
base.Strategy
}

Expand All @@ -121,7 +121,7 @@ func (s *customStrategy) Description() string {
return "this is a demonstration of loading strategies via custom plugins"
}
func (s *customStrategy) SupportsSimultaneousProcessing() bool {
return true
return s.allowSimultaneousProcessing
}
func (s *customStrategy) OnSignal(d data.Handler, _ funding.IFundingTransferer, _ portfolio.Handler) (signal.Event, error) {
return s.createSignal(d)
Expand Down
3 changes: 1 addition & 2 deletions config/config_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ func (c *Config) encryptConfigData(configData []byte) ([]byte, error) {

ciphertext := aead.Seal(nil, nil, configData, nil)

totalLen := len(encryptionPrefix) + len(c.storedSalt) + len(encryptionVersionPrefix) + versionSize + len(ciphertext)
appendedFile := make([]byte, totalLen)
appendedFile := make([]byte, len(encryptionPrefix)+len(c.storedSalt)+len(encryptionVersionPrefix)+versionSize+len(ciphertext))
offset := 0
copy(appendedFile[offset:], encryptionPrefix)
offset += len(encryptionPrefix)
Expand Down
4 changes: 2 additions & 2 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ func TestGetDefaultConfigurations(t *testing.T) {
defaultCfg, err := exchange.GetDefaultConfig(context.Background(), exch)
require.NoError(t, err, "GetDefaultConfig must not error")
require.NotNil(t, defaultCfg)
assert.NotEmpty(t, defaultCfg.Name)
assert.True(t, defaultCfg.Enabled)
assert.NotEmpty(t, defaultCfg.Name, "Name should not be empty")
assert.True(t, defaultCfg.Enabled, "Enabled should have the correct value")

if exch.SupportsWebsocket() {
assert.Positive(t, defaultCfg.WebsocketResponseCheckTimeout, 0)
Expand Down

0 comments on commit 070e09e

Please sign in to comment.