Skip to content

Commit b6700c9

Browse files
Update slices dependency to use Compare (#2424)
Co-authored-by: James Walker <jim.walker@smartcontract.com>
1 parent 5d9e482 commit b6700c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+390
-360
lines changed

api/metrics/multi_gatherer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
dto "github.com/prometheus/client_model/go"
1414

1515
"golang.org/x/exp/slices"
16+
17+
"github.com/ava-labs/avalanchego/utils"
1618
)
1719

1820
var (
@@ -91,7 +93,7 @@ func (g *multiGatherer) Register(namespace string, gatherer prometheus.Gatherer)
9193
}
9294

9395
func sortMetrics(m []*dto.MetricFamily) {
94-
slices.SortFunc(m, func(i, j *dto.MetricFamily) bool {
95-
return *i.Name < *j.Name
96+
slices.SortFunc(m, func(i, j *dto.MetricFamily) int {
97+
return utils.Compare(*i.Name, *j.Name)
9698
})
9799
}

chains/atomic/state.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"errors"
99
"fmt"
1010

11+
"golang.org/x/exp/slices"
12+
1113
"github.com/ava-labs/avalanchego/database"
1214
"github.com/ava-labs/avalanchego/database/linkeddb"
1315
"github.com/ava-labs/avalanchego/database/prefixdb"
1416
"github.com/ava-labs/avalanchego/ids"
15-
"github.com/ava-labs/avalanchego/utils"
1617
"github.com/ava-labs/avalanchego/utils/hashing"
1718
"github.com/ava-labs/avalanchego/utils/set"
1819
)
@@ -207,7 +208,7 @@ func (s *state) getKeys(traits [][]byte, startTrait, startKey []byte, limit int)
207208
lastKey := startKey
208209
// Iterate over the traits in order appending all of the keys that possess
209210
// the given [traits].
210-
utils.SortBytes(traits)
211+
slices.SortFunc(traits, bytes.Compare)
211212
for _, trait := range traits {
212213
switch bytes.Compare(trait, startTrait) {
213214
case -1:

codec/reflectcodec/type_codec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,10 @@ func (c *genericCodec) marshal(
490490
endOffset = p.Offset
491491
}
492492

493-
slices.SortFunc(sortedKeys, func(a, b keyTuple) bool {
493+
slices.SortFunc(sortedKeys, func(a, b keyTuple) int {
494494
aBytes := p.Bytes[a.startIndex:a.endIndex]
495495
bBytes := p.Bytes[b.startIndex:b.endIndex]
496-
return bytes.Compare(aBytes, bBytes) < 0
496+
return bytes.Compare(aBytes, bBytes)
497497
})
498498

499499
allKeyBytes := slices.Clone(p.Bytes[startOffset:p.Offset])

genesis/config.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ func (a Allocation) Unparse(networkID uint32) (UnparsedAllocation, error) {
5353
return ua, err
5454
}
5555

56-
func (a Allocation) Less(other Allocation) bool {
57-
return a.InitialAmount < other.InitialAmount ||
58-
(a.InitialAmount == other.InitialAmount && a.AVAXAddr.Less(other.AVAXAddr))
56+
func (a Allocation) Compare(other Allocation) int {
57+
if amountCmp := utils.Compare(a.InitialAmount, other.InitialAmount); amountCmp != 0 {
58+
return amountCmp
59+
}
60+
return a.AVAXAddr.Compare(other.AVAXAddr)
5961
}
6062

6163
type Staker struct {

genesis/config_test.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,43 @@ import (
1111
"github.com/ava-labs/avalanchego/ids"
1212
)
1313

14-
func TestAllocationLess(t *testing.T) {
14+
func TestAllocationCompare(t *testing.T) {
1515
type test struct {
1616
name string
1717
alloc1 Allocation
1818
alloc2 Allocation
19-
expected bool
19+
expected int
2020
}
2121
tests := []test{
2222
{
2323
name: "equal",
2424
alloc1: Allocation{},
2525
alloc2: Allocation{},
26-
expected: false,
26+
expected: 0,
2727
},
2828
{
29-
name: "first initial amount smaller",
29+
name: "initial amount smaller",
3030
alloc1: Allocation{},
3131
alloc2: Allocation{
3232
InitialAmount: 1,
3333
},
34-
expected: true,
34+
expected: -1,
3535
},
3636
{
37-
name: "first initial amount larger",
38-
alloc1: Allocation{
39-
InitialAmount: 1,
40-
},
41-
alloc2: Allocation{},
42-
expected: false,
43-
},
44-
{
45-
name: "first bytes smaller",
37+
name: "bytes smaller",
4638
alloc1: Allocation{},
4739
alloc2: Allocation{
4840
AVAXAddr: ids.ShortID{1},
4941
},
50-
expected: true,
51-
},
52-
{
53-
name: "first bytes larger",
54-
alloc1: Allocation{
55-
AVAXAddr: ids.ShortID{1},
56-
},
57-
alloc2: Allocation{},
58-
expected: false,
42+
expected: -1,
5943
},
6044
}
6145
for _, tt := range tests {
6246
t.Run(tt.name, func(t *testing.T) {
63-
require.Equal(t, tt.expected, tt.alloc1.Less(tt.alloc2))
47+
require := require.New(t)
48+
49+
require.Equal(tt.expected, tt.alloc1.Compare(tt.alloc2))
50+
require.Equal(-tt.expected, tt.alloc2.Compare(tt.alloc1))
6451
})
6552
}
6653
}

go.mod

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/DataDog/zstd v1.5.2
1212
github.com/Microsoft/go-winio v0.5.2
1313
github.com/NYTimes/gziphandler v1.1.1
14-
github.com/ava-labs/coreth v0.12.9-rc.9
14+
github.com/ava-labs/coreth v0.12.10-rc.0
1515
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34
1616
github.com/btcsuite/btcd/btcutil v1.1.3
1717
github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811
@@ -58,11 +58,11 @@ require (
5858
go.uber.org/goleak v1.2.1
5959
go.uber.org/mock v0.2.0
6060
go.uber.org/zap v1.26.0
61-
golang.org/x/crypto v0.14.0
62-
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
63-
golang.org/x/net v0.17.0
64-
golang.org/x/sync v0.4.0
65-
golang.org/x/term v0.13.0
61+
golang.org/x/crypto v0.16.0
62+
golang.org/x/exp v0.0.0-20231127185646-65229373498e
63+
golang.org/x/net v0.19.0
64+
golang.org/x/sync v0.5.0
65+
golang.org/x/term v0.15.0
6666
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af
6767
gonum.org/v1/gonum v0.11.0
6868
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98
@@ -146,9 +146,9 @@ require (
146146
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.0 // indirect
147147
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
148148
go.uber.org/multierr v1.10.0 // indirect
149-
golang.org/x/sys v0.14.0 // indirect
150-
golang.org/x/text v0.13.0 // indirect
151-
golang.org/x/tools v0.14.0 // indirect
149+
golang.org/x/sys v0.15.0 // indirect
150+
golang.org/x/text v0.14.0 // indirect
151+
golang.org/x/tools v0.16.0 // indirect
152152
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect
153153
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
154154
gopkg.in/ini.v1 v1.67.0 // indirect

go.sum

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156 h1:eMwmnE/GDgah
6666
github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
6767
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
6868
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
69-
github.com/ava-labs/coreth v0.12.9-rc.9 h1:mvYxABdyPByXwwwIxnTBCiNO23dsE1Kfnd5H106lric=
70-
github.com/ava-labs/coreth v0.12.9-rc.9/go.mod h1:yrf2vEah4Fgj6sJ4UpHewo4DLolwdpf2bJuLRT80PGw=
69+
github.com/ava-labs/coreth v0.12.10-rc.0 h1:qmuom7rtH5hc1E3lnqrMFNLFL1TMnEVa/2O8poB1YLU=
70+
github.com/ava-labs/coreth v0.12.10-rc.0/go.mod h1:plFm/xzvWmx1+qJ3JQSTzF8+FdaA2xu7GgY/AdaZDfk=
7171
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34 h1:mg9Uw6oZFJKytJxgxnl3uxZOs/SB8CVHg6Io4Tf99Zc=
7272
github.com/ava-labs/ledger-avalanche/go v0.0.0-20231102202641-ae2ebdaeac34/go.mod h1:pJxaT9bUgeRNVmNRgtCHb7sFDIRKy7CzTQVi8gGNT6g=
7373
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
@@ -696,8 +696,8 @@ golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm
696696
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
697697
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
698698
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
699-
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
700-
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
699+
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
700+
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
701701
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
702702
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
703703
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -708,8 +708,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
708708
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
709709
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
710710
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
711-
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
712-
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
711+
golang.org/x/exp v0.0.0-20231127185646-65229373498e h1:Gvh4YaCaXNs6dKTlfgismwWZKyjVZXwOPfIyUaqU3No=
712+
golang.org/x/exp v0.0.0-20231127185646-65229373498e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
713713
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
714714
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
715715
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -736,7 +736,7 @@ golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
736736
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
737737
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
738738
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
739-
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
739+
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
740740
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
741741
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
742742
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -783,8 +783,8 @@ golang.org/x/net v0.0.0-20211008194852-3b03d305991f/go.mod h1:9nx3DQGgdP8bBQD5qx
783783
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
784784
golang.org/x/net v0.0.0-20220607020251-c690dde0001d/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
785785
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
786-
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
787-
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
786+
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
787+
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
788788
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
789789
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
790790
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -807,8 +807,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
807807
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
808808
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
809809
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
810-
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
811-
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
810+
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
811+
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
812812
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
813813
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
814814
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -878,12 +878,12 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
878878
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
879879
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
880880
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
881-
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
882-
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
881+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
882+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
883883
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
884884
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
885-
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
886-
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
885+
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
886+
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
887887
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
888888
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
889889
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -894,8 +894,8 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
894894
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
895895
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
896896
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
897-
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
898-
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
897+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
898+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
899899
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
900900
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
901901
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -958,8 +958,8 @@ golang.org/x/tools v0.0.0-20210108195828-e2f9c7f1fc8e/go.mod h1:emZCQorbCU4vsT4f
958958
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
959959
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
960960
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
961-
golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc=
962-
golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg=
961+
golang.org/x/tools v0.16.0 h1:GO788SKMRunPIBCXiQyo2AaexLstOrVhuAL5YwsckQM=
962+
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
963963
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
964964
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
965965
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

ids/id.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,6 @@ func (id ID) MarshalText() ([]byte, error) {
145145
return []byte(id.String()), nil
146146
}
147147

148-
func (id ID) Less(other ID) bool {
149-
return bytes.Compare(id[:], other[:]) < 0
148+
func (id ID) Compare(other ID) int {
149+
return bytes.Compare(id[:], other[:])
150150
}

ids/id_test.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package ids
55

66
import (
77
"encoding/json"
8+
"fmt"
89
"testing"
910

1011
"github.com/stretchr/testify/require"
@@ -200,26 +201,34 @@ func TestIDMapMarshalling(t *testing.T) {
200201
require.Equal(originalMap, unmarshalledMap)
201202
}
202203

203-
func TestIDLess(t *testing.T) {
204-
require := require.New(t)
204+
func TestIDCompare(t *testing.T) {
205+
tests := []struct {
206+
a ID
207+
b ID
208+
expected int
209+
}{
210+
{
211+
a: ID{1},
212+
b: ID{0},
213+
expected: 1,
214+
},
215+
{
216+
a: ID{1},
217+
b: ID{1},
218+
expected: 0,
219+
},
220+
{
221+
a: ID{1, 0},
222+
b: ID{1, 2},
223+
expected: -1,
224+
},
225+
}
226+
for _, test := range tests {
227+
t.Run(fmt.Sprintf("%s_%s_%d", test.a, test.b, test.expected), func(t *testing.T) {
228+
require := require.New(t)
205229

206-
id1 := ID{}
207-
id2 := ID{}
208-
require.False(id1.Less(id2))
209-
require.False(id2.Less(id1))
210-
211-
id1 = ID{1}
212-
id2 = ID{0}
213-
require.False(id1.Less(id2))
214-
require.True(id2.Less(id1))
215-
216-
id1 = ID{1}
217-
id2 = ID{1}
218-
require.False(id1.Less(id2))
219-
require.False(id2.Less(id1))
220-
221-
id1 = ID{1, 0}
222-
id2 = ID{1, 2}
223-
require.True(id1.Less(id2))
224-
require.False(id2.Less(id1))
230+
require.Equal(test.expected, test.a.Compare(test.b))
231+
require.Equal(-test.expected, test.b.Compare(test.a))
232+
})
233+
}
225234
}

ids/node_id.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ func (id *NodeID) UnmarshalText(text []byte) error {
6666
return id.UnmarshalJSON(text)
6767
}
6868

69-
func (id NodeID) Less(other NodeID) bool {
70-
return bytes.Compare(id[:], other[:]) == -1
69+
func (id NodeID) Compare(other NodeID) int {
70+
return bytes.Compare(id[:], other[:])
7171
}
7272

7373
// ToNodeID attempt to convert a byte slice into a node id

0 commit comments

Comments
 (0)