Implement Cmp() functions for ID-like structs #333
6 changed files with 67 additions and 0 deletions
|
@ -3,6 +3,7 @@ package cid
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
"github.com/mr-tron/base58"
|
"github.com/mr-tron/base58"
|
||||||
|
@ -113,3 +114,9 @@ func (id *ID) DecodeString(s string) error {
|
||||||
func (id ID) String() string {
|
func (id ID) String() string {
|
||||||
return id.EncodeToString()
|
return id.EncodeToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cmp returns an integer comparing two base58 encoded container ID lexicographically.
|
||||||
|
// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2.
|
||||||
|
func (id ID) Cmp(id2 ID) int {
|
||||||
|
|||||||
|
return strings.Compare(id.EncodeToString(), id2.EncodeToString())
|
||||||
a-savchuk marked this conversation as resolved
Outdated
a-savchuk
commented
Is there any reason to use Is there any reason to use `bytes.Compare` instead of `strings.Compare`?
|
|||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package cid_test
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
|
@ -106,3 +108,17 @@ func TestID_Encode(t *testing.T) {
|
||||||
require.Equal(t, emptyID, id.EncodeToString())
|
require.Equal(t, emptyID, id.EncodeToString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestID_Cmp(t *testing.T) {
|
||||||
dstepanov-yadro
commented
`TestID__Cmp` -> `TestID_Cmp` ?
|
|||||||
|
var arr []cid.ID
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
checksum := randSHA256Checksum()
|
||||||
|
arr = append(arr, cidtest.IDWithChecksum(checksum))
|
||||||
|
}
|
||||||
|
|
||||||
|
slices.SortFunc(arr, cid.ID.Cmp)
|
||||||
|
|
||||||
|
for i := 1; i < len(arr); i++ {
|
||||||
|
require.NotEqual(t, strings.Compare(arr[i-1].EncodeToString(), arr[i].EncodeToString()), 1, "array is not sorted correctly")
|
||||||
a-savchuk marked this conversation as resolved
Outdated
a-savchuk
commented
Can you use Can you use `testify` library here?
a-savchuk marked this conversation as resolved
Outdated
a-savchuk
commented
array IS array IS
|
|||||||
|
}
|
||||||
a-savchuk marked this conversation as resolved
Outdated
a-savchuk
commented
"arrayS". How many? "arrayS". How many?
a-savchuk
commented
`Fatalf` -> `Fatal`
|
|||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
||||||
|
@ -167,3 +168,9 @@ func (id *ID) UnmarshalJSON(data []byte) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cmp returns an integer comparing two base58 encoded object ID lexicographically.
|
||||||
|
// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2.
|
||||||
|
func (id ID) Cmp(id2 ID) int {
|
||||||
|
return strings.Compare(id.EncodeToString(), id2.EncodeToString())
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@ package oid
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
|
@ -180,3 +182,16 @@ func TestID_Encode(t *testing.T) {
|
||||||
require.Equal(t, emptyID, id.EncodeToString())
|
require.Equal(t, emptyID, id.EncodeToString())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestID_Cmp(t *testing.T) {
|
||||||
|
id1 := randID(t)
|
||||||
|
id2 := randID(t)
|
||||||
|
id3 := randID(t)
|
||||||
|
|
||||||
|
arr := []ID{id1, id2, id3}
|
||||||
|
|
||||||
|
slices.SortFunc(arr, ID.Cmp)
|
||||||
|
for i := 1; i < len(arr); i++ {
|
||||||
|
require.NotEqual(t, strings.Compare(arr[i-1].EncodeToString(), arr[i].EncodeToString()), 1, "array is not sorted correctly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
"github.com/mr-tron/base58"
|
"github.com/mr-tron/base58"
|
||||||
|
@ -123,3 +124,9 @@ func (x *ID) setUserID(w []byte) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cmp returns an integer comparing two base58 encoded user ID lexicographically.
|
||||||
|
// The result will be 0 if id1 == id2, -1 if id1 < id2, and +1 if id1 > id2.
|
||||||
|
func (x ID) Cmp(x2 ID) int {
|
||||||
|
return strings.Compare(x.EncodeToString(), x2.EncodeToString())
|
||||||
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package user_test
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
||||||
|
@ -132,3 +134,16 @@ func TestID_Equal(t *testing.T) {
|
||||||
require.True(t, id3.Equals(id1)) // commutativity
|
require.True(t, id3.Equals(id1)) // commutativity
|
||||||
require.False(t, id1.Equals(id2))
|
require.False(t, id1.Equals(id2))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestID_Cmp(t *testing.T) {
|
||||||
|
id1 := usertest.ID()
|
||||||
|
id2 := usertest.ID()
|
||||||
|
id3 := usertest.ID()
|
||||||
|
|
||||||
|
arr := []ID{id1, id2, id3}
|
||||||
|
|
||||||
|
slices.SortFunc(arr, ID.Cmp)
|
||||||
|
for i := 1; i < len(arr); i++ {
|
||||||
|
require.NotEqual(t, strings.Compare(arr[i-1].EncodeToString(), arr[i].EncodeToString()), 1, "array is not sorted correctly")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue
func Cmp(id1, id2 ID) int
?SortFunc(ids, cid.Cmp)
will look better thanSortFunc(ids, (cid.ID).Cmp)
a
->id1
,b
->id2
Compare
similar to how it's done in the standard library?I think we should use NeoGo code style.
Disagree but approve