From ee28d18f189fb1f022e746a81e3290c4bea46931 Mon Sep 17 00:00:00 2001 From: leilaes <44886693+leilaes@users.noreply.github.com> Date: Thu, 9 May 2019 16:13:48 +1200 Subject: [PATCH] Omit unnecessary else statements when `if` does not flow into the next statement (#293) * Removing unnecessary else * Fix go fmt issues --- codelingo.yaml | 1 + config/config.go | 25 +- config/config_encryption.go | 4 +- exchange.go | 11 +- exchanges/bitfinex/bitfinex_test.go | 3 +- exchanges/bitfinex/bitfinex_websocket.go | 391 +++++++++++------------ 6 files changed, 215 insertions(+), 220 deletions(-) diff --git a/codelingo.yaml b/codelingo.yaml index 21d580ac..a35408fb 100644 --- a/codelingo.yaml +++ b/codelingo.yaml @@ -5,6 +5,7 @@ tenets: - import: codelingo/effective-go/good-package-name - import: codelingo/effective-go/single-method-interface-name - import: codelingo/effective-go/underscores-in-name + - import: codelingo/effective-go/unnecessary-else # Overwrite one tenet with custom logic # - import: codelingo/effective-go/comment-first-word-when-empty diff --git a/config/config.go b/config/config.go index 4265d0f6..14611f57 100644 --- a/config/config.go +++ b/config/config.go @@ -1217,20 +1217,19 @@ func GetFilePath(file string) (string, error) { _, err := os.Stat(oldDirs[x]) if os.IsNotExist(err) { continue - } else { - if filepath.Ext(oldDirs[x]) == ".json" { - err = os.Rename(oldDirs[x], newDirs[0]) - if err != nil { - return "", err - } - log.Debugf("Renamed old config file %s to %s", oldDirs[x], newDirs[0]) - } else { - err = os.Rename(oldDirs[x], newDirs[1]) - if err != nil { - return "", err - } - log.Debugf("Renamed old config file %s to %s", oldDirs[x], newDirs[1]) + } + if filepath.Ext(oldDirs[x]) == ".json" { + err = os.Rename(oldDirs[x], newDirs[0]) + if err != nil { + return "", err } + log.Debugf("Renamed old config file %s to %s", oldDirs[x], newDirs[0]) + } else { + err = os.Rename(oldDirs[x], newDirs[1]) + if err != nil { + return "", err + } + log.Debugf("Renamed old config file %s to %s", oldDirs[x], newDirs[1]) } } diff --git a/config/config_encryption.go b/config/config_encryption.go index 638fa531..a94dc6fb 100644 --- a/config/config_encryption.go +++ b/config/config_encryption.go @@ -81,10 +81,8 @@ func PromptForConfigKey(initialSetup bool) ([]byte, error) { if bytes.Equal(p1, p2) { cryptoKey = p1 break - } else { - log.Printf("Passwords did not match, please try again.") - continue } + log.Printf("Passwords did not match, please try again.") } return cryptoKey, nil } diff --git a/exchange.go b/exchange.go index c379728f..1825af82 100644 --- a/exchange.go +++ b/exchange.go @@ -244,12 +244,11 @@ func SetupExchanges() { if !exch.Enabled { log.Debugf("%s: Exchange support: Disabled", exch.Name) continue - } else { - err := LoadExchange(exch.Name, true, &wg) - if err != nil { - log.Errorf("LoadExchange %s failed: %s", exch.Name, err) - continue - } + } + err := LoadExchange(exch.Name, true, &wg) + if err != nil { + log.Errorf("LoadExchange %s failed: %s", exch.Name, err) + continue } log.Debugf( "%s: Exchange support: Enabled (Authenticated API support: %s - Verbose mode: %s).\n", diff --git a/exchanges/bitfinex/bitfinex_test.go b/exchanges/bitfinex/bitfinex_test.go index ae3ec8d1..3f55ba59 100644 --- a/exchanges/bitfinex/bitfinex_test.go +++ b/exchanges/bitfinex/bitfinex_test.go @@ -221,9 +221,8 @@ func TestGetSymbols(t *testing.T) { for _, explicitSymbol := range expectedCurrencies { if common.StringDataCompare(expectedCurrencies, explicitSymbol) { break - } else { - t.Error("BitfinexGetSymbols currency mismatch with: ", explicitSymbol) } + t.Error("BitfinexGetSymbols currency mismatch with: ", explicitSymbol) } } else { t.Error("BitfinexGetSymbols currency mismatch, Expected Currencies < Exchange Currencies") diff --git a/exchanges/bitfinex/bitfinex_websocket.go b/exchanges/bitfinex/bitfinex_websocket.go index 39da6cae..ad8ade13 100644 --- a/exchanges/bitfinex/bitfinex_websocket.go +++ b/exchanges/bitfinex/bitfinex_websocket.go @@ -272,227 +272,226 @@ func (b *Bitfinex) WsDataHandler() { b.Websocket.DataHandler <- fmt.Errorf("bitfinex.go error - Unable to locate chanID: %d", chanID) continue - } else { - if len(chanData) == 2 { - if reflect.TypeOf(chanData[1]).String() == "string" { - if chanData[1].(string) == bitfinexWebsocketHeartbeat { - continue - } else if chanData[1].(string) == "pong" { - pongReceive <- struct{}{} - continue - } + } + if len(chanData) == 2 { + if reflect.TypeOf(chanData[1]).String() == "string" { + if chanData[1].(string) == bitfinexWebsocketHeartbeat { + continue + } else if chanData[1].(string) == "pong" { + pongReceive <- struct{}{} + continue } } + } - switch chanInfo.Channel { - case "book": - newOrderbook := []WebsocketBook{} - switch len(chanData) { - case 2: - data := chanData[1].([]interface{}) - for _, x := range data { - y := x.([]interface{}) - newOrderbook = append(newOrderbook, WebsocketBook{ - Price: y[0].(float64), - Count: int(y[1].(float64)), - Amount: y[2].(float64)}) - } - - case 4: + switch chanInfo.Channel { + case "book": + newOrderbook := []WebsocketBook{} + switch len(chanData) { + case 2: + data := chanData[1].([]interface{}) + for _, x := range data { + y := x.([]interface{}) newOrderbook = append(newOrderbook, WebsocketBook{ - Price: chanData[1].(float64), - Count: int(chanData[2].(float64)), - Amount: chanData[3].(float64)}) + Price: y[0].(float64), + Count: int(y[1].(float64)), + Amount: y[2].(float64)}) } - if len(newOrderbook) > 1 { - err := b.WsInsertSnapshot(currency.NewPairFromString(chanInfo.Pair), - "SPOT", - newOrderbook) + case 4: + newOrderbook = append(newOrderbook, WebsocketBook{ + Price: chanData[1].(float64), + Count: int(chanData[2].(float64)), + Amount: chanData[3].(float64)}) + } - if err != nil { - b.Websocket.DataHandler <- fmt.Errorf("bitfinex_websocket.go inserting snapshot error: %s", - err) - } + if len(newOrderbook) > 1 { + err := b.WsInsertSnapshot(currency.NewPairFromString(chanInfo.Pair), + "SPOT", + newOrderbook) + + if err != nil { + b.Websocket.DataHandler <- fmt.Errorf("bitfinex_websocket.go inserting snapshot error: %s", + err) + } + continue + } + + err := b.WsUpdateOrderbook(currency.NewPairFromString(chanInfo.Pair), + "SPOT", + newOrderbook[0]) + + if err != nil { + b.Websocket.DataHandler <- fmt.Errorf("bitfinex_websocket.go updating orderbook error: %s", + err) + } + + case "ticker": + b.Websocket.DataHandler <- exchange.TickerData{ + Quantity: chanData[8].(float64), + ClosePrice: chanData[7].(float64), + HighPrice: chanData[9].(float64), + LowPrice: chanData[10].(float64), + Pair: currency.NewPairFromString(chanInfo.Pair), + Exchange: b.GetName(), + AssetType: "SPOT", + } + + case "account": + switch chanData[1].(string) { + case bitfinexWebsocketPositionSnapshot: + positionSnapshot := []WebsocketPosition{} + data := chanData[2].([]interface{}) + for _, x := range data { + y := x.([]interface{}) + positionSnapshot = append(positionSnapshot, + WebsocketPosition{ + Pair: y[0].(string), + Status: y[1].(string), + Amount: y[2].(float64), + Price: y[3].(float64), + MarginFunding: y[4].(float64), + MarginFundingType: int(y[5].(float64))}) + } + + if len(positionSnapshot) == 0 { continue } - err := b.WsUpdateOrderbook(currency.NewPairFromString(chanInfo.Pair), - "SPOT", - newOrderbook[0]) + b.Websocket.DataHandler <- positionSnapshot - if err != nil { - b.Websocket.DataHandler <- fmt.Errorf("bitfinex_websocket.go updating orderbook error: %s", - err) + case bitfinexWebsocketPositionNew, bitfinexWebsocketPositionUpdate, bitfinexWebsocketPositionClose: + data := chanData[2].([]interface{}) + position := WebsocketPosition{ + Pair: data[0].(string), + Status: data[1].(string), + Amount: data[2].(float64), + Price: data[3].(float64), + MarginFunding: data[4].(float64), + MarginFundingType: int(data[5].(float64))} + + b.Websocket.DataHandler <- position + + case bitfinexWebsocketWalletSnapshot: + data := chanData[2].([]interface{}) + walletSnapshot := []WebsocketWallet{} + for _, x := range data { + y := x.([]interface{}) + walletSnapshot = append(walletSnapshot, + WebsocketWallet{ + Name: y[0].(string), + Currency: y[1].(string), + Balance: y[2].(float64), + UnsettledInterest: y[3].(float64)}) } - case "ticker": - b.Websocket.DataHandler <- exchange.TickerData{ - Quantity: chanData[8].(float64), - ClosePrice: chanData[7].(float64), - HighPrice: chanData[9].(float64), - LowPrice: chanData[10].(float64), - Pair: currency.NewPairFromString(chanInfo.Pair), - Exchange: b.GetName(), - AssetType: "SPOT", + b.Websocket.DataHandler <- walletSnapshot + + case bitfinexWebsocketWalletUpdate: + data := chanData[2].([]interface{}) + wallet := WebsocketWallet{ + Name: data[0].(string), + Currency: data[1].(string), + Balance: data[2].(float64), + UnsettledInterest: data[3].(float64)} + + b.Websocket.DataHandler <- wallet + + case bitfinexWebsocketOrderSnapshot: + orderSnapshot := []WebsocketOrder{} + data := chanData[2].([]interface{}) + for _, x := range data { + y := x.([]interface{}) + orderSnapshot = append(orderSnapshot, + WebsocketOrder{ + OrderID: int64(y[0].(float64)), + Pair: y[1].(string), + Amount: y[2].(float64), + OrigAmount: y[3].(float64), + OrderType: y[4].(string), + Status: y[5].(string), + Price: y[6].(float64), + PriceAvg: y[7].(float64), + Timestamp: y[8].(string)}) } - case "account": - switch chanData[1].(string) { - case bitfinexWebsocketPositionSnapshot: - positionSnapshot := []WebsocketPosition{} - data := chanData[2].([]interface{}) - for _, x := range data { - y := x.([]interface{}) - positionSnapshot = append(positionSnapshot, - WebsocketPosition{ - Pair: y[0].(string), - Status: y[1].(string), - Amount: y[2].(float64), - Price: y[3].(float64), - MarginFunding: y[4].(float64), - MarginFundingType: int(y[5].(float64))}) - } + b.Websocket.DataHandler <- orderSnapshot - if len(positionSnapshot) == 0 { + case bitfinexWebsocketOrderNew, bitfinexWebsocketOrderUpdate, bitfinexWebsocketOrderCancel: + data := chanData[2].([]interface{}) + order := WebsocketOrder{ + OrderID: int64(data[0].(float64)), + Pair: data[1].(string), + Amount: data[2].(float64), + OrigAmount: data[3].(float64), + OrderType: data[4].(string), + Status: data[5].(string), + Price: data[6].(float64), + PriceAvg: data[7].(float64), + Timestamp: data[8].(string), + Notify: int(data[9].(float64))} + + b.Websocket.DataHandler <- order + + case bitfinexWebsocketTradeExecuted: + data := chanData[2].([]interface{}) + trade := WebsocketTradeExecuted{ + TradeID: int64(data[0].(float64)), + Pair: data[1].(string), + Timestamp: int64(data[2].(float64)), + OrderID: int64(data[3].(float64)), + AmountExecuted: data[4].(float64), + PriceExecuted: data[5].(float64)} + + b.Websocket.DataHandler <- trade + } + + case "trades": + trades := []WebsocketTrade{} + switch len(chanData) { + case 2: + data := chanData[1].([]interface{}) + for _, x := range data { + y := x.([]interface{}) + if _, ok := y[0].(string); ok { continue } - b.Websocket.DataHandler <- positionSnapshot + id, _ := y[0].(float64) - case bitfinexWebsocketPositionNew, bitfinexWebsocketPositionUpdate, bitfinexWebsocketPositionClose: - data := chanData[2].([]interface{}) - position := WebsocketPosition{ - Pair: data[0].(string), - Status: data[1].(string), - Amount: data[2].(float64), - Price: data[3].(float64), - MarginFunding: data[4].(float64), - MarginFundingType: int(data[5].(float64))} - - b.Websocket.DataHandler <- position - - case bitfinexWebsocketWalletSnapshot: - data := chanData[2].([]interface{}) - walletSnapshot := []WebsocketWallet{} - for _, x := range data { - y := x.([]interface{}) - walletSnapshot = append(walletSnapshot, - WebsocketWallet{ - Name: y[0].(string), - Currency: y[1].(string), - Balance: y[2].(float64), - UnsettledInterest: y[3].(float64)}) - } - - b.Websocket.DataHandler <- walletSnapshot - - case bitfinexWebsocketWalletUpdate: - data := chanData[2].([]interface{}) - wallet := WebsocketWallet{ - Name: data[0].(string), - Currency: data[1].(string), - Balance: data[2].(float64), - UnsettledInterest: data[3].(float64)} - - b.Websocket.DataHandler <- wallet - - case bitfinexWebsocketOrderSnapshot: - orderSnapshot := []WebsocketOrder{} - data := chanData[2].([]interface{}) - for _, x := range data { - y := x.([]interface{}) - orderSnapshot = append(orderSnapshot, - WebsocketOrder{ - OrderID: int64(y[0].(float64)), - Pair: y[1].(string), - Amount: y[2].(float64), - OrigAmount: y[3].(float64), - OrderType: y[4].(string), - Status: y[5].(string), - Price: y[6].(float64), - PriceAvg: y[7].(float64), - Timestamp: y[8].(string)}) - } - - b.Websocket.DataHandler <- orderSnapshot - - case bitfinexWebsocketOrderNew, bitfinexWebsocketOrderUpdate, bitfinexWebsocketOrderCancel: - data := chanData[2].([]interface{}) - order := WebsocketOrder{ - OrderID: int64(data[0].(float64)), - Pair: data[1].(string), - Amount: data[2].(float64), - OrigAmount: data[3].(float64), - OrderType: data[4].(string), - Status: data[5].(string), - Price: data[6].(float64), - PriceAvg: data[7].(float64), - Timestamp: data[8].(string), - Notify: int(data[9].(float64))} - - b.Websocket.DataHandler <- order - - case bitfinexWebsocketTradeExecuted: - data := chanData[2].([]interface{}) - trade := WebsocketTradeExecuted{ - TradeID: int64(data[0].(float64)), - Pair: data[1].(string), - Timestamp: int64(data[2].(float64)), - OrderID: int64(data[3].(float64)), - AmountExecuted: data[4].(float64), - PriceExecuted: data[5].(float64)} - - b.Websocket.DataHandler <- trade + trades = append(trades, + WebsocketTrade{ + ID: int64(id), + Timestamp: int64(y[1].(float64)), + Price: y[2].(float64), + Amount: y[3].(float64)}) } - case "trades": - trades := []WebsocketTrade{} - switch len(chanData) { - case 2: - data := chanData[1].([]interface{}) - for _, x := range data { - y := x.([]interface{}) - if _, ok := y[0].(string); ok { - continue - } + case 7: + trade := WebsocketTrade{ + ID: int64(chanData[3].(float64)), + Timestamp: int64(chanData[4].(float64)), + Price: chanData[5].(float64), + Amount: chanData[6].(float64)} + trades = append(trades, trade) + } - id, _ := y[0].(float64) - - trades = append(trades, - WebsocketTrade{ - ID: int64(id), - Timestamp: int64(y[1].(float64)), - Price: y[2].(float64), - Amount: y[3].(float64)}) - } - - case 7: - trade := WebsocketTrade{ - ID: int64(chanData[3].(float64)), - Timestamp: int64(chanData[4].(float64)), - Price: chanData[5].(float64), - Amount: chanData[6].(float64)} - trades = append(trades, trade) + if len(trades) > 0 { + side := "BUY" + newAmount := trades[0].Amount + if newAmount < 0 { + side = "SELL" + newAmount *= -1 } - if len(trades) > 0 { - side := "BUY" - newAmount := trades[0].Amount - if newAmount < 0 { - side = "SELL" - newAmount *= -1 - } - - b.Websocket.DataHandler <- exchange.TradeData{ - CurrencyPair: currency.NewPairFromString(chanInfo.Pair), - Timestamp: time.Unix(trades[0].Timestamp, 0), - Price: trades[0].Price, - Amount: newAmount, - Exchange: b.GetName(), - AssetType: "SPOT", - Side: side, - } + b.Websocket.DataHandler <- exchange.TradeData{ + CurrencyPair: currency.NewPairFromString(chanInfo.Pair), + Timestamp: time.Unix(trades[0].Timestamp, 0), + Price: trades[0].Price, + Amount: newAmount, + Exchange: b.GetName(), + AssetType: "SPOT", + Side: side, } } }