From c54dee82be899aded5579b202c3a45248cbc105a Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 8 Jun 2021 15:19:51 +0300 Subject: [PATCH] [#302] pkg/container: Convert nil `UsedSpaceAnnouncement` to nil message Document that `UsedSpaceAnnouncement.ToV2` method returns `nil` when is called on `nil`. Document that `NewAnnouncementFromV2` method returns `nil` when is called on `nil`. Add corresponding unit test. Signed-off-by: Pavel Karpy --- pkg/container/announcement.go | 4 ++++ pkg/container/announcement_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/container/announcement.go b/pkg/container/announcement.go index 6d82966e..20041aa4 100644 --- a/pkg/container/announcement.go +++ b/pkg/container/announcement.go @@ -16,6 +16,8 @@ func NewAnnouncement() *UsedSpaceAnnouncement { // NewAnnouncementFromV2 wraps protocol dependent version of // UsedSpaceAnnouncement message. +// +// Nil container.UsedSpaceAnnouncement converts to nil. func NewAnnouncementFromV2(v *container.UsedSpaceAnnouncement) *UsedSpaceAnnouncement { return (*UsedSpaceAnnouncement)(v) } @@ -53,6 +55,8 @@ func (a *UsedSpaceAnnouncement) SetUsedSpace(value uint64) { } // ToV2 returns protocol dependent version of UsedSpaceAnnouncement message. +// +// Nil UsedSpaceAnnouncement converts to nil. func (a *UsedSpaceAnnouncement) ToV2() *container.UsedSpaceAnnouncement { return (*container.UsedSpaceAnnouncement)(a) } diff --git a/pkg/container/announcement_test.go b/pkg/container/announcement_test.go index 26164bb3..3560ae6a 100644 --- a/pkg/container/announcement_test.go +++ b/pkg/container/announcement_test.go @@ -8,6 +8,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" containertest "github.com/nspcc-dev/neofs-api-go/pkg/container/test" + containerv2 "github.com/nspcc-dev/neofs-api-go/v2/container" "github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/stretchr/testify/require" ) @@ -64,3 +65,19 @@ func TestUsedSpaceEncoding(t *testing.T) { require.Equal(t, a, a2) }) } + +func TestUsedSpaceAnnouncement_ToV2(t *testing.T) { + t.Run("nil", func(t *testing.T) { + var x *container.UsedSpaceAnnouncement + + require.Nil(t, x.ToV2()) + }) +} + +func TestNewAnnouncementFromV2(t *testing.T) { + t.Run("from nil", func(t *testing.T) { + var x *containerv2.UsedSpaceAnnouncement + + require.Nil(t, container.NewAnnouncementFromV2(x)) + }) +}