From 12574469e5635a0c4766fce74403a325a8525b85 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 19:49:25 +0300 Subject: [PATCH] [#302] pkg/owner: 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/owner/id.go | 4 ++++ pkg/owner/id_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/owner/id.go b/pkg/owner/id.go index 2743b73..fadb818 100644 --- a/pkg/owner/id.go +++ b/pkg/owner/id.go @@ -16,6 +16,8 @@ type ID refs.OwnerID var errInvalidIDString = errors.New("incorrect format of the string owner ID") // NewIDFromV2 wraps v2 OwnerID message to ID. +// +// Nil refs.OwnerID converts to nil. func NewIDFromV2(idV2 *refs.OwnerID) *ID { return (*ID)(idV2) } @@ -33,6 +35,8 @@ func (id *ID) SetNeo3Wallet(v *NEO3Wallet) { } // ToV2 returns the v2 owner ID message. +// +// Nil ID converts to nil. func (id *ID) ToV2() *refs.OwnerID { return (*refs.OwnerID)(id) } diff --git a/pkg/owner/id_test.go b/pkg/owner/id_test.go index 9ab2e30..d32b895 100644 --- a/pkg/owner/id_test.go +++ b/pkg/owner/id_test.go @@ -7,6 +7,7 @@ import ( "github.com/mr-tron/base58" . "github.com/nspcc-dev/neofs-api-go/pkg/owner" ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test" + "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-crypto/test" "github.com/stretchr/testify/require" ) @@ -99,3 +100,19 @@ func TestID_Equal(t *testing.T) { ownertest.GenerateFromBytes(data3), )) } + +func TestNewIDFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *refs.OwnerID + + 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()) + }) +}