From f86fff443ec5f570a8cd8f33a0eee643d50bcccf Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Wed, 18 Dec 2024 13:52:34 +0700 Subject: [PATCH] Linter: Disable shadow linting for err 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. --- .golangci.yml | 3 ++ exchanges/bitfinex/bitfinex_test.go | 50 +++++++++--------------- exchanges/bitfinex/bitfinex_types.go | 6 +++ exchanges/bitfinex/bitfinex_websocket.go | 13 +++--- exchanges/btse/btse_wrapper.go | 2 +- exchanges/subscription/template.go | 2 +- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index b59ae9635b3..c577a1f7d5d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 diff --git a/exchanges/bitfinex/bitfinex_test.go b/exchanges/bitfinex/bitfinex_test.go index eecbe03cd95..629159e01f5 100644 --- a/exchanges/bitfinex/bitfinex_test.go +++ b/exchanges/bitfinex/bitfinex_test.go @@ -1260,11 +1260,10 @@ 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", @@ -1272,22 +1271,19 @@ func TestWSPlaceOrder(t *testing.T) { 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{ @@ -1295,31 +1291,26 @@ func TestWSUpdateOrder(t *testing.T) { 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) @@ -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) { diff --git a/exchanges/bitfinex/bitfinex_types.go b/exchanges/bitfinex/bitfinex_types.go index 1ad4d314947..085e01960ad 100644 --- a/exchanges/bitfinex/bitfinex_types.go +++ b/exchanges/bitfinex/bitfinex_types.go @@ -1,6 +1,7 @@ package bitfinex import ( + "encoding/json" "errors" "sync" "time" @@ -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 diff --git a/exchanges/bitfinex/bitfinex_websocket.go b/exchanges/bitfinex/bitfinex_websocket.go index b183329f6d5..e7d0978a479 100644 --- a/exchanges/bitfinex/bitfinex_websocket.go +++ b/exchanges/bitfinex/bitfinex_websocket.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "hash/crc32" + "math" "net/http" "sort" "strconv" @@ -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...) } @@ -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) @@ -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 diff --git a/exchanges/btse/btse_wrapper.go b/exchanges/btse/btse_wrapper.go index 72d25f42925..54fa94d2a79 100644 --- a/exchanges/btse/btse_wrapper.go +++ b/exchanges/btse/btse_wrapper.go @@ -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 diff --git a/exchanges/subscription/template.go b/exchanges/subscription/template.go index ad48f3b462e..f112c5a390a 100644 --- a/exchanges/subscription/template.go +++ b/exchanges/subscription/template.go @@ -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 }