From 6ce8844654ebf2e43681ebdb321d1ba76d88aa46 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 15:07:00 +0300 Subject: [PATCH] [#302] pkg/container/id: Convert nil `ID` to nil message Document that `ID.ToV2` method returns `nil` when is called on `nil`. Document that `NewFromV2` method returns `nil` when is called on `nil`. Add corresponding unit test. Signed-off-by: Pavel Karpy --- pkg/container/id/id.go | 4 ++++ pkg/container/id/id_test.go | 26 ++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/container/id/id.go b/pkg/container/id/id.go index 46b8d371..b09c7b33 100644 --- a/pkg/container/id/id.go +++ b/pkg/container/id/id.go @@ -13,6 +13,8 @@ import ( type ID refs.ContainerID // NewFromV2 wraps v2 ContainerID message to ID. +// +// Nil refs.ContainerID converts to nil. func NewFromV2(idV2 *refs.ContainerID) *ID { return (*ID)(idV2) } @@ -28,6 +30,8 @@ func (id *ID) SetSHA256(v [sha256.Size]byte) { } // ToV2 returns the v2 container ID message. +// +// Nil Result converts to nil. func (id *ID) ToV2() *refs.ContainerID { return (*refs.ContainerID)(id) } diff --git a/pkg/container/id/id_test.go b/pkg/container/id/id_test.go index 6aeca3c8..e0a266ee 100644 --- a/pkg/container/id/id_test.go +++ b/pkg/container/id/id_test.go @@ -7,6 +7,7 @@ import ( cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id" cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test" + "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/stretchr/testify/require" ) @@ -16,13 +17,22 @@ func randSHA256Checksum() (cs [sha256.Size]byte) { } func TestID_ToV2(t *testing.T) { - checksum := randSHA256Checksum() + t.Run("non-nil", func(t *testing.T) { + checksum := randSHA256Checksum() - id := cidtest.GenerateWithChecksum(checksum) + id := cidtest.GenerateWithChecksum(checksum) - idV2 := id.ToV2() + idV2 := id.ToV2() - require.Equal(t, id, cid.NewFromV2(idV2)) + require.Equal(t, id, cid.NewFromV2(idV2)) + require.Equal(t, checksum[:], idV2.GetValue()) + }) + + t.Run("nil", func(t *testing.T) { + var x *cid.ID + + require.Nil(t, x.ToV2()) + }) } func TestID_Equal(t *testing.T) { @@ -69,3 +79,11 @@ func TestContainerIDEncoding(t *testing.T) { require.Equal(t, id, a2) }) } + +func TestNewFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *refs.ContainerID + + require.Nil(t, cid.NewFromV2(x)) + }) +}