mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-14 07:26:47 +00:00
Add currency translation mapping
For example BTC is also XBT
This commit is contained in:
32
currency/translation/translation.go
Normal file
32
currency/translation/translation.go
Normal 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
|
||||
}
|
||||
42
currency/translation/translation_test.go
Normal file
42
currency/translation/translation_test.go
Normal 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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user