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:
Scott
2023-10-04 09:19:41 +10:00
committed by GitHub
parent 033a72b61a
commit 91d699be9d
47 changed files with 1478 additions and 1339 deletions

47
common/key/key.go Normal file
View 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
View 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")
}
}