From 1b2c317489c75c7bec47f371c939e81652c9ef30 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Thu, 11 Jul 2024 18:25:00 +0700 Subject: [PATCH] Common: Add common.SortStrings for stringers --- common/common.go | 10 ++++++++++ common/common_test.go | 10 ++++++++++ currency/pairs_test.go | 10 ---------- exchanges/kucoin/kucoin_test.go | 2 +- exchanges/kucoin/kucoin_websocket.go | 2 +- exchanges/subscription/list.go | 2 +- exchanges/subscription/template_test.go | 2 +- 7 files changed, 24 insertions(+), 14 deletions(-) diff --git a/common/common.go b/common/common.go index 45d05c21695..191060c222e 100644 --- a/common/common.go +++ b/common/common.go @@ -15,6 +15,7 @@ import ( "path/filepath" "reflect" "regexp" + "slices" "strconv" "strings" "sync" @@ -666,3 +667,12 @@ func Batch[S ~[]E, E any](blobs S, batchSize int) (batches []S) { } return } + +// SortStrings takes a slice of fmt.Stringer implementers and returns a new sorted slice +func SortStrings[S ~[]E, E fmt.Stringer](x S) S { + n := slices.Clone(x) + slices.SortFunc(n, func(a, b E) int { + return strings.Compare(a.String(), b.String()) + }) + return n +} diff --git a/common/common_test.go b/common/common_test.go index 6f2df157715..27e114af653 100644 --- a/common/common_test.go +++ b/common/common_test.go @@ -866,3 +866,13 @@ func TestBatch(t *testing.T) { require.Len(t, b[1], 3) require.Len(t, b[2], 1) } + +type A int + +func (a A) String() string { + return strconv.Itoa(int(a)) +} + +func TestSortStrings(t *testing.T) { + assert.Equal(t, []A{1, 2, 5, 6}, SortStrings([]A{6, 2, 5, 1})) +} diff --git a/currency/pairs_test.go b/currency/pairs_test.go index c4510638963..086a3d26006 100644 --- a/currency/pairs_test.go +++ b/currency/pairs_test.go @@ -827,13 +827,3 @@ func TestPairsEqual(t *testing.T) { assert.Equal(t, "USDT-BTC", orig[0].String(), "Equal Pairs should not effect original order or format") assert.False(t, orig.Equal(Pairs{NewPair(DAI, XRP), NewPair(DAI, BTC), NewPair(USD, LTC)}), "UnEqual Pairs should return false") } - -// TestPairsSort exercises Pairs.Sort -func TestPairsSort(t *testing.T) { - t.Parallel() - p := Pairs{NewPair(LTC, USD), NewPair(USD, NZD), NewPair(BTC, USD), NewPair(USDT, XRP), NewPair(DAI, XRP)} - n := p.Sort() - exp := Pairs{NewPair(BTC, USD), NewPair(DAI, XRP), NewPair(LTC, USD), NewPair(USD, NZD), NewPair(USDT, XRP)} - assert.Equal(t, exp, n, "Pairs should be sorted") - assert.NotEqual(t, exp, p, "Original Pairs should not be sorted") -} diff --git a/exchanges/kucoin/kucoin_test.go b/exchanges/kucoin/kucoin_test.go index e46dbe4a090..41c0602c4bd 100644 --- a/exchanges/kucoin/kucoin_test.go +++ b/exchanges/kucoin/kucoin_test.go @@ -2045,7 +2045,7 @@ func TestGenerateSubscriptions(t *testing.T) { ku.Websocket.SetCanUseAuthenticatedEndpoints(true) var loanPairs currency.Pairs - loanCurrs := common.Sort(subPairs[0:8].GetCurrencies()) + loanCurrs := common.SortStrings(subPairs[0:8].GetCurrencies()) for _, c := range loanCurrs { loanPairs = append(loanPairs, currency.Pair{Base: c}) } diff --git a/exchanges/kucoin/kucoin_websocket.go b/exchanges/kucoin/kucoin_websocket.go index 09ed89fd38e..882c70f5c1f 100644 --- a/exchanges/kucoin/kucoin_websocket.go +++ b/exchanges/kucoin/kucoin_websocket.go @@ -1630,7 +1630,7 @@ func channelInterval(s *subscription.Subscription) string { // currencies returns the currencies from all pairs in an asset // Updates the AssetPairs map parameter to contain only those currencies as Base items func assetCurrencies(s *subscription.Subscription, ap map[asset.Item]currency.Pairs) currency.Currencies { - cs := common.Sort(ap[s.Asset].GetCurrencies()) + cs := common.SortStrings(ap[s.Asset].GetCurrencies()) p := currency.Pairs{} for _, c := range cs { p = append(p, currency.Pair{Base: c}) diff --git a/exchanges/subscription/list.go b/exchanges/subscription/list.go index 295776a42d5..f88a6cd2176 100644 --- a/exchanges/subscription/list.go +++ b/exchanges/subscription/list.go @@ -76,7 +76,7 @@ func fillAssetPairs(ap assetPairs, a asset.Item, e iExchange) error { if err != nil { return err } - ap[a] = p.Sort().Format(f) + ap[a] = common.SortStrings(p).Format(f) return nil } diff --git a/exchanges/subscription/template_test.go b/exchanges/subscription/template_test.go index 48856e2bd9d..cfa8e89c78c 100644 --- a/exchanges/subscription/template_test.go +++ b/exchanges/subscription/template_test.go @@ -55,7 +55,7 @@ func TestExpandTemplates(t *testing.T) { {Channel: "feature3", QualifiedChannel: "future-" + p.Swap().String() + "-feature3@100", Asset: asset.Futures, Pairs: currency.Pairs{p}, Levels: 100}, }...) } - for _, b := range common.Batch(pairs.Sort(), 3) { + for _, b := range common.Batch(common.SortStrings(pairs), 3) { exp = append(exp, &Subscription{Channel: "feature6", QualifiedChannel: "spot-" + b.Join() + "-feature6", Asset: asset.Spot, Pairs: b}) }