Subscriptions: Add List.Public, List.Enabled and List.Private (#1629)

This commit is contained in:
Gareth Kirwan
2024-10-10 04:48:16 +01:00
committed by GitHub
parent 910cdaacd4
commit c2dfb37efd
4 changed files with 66 additions and 12 deletions

View File

@@ -172,12 +172,7 @@ func (b *Base) SetSubscriptionsFromConfig() {
// Set config from the defaults, including any disabled subscriptions
b.Config.Features.Subscriptions = b.Features.Subscriptions
}
b.Features.Subscriptions = subscription.List{}
for _, s := range b.Config.Features.Subscriptions {
if s.Enabled {
b.Features.Subscriptions = append(b.Features.Subscriptions, s)
}
}
b.Features.Subscriptions = b.Config.Features.Subscriptions.Enabled()
if b.Verbose {
names := make([]string, 0, len(b.Features.Subscriptions))
for _, s := range b.Features.Subscriptions {

View File

@@ -1658,12 +1658,7 @@ func (ku *Kucoin) checkSubscriptions() {
return false
})
if upgraded {
ku.Features.Subscriptions = subscription.List{}
for _, s := range ku.Config.Features.Subscriptions {
if s.Enabled {
ku.Features.Subscriptions = append(ku.Features.Subscriptions, s)
}
}
ku.Features.Subscriptions = ku.Config.Features.Subscriptions.Enabled()
}
}

View File

@@ -113,3 +113,36 @@ func (l List) assetPairs(e iExchange) (assetPairs, error) {
}
return ap, nil
}
// Enabled returns a new list of only enabled subscriptions
func (l List) Enabled() List {
n := make(List, 0, len(l))
for _, s := range l {
if s.Enabled {
n = append(n, s)
}
}
return slices.Clip(n)
}
// Private returns only subscriptions which require authentication
func (l List) Private() List {
n := List{}
for _, s := range l {
if s.Authenticated {
n = append(n, s)
}
}
return n
}
// Public returns only subscriptions which do not require authentication
func (l List) Public() List {
n := List{}
for _, s := range l {
if !s.Authenticated {
n = append(n, s)
}
}
return n
}

View File

@@ -106,3 +106,34 @@ func TestListClone(t *testing.T) {
l[0].Interval = kline.OneHour
assert.NotEqual(t, n[0], l[0], "Subscriptions should be cloned")
}
var filterable = List{
{Channel: "a", Enabled: true, Authenticated: false},
{Channel: "b", Enabled: true, Authenticated: true},
{Channel: "c", Enabled: false, Authenticated: true},
{Channel: "d", Enabled: false, Authenticated: false},
}
func TestListEnabled(t *testing.T) {
t.Parallel()
l := filterable.Enabled()
require.Len(t, l, 2)
assert.Equal(t, filterable[:2], l)
assert.Len(t, filterable, 4)
}
func TestListPublic(t *testing.T) {
t.Parallel()
l := filterable.Public()
require.Len(t, l, 2)
assert.Equal(t, List{filterable[0], filterable[3]}, l)
assert.Len(t, filterable, 4)
}
func TestListPrivate(t *testing.T) {
t.Parallel()
l := filterable.Private()
require.Len(t, l, 2)
assert.Equal(t, filterable[1:3], l)
assert.Len(t, filterable, 4)
}