[#140] sdk/object: Implement ID comparison method

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-09-11 18:18:29 +03:00 committed by Stanislav Bogatyrev
parent 5473b4ef95
commit bfdf8a452f
2 changed files with 25 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package object package object
import ( import (
"bytes"
"crypto/sha256" "crypto/sha256"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
@ -26,6 +27,14 @@ func (id *ID) SetSHA256(v [sha256.Size]byte) {
(*refs.ObjectID)(id).SetValue(v[:]) (*refs.ObjectID)(id).SetValue(v[:])
} }
// Equal returns true if identifiers are identical.
func (id *ID) Equal(id2 *ID) bool {
return bytes.Equal(
(*ID)(id).ToV2().GetValue(),
(*ID)(id2).ToV2().GetValue(),
)
}
// ToV2 converts ID to v2 ObjectID message. // ToV2 converts ID to v2 ObjectID message.
func (id *ID) ToV2() *refs.ObjectID { func (id *ID) ToV2() *refs.ObjectID {
return (*refs.ObjectID)(id) return (*refs.ObjectID)(id)

View file

@ -22,3 +22,19 @@ func TestIDV2(t *testing.T) {
require.Equal(t, checksum[:], idV2.GetValue()) require.Equal(t, checksum[:], idV2.GetValue())
} }
func TestID_Equal(t *testing.T) {
cs := randSHA256Checksum(t)
id1 := NewID()
id1.SetSHA256(cs)
id2 := NewID()
id2.SetSHA256(cs)
id3 := NewID()
id3.SetSHA256(randSHA256Checksum(t))
require.True(t, id1.Equal(id2))
require.False(t, id1.Equal(id3))
}