Add currency translation mapping

For example BTC is also XBT
This commit is contained in:
Adrian Gallagher
2018-01-02 18:49:59 +11:00
parent 4b3a5c4ba0
commit 8e7e2fb914
2 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package translation
import (
"errors"
"github.com/thrasher-/gocryptotrader/currency/pair"
)
var translations = map[pair.CurrencyItem]pair.CurrencyItem{
"BTC": "XBT",
"DOGE": "XDG",
"USD": "USDT",
}
// GetTranslation returns similar strings for a particular currency
func GetTranslation(currency pair.CurrencyItem) (pair.CurrencyItem, error) {
result, ok := translations[currency]
if !ok {
return "", errors.New("no translation found for specified currency")
}
return result, nil
}
// HasTranslation returns whether or not a particular currency has a translation
func HasTranslation(currency pair.CurrencyItem) bool {
_, ok := translations[currency]
if !ok {
return false
}
return true
}

View File

@@ -0,0 +1,42 @@
package translation
import (
"testing"
"github.com/thrasher-/gocryptotrader/currency/pair"
)
func TestGetTranslation(t *testing.T) {
currencyPair := pair.NewCurrencyPair("BTC", "USD")
expected := pair.CurrencyItem("XBT")
actual, err := GetTranslation(currencyPair.FirstCurrency)
if err != nil {
t.Error("GetTranslation: failed to retrieve translation for BTC")
}
if expected != actual {
t.Error("GetTranslation: translation result was different to expected result")
}
currencyPair.FirstCurrency = "ETH"
_, err = GetTranslation(currencyPair.FirstCurrency)
if err == nil {
t.Error("GetTranslation: no error on non translatable currency")
}
}
func TestHasTranslation(t *testing.T) {
currencyPair := pair.NewCurrencyPair("BTC", "USD")
expected := true
actual := HasTranslation(currencyPair.FirstCurrency)
if expected != actual {
t.Error("HasTranslation: translation result was different to expected result")
}
currencyPair.FirstCurrency = "ETH"
expected = false
actual = HasTranslation(currencyPair.FirstCurrency)
if expected != actual {
t.Error("HasTranslation: translation result was different to expected result")
}
}