From edc07f7839290ce0fbe767c2b9378ce98379aeb4 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 18 Dec 2019 11:38:36 +1100 Subject: [PATCH] [Config] Add "ID" field to BankAccount struct & GetBankAccountByID method (#402) * added id field to bank struct * fixed casing on error message * whitespace :D * turns out you shouldn't turn multiple tests at the same time that modify a var --- config/config.go | 14 ++++++++++++++ config/config_test.go | 24 ++++++++++++++++++++++++ config/config_types.go | 2 ++ 3 files changed, 40 insertions(+) diff --git a/config/config.go b/config/config.go index 94892c1f..b2e286b6 100644 --- a/config/config.go +++ b/config/config.go @@ -110,6 +110,7 @@ func (c *Config) CheckClientBankAccounts() { if len(c.BankAccounts) == 0 { c.BankAccounts = append(c.BankAccounts, BankAccount{ + ID: "test-bank-01", BankName: "Test Bank", BankAddress: "42 Bank Street", BankPostalCode: "13337", @@ -137,6 +138,19 @@ func (c *Config) CheckClientBankAccounts() { } } +// GetBankAccountByID Returns a bank account based on its ID +func (c *Config) GetBankAccountByID(id string) (*BankAccount, error) { + m.Lock() + defer m.Unlock() + + for x := range c.BankAccounts { + if strings.EqualFold(c.BankAccounts[x].ID, id) { + return &c.BankAccounts[x], nil + } + } + return nil, fmt.Errorf(ErrBankAccountNotFound, id) +} + // PurgeExchangeAPICredentials purges the stored API credentials func (c *Config) PurgeExchangeAPICredentials() { m.Lock() diff --git a/config/config_test.go b/config/config_test.go index 21a1a386..88577db5 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -198,6 +198,30 @@ func TestCheckClientBankAccounts(t *testing.T) { } } +func TestGetBankAccountByID(t *testing.T) { + cfg := GetConfig() + err := cfg.LoadConfig(TestFile, true) + if err != nil { + t.Error("CheckClientBankAccounts LoadConfig error", err) + } + + cfg.BankAccounts = nil + cfg.CheckClientBankAccounts() + if len(cfg.BankAccounts) == 0 { + t.Error("CheckClientBankAccounts error:", err) + } + + _, err = cfg.GetBankAccountByID("test-bank-01") + if err != nil { + t.Error(err) + } + + _, err = cfg.GetBankAccountByID("invalid-test-bank-01") + if err == nil { + t.Error("error expected for invalid account received nil") + } +} + func TestPurgeExchangeCredentials(t *testing.T) { t.Parallel() var c Config diff --git a/config/config_types.go b/config/config_types.go index 6e629610..e7aacaff 100644 --- a/config/config_types.go +++ b/config/config_types.go @@ -49,6 +49,7 @@ const ( ErrFailureOpeningConfig = "fatal error opening %s file. Error: %s" ErrCheckingConfigValues = "fatal error checking config values. Error: %s" ErrSavingConfigBytesMismatch = "config file %q bytes comparison doesn't match, read %s expected %s" + ErrBankAccountNotFound = "bank account ID: %v not found" WarningWebserverCredentialValuesEmpty = "webserver support disabled due to empty Username/Password values" WarningWebserverListenAddressInvalid = "webserver support disabled due to invalid listen address" WarningExchangeAuthAPIDefaultOrEmptyValues = "exchange %s authenticated API support disabled due to default/empty APIKey/Secret/ClientID values" @@ -226,6 +227,7 @@ type CurrencyPairFormatConfig struct { // currency type BankAccount struct { Enabled bool `json:"enabled"` + ID string `json:"id,omitempty"` BankName string `json:"bankName"` BankAddress string `json:"bankAddress"` BankPostalCode string `json:"bankPostalCode"`