From 245a55f7152ae10d6d17534c3b6387032caef053 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 18:58:12 +0300 Subject: [PATCH] [#302] pkg/object: Convert nil `ID` to nil message Document that `ID.ToV2` method return `nil` when called on `nil`. Document that `NewIDFromV2` function return `nil` when called on `nil`. Write corresponding unit tests. Signed-off-by: Pavel Karpy --- pkg/object/id.go | 4 ++++ pkg/object/id_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/object/id.go b/pkg/object/id.go index 21d566e9..e4871a93 100644 --- a/pkg/object/id.go +++ b/pkg/object/id.go @@ -16,6 +16,8 @@ type ID refs.ObjectID var errInvalidIDString = errors.New("incorrect format of the string object ID") // NewIDFromV2 wraps v2 ObjectID message to ID. +// +// Nil refs.ObjectID converts to nil. func NewIDFromV2(idV2 *refs.ObjectID) *ID { return (*ID)(idV2) } @@ -41,6 +43,8 @@ func (id *ID) Equal(id2 *ID) bool { } // ToV2 converts ID to v2 ObjectID message. +// +// Nil ID converts to nil. 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 a11e80b1..e5e80db2 100644 --- a/pkg/object/id_test.go +++ b/pkg/object/id_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/mr-tron/base58" + "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/stretchr/testify/require" ) @@ -107,3 +108,19 @@ func TestObjectIDEncoding(t *testing.T) { require.Equal(t, id, a2) }) } + +func TestNewIDFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *refs.ObjectID + + require.Nil(t, NewIDFromV2(x)) + }) +} + +func TestID_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *ID + + require.Nil(t, x.ToV2()) + }) +}