From 9bdcc22ae1de46c894fde795c2214bb2b8867635 Mon Sep 17 00:00:00 2001 From: Adrian Gallagher Date: Tue, 23 Apr 2019 14:22:00 +1000 Subject: [PATCH] Relax case sensitive string comparisons in various parts of GCT --- common/common.go | 10 +++++----- common/common_test.go | 8 ++++---- config/config.go | 12 ++++++------ currency/forexprovider/base/base_interface.go | 4 ++-- exchange.go | 19 ++++++++----------- exchanges/bitmex/bitmex_wrapper.go | 4 ++-- exchanges/request/request.go | 2 +- tools/exchange_template/exchange_template.go | 2 +- 8 files changed, 29 insertions(+), 32 deletions(-) diff --git a/common/common.go b/common/common.go index 1b6fa766..51eebeb6 100644 --- a/common/common.go +++ b/common/common.go @@ -206,20 +206,20 @@ func StringDataCompare(haystack []string, needle string) bool { return false } -// StringDataCompareUpper data checks the substring array with an input and returns +// StringDataCompareInsensitive data checks the substring array with an input and returns // a bool irrespective of lower or upper case strings -func StringDataCompareUpper(haystack []string, needle string) bool { +func StringDataCompareInsensitive(haystack []string, needle string) bool { for x := range haystack { - if StringToUpper(haystack[x]) == StringToUpper(needle) { + if strings.EqualFold(haystack[x], needle) { return true } } return false } -// StringDataContainsUpper checks the substring array with an input and returns +// StringDataContainsInsensitive checks the substring array with an input and returns // a bool irrespective of lower or upper case strings -func StringDataContainsUpper(haystack []string, needle string) bool { +func StringDataContainsInsensitive(haystack []string, needle string) bool { for _, data := range haystack { if strings.Contains(StringToUpper(data), StringToUpper(needle)) { return true diff --git a/common/common_test.go b/common/common_test.go index fdde63db..f01f7697 100644 --- a/common/common_test.go +++ b/common/common_test.go @@ -338,13 +338,13 @@ func TestStringDataCompareUpper(t *testing.T) { anotherNeedle := "WoRldD" expectedOutput := true expectedOutputTwo := false - actualResult := StringDataCompareUpper(originalHaystack, originalNeedle) + actualResult := StringDataCompareInsensitive(originalHaystack, originalNeedle) if actualResult != expectedOutput { t.Errorf("Test failed. Expected '%v'. Actual '%v'", expectedOutput, actualResult) } - actualResult = StringDataCompareUpper(originalHaystack, anotherNeedle) + actualResult = StringDataCompareInsensitive(originalHaystack, anotherNeedle) if actualResult != expectedOutputTwo { t.Errorf("Test failed. Expected '%v'. Actual '%v'", expectedOutput, actualResult) @@ -358,12 +358,12 @@ func TestStringDataContainsUpper(t *testing.T) { anotherNeedle := "ning" expectedOutput := true expectedOutputTwo := false - actualResult := StringDataContainsUpper(originalHaystack, originalNeedle) + actualResult := StringDataContainsInsensitive(originalHaystack, originalNeedle) if actualResult != expectedOutput { t.Errorf("Test failed. Expected '%v'. Actual '%v'", expectedOutput, actualResult) } - actualResult = StringDataContainsUpper(originalHaystack, anotherNeedle) + actualResult = StringDataContainsInsensitive(originalHaystack, anotherNeedle) if actualResult != expectedOutputTwo { t.Errorf("Test failed. Expected '%v'. Actual '%v'", expectedOutput, actualResult) diff --git a/config/config.go b/config/config.go index 50df97ba..2668a906 100644 --- a/config/config.go +++ b/config/config.go @@ -276,7 +276,7 @@ func (c *Config) GetExchangeBankAccounts(exchangeName, depositingCurrency string defer m.Unlock() for x := range c.Exchanges { - if c.Exchanges[x].Name == exchangeName { + if strings.EqualFold(c.Exchanges[x].Name, exchangeName) { for y := range c.Exchanges[x].BankAccounts { if common.StringContains(c.Exchanges[x].BankAccounts[y].SupportedCurrencies, depositingCurrency) { @@ -297,7 +297,7 @@ func (c *Config) UpdateExchangeBankAccounts(exchangeName string, bankCfg []BankA defer m.Unlock() for i := range c.Exchanges { - if c.Exchanges[i].Name == exchangeName { + if strings.EqualFold(c.Exchanges[i].Name, exchangeName) { c.Exchanges[i].BankAccounts = bankCfg return nil } @@ -698,7 +698,7 @@ func (c *Config) GetExchangeConfig(name string) (ExchangeConfig, error) { m.Lock() defer m.Unlock() for i := range c.Exchanges { - if c.Exchanges[i].Name == name { + if strings.EqualFold(c.Exchanges[i].Name, name) { return c.Exchanges[i], nil } } @@ -710,7 +710,7 @@ func (c *Config) GetForexProviderConfig(name string) (base.Settings, error) { m.Lock() defer m.Unlock() for i := range c.Currency.ForexProviders { - if c.Currency.ForexProviders[i].Name == name { + if strings.EqualFold(c.Currency.ForexProviders[i].Name, name) { return c.Currency.ForexProviders[i], nil } } @@ -734,7 +734,7 @@ func (c *Config) UpdateExchangeConfig(e *ExchangeConfig) error { m.Lock() defer m.Unlock() for i := range c.Exchanges { - if c.Exchanges[i].Name == e.Name { + if strings.EqualFold(c.Exchanges[i].Name, e.Name) { c.Exchanges[i] = *e return nil } @@ -747,7 +747,7 @@ func (c *Config) UpdateExchangeConfig(e *ExchangeConfig) error { func (c *Config) CheckExchangeConfigValues() error { exchanges := 0 for i := range c.Exchanges { - if c.Exchanges[i].Name == "GDAX" { + if strings.EqualFold(c.Exchanges[i].Name, "GDAX") { c.Exchanges[i].Name = "CoinbasePro" } diff --git a/currency/forexprovider/base/base_interface.go b/currency/forexprovider/base/base_interface.go index c40f8ab5..5e360833 100644 --- a/currency/forexprovider/base/base_interface.go +++ b/currency/forexprovider/base/base_interface.go @@ -59,7 +59,7 @@ func (p *Provider) GetNewRate(base string, currencies []string) (map[string]floa func (p Provider) CheckCurrencies(currencies []string) []string { var spillOver []string for _, c := range currencies { - if !common.StringDataCompareUpper(p.SupportedCurrencies, c) { + if !common.StringDataCompareInsensitive(p.SupportedCurrencies, c) { spillOver = append(spillOver, c) } } @@ -70,7 +70,7 @@ func (p Provider) CheckCurrencies(currencies []string) []string { func (f *FXHandler) GetCurrencyData(baseCurrency string, currencies []string) (map[string]float64, error) { var fullRange = currencies - if !common.StringDataCompareUpper(currencies, baseCurrency) { + if !common.StringDataCompareInsensitive(currencies, baseCurrency) { fullRange = append(fullRange, baseCurrency) } diff --git a/exchange.go b/exchange.go index f1a9da27..c379728f 100644 --- a/exchange.go +++ b/exchange.go @@ -2,6 +2,7 @@ package main import ( "errors" + "strings" "sync" "github.com/thrasher-/gocryptotrader/common" @@ -49,7 +50,7 @@ var ( // been loaded func CheckExchangeExists(exchName string) bool { for x := range bot.exchanges { - if common.StringToLower(bot.exchanges[x].GetName()) == common.StringToLower(exchName) { + if strings.EqualFold(bot.exchanges[x].GetName(), exchName) { return true } } @@ -59,7 +60,7 @@ func CheckExchangeExists(exchName string) bool { // GetExchangeByName returns an exchange given an exchange name func GetExchangeByName(exchName string) exchange.IBotExchange { for x := range bot.exchanges { - if common.StringToLower(bot.exchanges[x].GetName()) == common.StringToLower(exchName) { + if strings.EqualFold(bot.exchanges[x].GetName(), exchName) { return bot.exchanges[x] } } @@ -68,13 +69,11 @@ func GetExchangeByName(exchName string) exchange.IBotExchange { // ReloadExchange loads an exchange config by name func ReloadExchange(name string) error { - nameLower := common.StringToLower(name) - if len(bot.exchanges) == 0 { return ErrNoExchangesLoaded } - if !CheckExchangeExists(nameLower) { + if !CheckExchangeExists(name) { return ErrExchangeNotFound } @@ -83,7 +82,7 @@ func ReloadExchange(name string) error { return err } - e := GetExchangeByName(nameLower) + e := GetExchangeByName(name) e.Setup(&exchCfg) log.Debugf("%s exchange reloaded successfully.\n", name) return nil @@ -91,13 +90,11 @@ func ReloadExchange(name string) error { // UnloadExchange unloads an exchange by name func UnloadExchange(name string) error { - nameLower := common.StringToLower(name) - if len(bot.exchanges) == 0 { return ErrNoExchangesLoaded } - if !CheckExchangeExists(nameLower) { + if !CheckExchangeExists(name) { return ErrExchangeNotFound } @@ -113,7 +110,7 @@ func UnloadExchange(name string) error { } for x := range bot.exchanges { - if bot.exchanges[x].GetName() == name { + if strings.EqualFold(bot.exchanges[x].GetName(), name) { bot.exchanges[x].SetEnabled(false) bot.exchanges = append(bot.exchanges[:x], bot.exchanges[x+1:]...) return nil @@ -129,7 +126,7 @@ func LoadExchange(name string, useWG bool, wg *sync.WaitGroup) error { var exch exchange.IBotExchange if len(bot.exchanges) > 0 { - if CheckExchangeExists(nameLower) { + if CheckExchangeExists(name) { return ErrExchangeAlreadyLoaded } } diff --git a/exchanges/bitmex/bitmex_wrapper.go b/exchanges/bitmex/bitmex_wrapper.go index 02431657..a86f46c2 100644 --- a/exchanges/bitmex/bitmex_wrapper.go +++ b/exchanges/bitmex/bitmex_wrapper.go @@ -108,12 +108,12 @@ func (b *Bitmex) UpdateOrderbook(p currency.Pair, assetType string) (orderbook.B } for _, ob := range orderbookNew { - if strings.ToUpper(ob.Side) == exchange.SellOrderSide.ToString() { + if strings.EqualFold(ob.Side, exchange.SellOrderSide.ToString()) { orderBook.Asks = append(orderBook.Asks, orderbook.Item{Amount: float64(ob.Size), Price: ob.Price}) continue } - if strings.ToUpper(ob.Side) == exchange.BuyOrderSide.ToString() { + if strings.EqualFold(ob.Side, exchange.BuyOrderSide.ToString()) { orderBook.Bids = append(orderBook.Bids, orderbook.Item{Amount: float64(ob.Size), Price: ob.Price}) continue diff --git a/exchanges/request/request.go b/exchanges/request/request.go index 3772f1fa..2454ef16 100644 --- a/exchanges/request/request.go +++ b/exchanges/request/request.go @@ -220,7 +220,7 @@ func New(name string, authLimit, unauthLimit *RateLimit, httpRequester *http.Cli // IsValidMethod returns whether the supplied method is supported func IsValidMethod(method string) bool { - return common.StringDataCompareUpper(supportedMethods, method) + return common.StringDataCompareInsensitive(supportedMethods, method) } // IsValidCycle checks to see whether the current request cycle is valid or not diff --git a/tools/exchange_template/exchange_template.go b/tools/exchange_template/exchange_template.go index 3735ac25..2d89315f 100644 --- a/tools/exchange_template/exchange_template.go +++ b/tools/exchange_template/exchange_template.go @@ -110,7 +110,7 @@ func main() { configTestExchanges = append(configTestExchanges, configTestFile.Exchanges[x].Name) } - if common.StringDataContainsUpper(configTestExchanges, capName) { + if common.StringDataContainsInsensitive(configTestExchanges, capName) { log.Fatal("GoCryptoTrader: Exchange templating configuration error - exchange already exists") }