mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
maps: expansion of Key concept (#1349)
* moves everything to use single map keys, also breaks * full rollout * tests * fix a little bug * minor test fixups * Fix Key use * rm 🔑 from 🔑 struct name
This commit is contained in:
47
common/key/key.go
Normal file
47
common/key/key.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package key
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// ExchangePairAsset is a unique map key signature for exchange, currency pair and asset
|
||||
type ExchangePairAsset struct {
|
||||
Exchange string
|
||||
Base *currency.Item
|
||||
Quote *currency.Item
|
||||
Asset asset.Item
|
||||
}
|
||||
|
||||
// PairAsset is a unique map key signature for currency pair and asset
|
||||
type PairAsset struct {
|
||||
Base *currency.Item
|
||||
Quote *currency.Item
|
||||
Asset asset.Item
|
||||
}
|
||||
|
||||
// SubAccountCurrencyAsset is a unique map key signature for subaccount, currency code and asset
|
||||
type SubAccountCurrencyAsset struct {
|
||||
SubAccount string
|
||||
Currency *currency.Item
|
||||
Asset asset.Item
|
||||
}
|
||||
|
||||
// MatchesExchangeAsset checks if the key matches the exchange and asset
|
||||
func (k *ExchangePairAsset) MatchesExchangeAsset(exch string, item asset.Item) bool {
|
||||
return strings.EqualFold(k.Exchange, exch) && k.Asset == item
|
||||
}
|
||||
|
||||
// MatchesPairAsset checks if the key matches the pair and asset
|
||||
func (k *ExchangePairAsset) MatchesPairAsset(pair currency.Pair, item asset.Item) bool {
|
||||
return k.Base == pair.Base.Item &&
|
||||
k.Quote == pair.Quote.Item &&
|
||||
k.Asset == item
|
||||
}
|
||||
|
||||
// MatchesExchange checks if the exchange matches
|
||||
func (k *ExchangePairAsset) MatchesExchange(exch string) bool {
|
||||
return strings.EqualFold(k.Exchange, exch)
|
||||
}
|
||||
72
common/key/key_test.go
Normal file
72
common/key/key_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package key
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
func TestMatchesExchangeAsset(t *testing.T) {
|
||||
t.Parallel()
|
||||
cp := currency.NewPair(currency.BTC, currency.USD)
|
||||
k := ExchangePairAsset{
|
||||
Exchange: "test",
|
||||
Base: cp.Base.Item,
|
||||
Quote: cp.Quote.Item,
|
||||
Asset: asset.Spot,
|
||||
}
|
||||
if !k.MatchesExchangeAsset("test", asset.Spot) {
|
||||
t.Error("expected true")
|
||||
}
|
||||
if k.MatchesExchangeAsset("TEST", asset.Futures) {
|
||||
t.Error("expected false")
|
||||
}
|
||||
if k.MatchesExchangeAsset("test", asset.Futures) {
|
||||
t.Error("expected false")
|
||||
}
|
||||
if !k.MatchesExchangeAsset("TEST", asset.Spot) {
|
||||
t.Error("expected true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesPairAsset(t *testing.T) {
|
||||
t.Parallel()
|
||||
cp := currency.NewPair(currency.BTC, currency.USD)
|
||||
k := ExchangePairAsset{
|
||||
Base: cp.Base.Item,
|
||||
Quote: cp.Quote.Item,
|
||||
Asset: asset.Spot,
|
||||
}
|
||||
if !k.MatchesPairAsset(cp, asset.Spot) {
|
||||
t.Error("expected true")
|
||||
}
|
||||
if k.MatchesPairAsset(cp, asset.Futures) {
|
||||
t.Error("expected false")
|
||||
}
|
||||
if k.MatchesPairAsset(currency.EMPTYPAIR, asset.Futures) {
|
||||
t.Error("expected false")
|
||||
}
|
||||
if k.MatchesPairAsset(currency.NewPair(currency.BTC, currency.USDT), asset.Spot) {
|
||||
t.Error("expected false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchesExchange(t *testing.T) {
|
||||
t.Parallel()
|
||||
k := ExchangePairAsset{
|
||||
Exchange: "test",
|
||||
}
|
||||
if !k.MatchesExchange("test") {
|
||||
t.Error("expected true")
|
||||
}
|
||||
if !k.MatchesExchange("TEST") {
|
||||
t.Error("expected true")
|
||||
}
|
||||
if k.MatchesExchange("tèst") {
|
||||
t.Error("expected false")
|
||||
}
|
||||
if k.MatchesExchange("") {
|
||||
t.Error("expected false")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user