Skip to content

Commit

Permalink
Common: Add common.SortStrings for stringers
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Jul 12, 2024
1 parent 3866f08 commit b0133ff
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
10 changes: 10 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path/filepath"
"reflect"
"regexp"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
}
10 changes: 0 additions & 10 deletions currency/pairs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion exchanges/kucoin/kucoin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down
2 changes: 1 addition & 1 deletion exchanges/kucoin/kucoin_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,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})
Expand Down
2 changes: 1 addition & 1 deletion exchanges/subscription/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion exchanges/subscription/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}

Expand Down

0 comments on commit b0133ff

Please sign in to comment.