Skip to content

Commit

Permalink
Linter: Disable shadow linting for err
Browse files Browse the repository at this point in the history
It's been a year, and I'm still getting caught out by govet demanding I
don't shadow a var I was deliberately shadowing.
Made worse by an increase in clashes with stylecheck when they both want
opposite things on the same line.
  • Loading branch information
gbjk committed Jan 6, 2025
1 parent 7d4be35 commit f86fff4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ issues:
- text: "Expect WriteFile permissions to be 0600 or less"
linters:
- gosec
- text: 'shadow: declaration of "err" shadows declaration at'
linters: [ govet ]


exclude-dirs:
- vendor
Expand Down
50 changes: 19 additions & 31 deletions exchanges/bitfinex/bitfinex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,66 +1260,57 @@ func TestSubToMap(t *testing.T) {
assert.Equal(t, "tBTCLTC", r["symbol"], "symbol should not use colon delimiter if both currencies < 3 chars")
}

// TestWSPlaceOrder dials websocket, sends order request.
func TestWSPlaceOrder(t *testing.T) {
// TestWSNewOrder exercises WsNewOrder
func TestWSNewOrder(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
testexch.SetupWs(t, b)

_, err := b.WsNewOrder(&WsNewOrderRequest{
GroupID: 1,
Type: "EXCHANGE LIMIT",
Symbol: "tXRPUSD",
Amount: -20,
Price: 1000,
})
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
}

// TestWSCancelOrder dials websocket, sends cancel request.
// TestWSCancelOrder exercises WsCancelOrder
func TestWSCancelOrder(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
testexch.SetupWs(t, b)
if err := b.WsCancelOrder(1234); err != nil {
t.Error(err)
}
err := b.WsCancelOrder(1234)
assert.NoError(t, err)
}

// TestWSCancelOrder dials websocket, sends modify request.
func TestWSUpdateOrder(t *testing.T) {
// TestWSModifyOrder exercises WsModifyOrder
func TestWSModifyOrder(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
testexch.SetupWs(t, b)
err := b.WsModifyOrder(&WsUpdateOrderRequest{
OrderID: 1234,
Price: -111,
Amount: 111,
})
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
}

// TestWSCancelAllOrders dials websocket, sends cancel all request.
// TestWSCancelAllOrders exercises WsCancelAllOrders
func TestWSCancelAllOrders(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
testexch.SetupWs(t, b)
if err := b.WsCancelAllOrders(); err != nil {
t.Error(err)
}
err := b.WsCancelAllOrders()
assert.NoError(t, err)
}

// TestWSCancelAllOrders dials websocket, sends cancel all request.
// TestWSCancelMultiOrders exercises WsCancelMultiOrders
func TestWSCancelMultiOrders(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
testexch.SetupWs(t, b)
err := b.WsCancelMultiOrders([]int64{1, 2, 3, 4})
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
}

// TestWSNewOffer dials websocket, sends new offer request.
// TestWSNewOffer exercises WsNewOffer
func TestWSNewOffer(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
testexch.SetupWs(t, b)
Expand All @@ -1330,18 +1321,15 @@ func TestWSNewOffer(t *testing.T) {
Rate: 10,
Period: 30,
})
if err != nil {
t.Error(err)
}
assert.NoError(t, err)
}

// TestWSCancelOffer dials websocket, sends cancel offer request.
// TestWSCancelOffer exercises WsCancelOffer
func TestWSCancelOffer(t *testing.T) {
sharedtestvalues.SkipTestIfCredentialsUnset(t, b, canManipulateRealOrders)
testexch.SetupWs(t, b)
if err := b.WsCancelOffer(1234); err != nil {
t.Error(err)
}
err := b.WsCancelOffer(1234)
assert.NoError(t, err)
}

func TestWSSubscribedResponse(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions exchanges/bitfinex/bitfinex_types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bitfinex

import (
"encoding/json"
"errors"
"sync"
"time"
Expand Down Expand Up @@ -497,6 +498,11 @@ type wsTrade struct {
Period int64 // Funding offer period in days
}

// UnmarshalJSON unmarshals json bytes into a wsTrade
func (t *wsTrade) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &[5]any{&t.ID, &t.Timestamp, &t.Amount, &t.Price, &t.Period})
}

// Candle holds OHLC data
type Candle struct {
Timestamp time.Time
Expand Down
13 changes: 7 additions & 6 deletions exchanges/bitfinex/bitfinex_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"hash/crc32"
"math"
"net/http"
"sort"
"strconv"
Expand Down Expand Up @@ -919,13 +920,13 @@ func (b *Bitfinex) handleWSAllTrades(s *subscription.Subscription, respRaw []byt
}
if t.Amount < 0 {
t.Side = order.Sell
t.Amount *= -1
}
if feedEnabled {
b.Websocket.DataHandler <- &t
t.Amount = math.Abs(t.Amount)
}
trades[i] = t
}
if feedEnabled {
b.Websocket.DataHandler <- trades
}
if b.IsSaveTradeDataEnabled() {
err = trade.AddTradesToBuffer(b.GetName(), trades...)
}
Expand All @@ -938,7 +939,7 @@ func (b *Bitfinex) handleWSPublicTradesSnapshot(respRaw []byte) (trades []*wsTra
errs = common.AppendError(errs, fmt.Errorf("%w `tradesSnapshot[1][*]`: %w `%s`", errParsingWSField, jsonparser.UnknownValueTypeError, valueType))
} else {
t := &wsTrade{}
if err := json.Unmarshal(v, &[]any{&t.ID, &t.Timestamp, &t.Amount, &t.Price, &t.Period}); err != nil {
if err := json.Unmarshal(v, t); err != nil {
errs = common.AppendError(errs, fmt.Errorf("%w `tradesSnapshot[1][*]`: %w", errParsingWSField, err))
} else {
trades = append(trades, t)
Expand All @@ -961,7 +962,7 @@ func (b *Bitfinex) handleWSPublicTradeUpdate(respRaw []byte) (*wsTrade, error) {
return nil, fmt.Errorf("%w `tradesUpdate[2]`: %w `%s`", errParsingWSField, jsonparser.UnknownValueTypeError, valueType)
}
t := &wsTrade{}
if err := json.Unmarshal(v, &[]any{&t.ID, &t.Timestamp, &t.Amount, &t.Price, &t.Period}); err != nil {
if err := json.Unmarshal(v, t); err != nil {
return nil, fmt.Errorf("%w `tradeUpdate[2]`: %w", errParsingWSField, err)
}
return t, nil
Expand Down
2 changes: 1 addition & 1 deletion exchanges/btse/btse_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ func (b *BTSE) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item) err
var errs error
limits := make([]order.MinMaxLevel, 0, len(summary))
for _, marketInfo := range summary {
p, err := marketInfo.Pair() //nolint:govet // Deliberately shadow err
p, err := marketInfo.Pair()
if err != nil {
errs = common.AppendError(err, fmt.Errorf("%s: %w", p, err))
continue
Expand Down
2 changes: 1 addition & 1 deletion exchanges/subscription/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It
}

buf := &bytes.Buffer{}
if err := t.Execute(buf, subCtx); err != nil { //nolint:govet // Shadow, or gocritic will complain sloppyReassign
if err := t.Execute(buf, subCtx); err != nil {
return nil, err
}

Expand Down

0 comments on commit f86fff4

Please sign in to comment.