From bfdf8a452fdc63833c206fa24021bb3c0be0f6a9 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 11 Sep 2020 18:18:29 +0300 Subject: [PATCH] [#140] sdk/object: Implement ID comparison method Signed-off-by: Leonard Lyubich --- pkg/object/id.go | 9 +++++++++ pkg/object/id_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/pkg/object/id.go b/pkg/object/id.go index 7006d6f3..c7eb5736 100644 --- a/pkg/object/id.go +++ b/pkg/object/id.go @@ -1,6 +1,7 @@ package object import ( + "bytes" "crypto/sha256" "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[:]) } +// 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. func (id *ID) ToV2() *refs.ObjectID { return (*refs.ObjectID)(id) diff --git a/pkg/object/id_test.go b/pkg/object/id_test.go index e1ab7632..373fa01d 100644 --- a/pkg/object/id_test.go +++ b/pkg/object/id_test.go @@ -22,3 +22,19 @@ func TestIDV2(t *testing.T) { 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)) +}