currency: Make pairs.Add variadic (#1566)

* Making Pairs(Add) Variatic

* Slight improvements

* Implementation overhaul

* Improvements

* Changing code which can use the variadic functionality

* Fixing silliness

* thing left over
This commit is contained in:
cranktakular
2024-07-12 14:36:05 +10:00
committed by GitHub
parent c601575c66
commit 90fee94c76
5 changed files with 40 additions and 20 deletions

View File

@@ -53,12 +53,8 @@ func TestCreateUSDTrackingPairs(t *testing.T) {
cp3 := currency.NewPair(currency.LTC, currency.BTC)
exchB := exch.GetBase()
eba := exchB.CurrencyPairs.Pairs[a]
eba.Available = eba.Available.Add(cp)
eba.Enabled = eba.Enabled.Add(cp)
eba.Available = eba.Available.Add(cp2)
eba.Enabled = eba.Enabled.Add(cp2)
eba.Available = eba.Available.Add(cp3)
eba.Enabled = eba.Enabled.Add(cp3)
eba.Available = eba.Available.Add(cp, cp2, cp3)
eba.Enabled = eba.Enabled.Add(cp, cp2, cp3)
eba.AssetEnabled = convert.BoolPtr(true)
err = em.Add(exch)

View File

@@ -211,12 +211,16 @@ func (p Pairs) Remove(pair Pair) (Pairs, error) {
}
// Add adds a specified pair to the list of pairs if it doesn't exist
func (p Pairs) Add(pair Pair) Pairs {
if p.Contains(pair, true) {
return p
func (p Pairs) Add(pairs ...Pair) Pairs {
merge := append(slices.Clone(p), pairs...)
var filterInt int
for x := len(p); x < len(merge); x++ {
if !merge[:len(p)+filterInt].Contains(merge[x], true) {
merge[len(p)+filterInt] = merge[x]
filterInt++
}
}
p = append(p, pair)
return p
return merge[:len(p)+filterInt]
}
// GetMatch returns either the pair that is equal including the reciprocal for

View File

@@ -267,19 +267,43 @@ func TestAdd(t *testing.T) {
NewPair(LTC, USD),
NewPair(LTC, USDT),
}
// Test adding a new pair to the list of pairs
p := NewPair(BTC, USDT)
pairs = pairs.Add(p)
if !pairs.Contains(p, true) || len(pairs) != 4 {
t.Error("TestAdd unexpected result")
}
// Now test adding a pair which already exists
pairs = pairs.Add(p)
if len(pairs) != 4 {
t.Error("TestAdd unexpected result")
}
// Test adding multiple pairs
pairs = pairs.Add(NewPair(BTC, LTC), NewPair(ETH, USD))
if len(pairs) != 6 {
t.Error("TestAdd unexpected result")
}
// Test adding multiple duplicate pairs
pairs = pairs.Add(NewPair(ETH, USDT), NewPair(ETH, USDT))
if len(pairs) != 7 {
t.Error("TestAdd unexpected result")
}
// Test whether the original pairs have been modified
pairsWithExtraBaggage := make(Pairs, 0, len(pairs)+3)
pairsWithExtraBaggage = append(pairsWithExtraBaggage, pairs...)
brain := NewPair(BRAIN, USD)
withBrain := pairsWithExtraBaggage.Add(NewPair(BTC, LTC), brain)
if len(pairs) != 7 {
t.Error("TestAdd unexpected result")
}
assert.Equal(t, brain, withBrain[len(withBrain)-1])
badger := NewPair(BADGER, USD)
withBadger := pairsWithExtraBaggage.Add(NewPair(BTC, LTC), badger)
if len(pairs) != 7 {
t.Error("TestAdd unexpected result")
}
assert.Equal(t, badger, withBadger[len(withBadger)-1])
assert.Equal(t, brain, withBrain[len(withBrain)-1])
}
func TestContains(t *testing.T) {

View File

@@ -168,10 +168,8 @@ func SetupCurrencyPairsForExchangeAsset(t *testing.T, exch exchange.IBotExchange
t.Fatal(err)
}
epLen := len(enabledPairs)
for i := range cp {
availPairs = availPairs.Add(cp[i])
enabledPairs = enabledPairs.Add(cp[i])
}
availPairs = availPairs.Add(cp...)
enabledPairs = enabledPairs.Add(cp...)
if len(availPairs) != apLen {
err = b.CurrencyPairs.StorePairs(a, availPairs, false)
if err != nil {

View File

@@ -159,8 +159,6 @@ func (s *Subscription) AddPairs(pairs ...currency.Pair) {
return
}
s.m.Lock()
for _, p := range pairs {
s.Pairs = s.Pairs.Add(p)
}
s.Pairs = s.Pairs.Add(pairs...)
s.m.Unlock()
}