Relax case sensitive string comparisons in various parts of GCT

This commit is contained in:
Adrian Gallagher
2019-04-23 14:22:00 +10:00
parent 32e4dcb63d
commit 9bdcc22ae1
8 changed files with 29 additions and 32 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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"
}

View File

@@ -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)
}

View File

@@ -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
}
}

View File

@@ -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

View File

@@ -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

View File

@@ -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")
}