[#302] pkg/storagegroup: Convert nil StorageGroup to nil message

Document that `StorageGroup.ToV2` method return `nil`
when called on `nil`. Document that `NewFromV2`
function return `nil` when called on `nil`. Write
corresponding unit tests.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 20:39:26 +03:00 committed by Alex Vanin
parent fb77bd3511
commit 934cf8ecc8
2 changed files with 21 additions and 0 deletions

View file

@ -11,6 +11,8 @@ import (
type StorageGroup storagegroup.StorageGroup
// NewFromV2 wraps v2 StorageGroup message to StorageGroup.
//
// Nil storagegroup.StorageGroup converts to nil.
func NewFromV2(aV2 *storagegroup.StorageGroup) *StorageGroup {
return (*StorageGroup)(aV2)
}
@ -110,6 +112,8 @@ func (sg *StorageGroup) SetMembers(members []*object.ID) {
}
// ToV2 converts StorageGroup to v2 StorageGroup message.
//
// Nil StorageGroup converts to nil.
func (sg *StorageGroup) ToV2() *storagegroup.StorageGroup {
return (*storagegroup.StorageGroup)(sg)
}

View file

@ -8,6 +8,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/storagegroup"
storagegrouptest "github.com/nspcc-dev/neofs-api-go/pkg/storagegroup/test"
refstest "github.com/nspcc-dev/neofs-api-go/pkg/test"
storagegroupV2 "github.com/nspcc-dev/neofs-api-go/v2/storagegroup"
"github.com/stretchr/testify/require"
)
@ -54,3 +55,19 @@ func TestStorageGroupEncoding(t *testing.T) {
require.Equal(t, sg, sg2)
})
}
func TestNewFromV2(t *testing.T) {
t.Run("from nil", func(t *testing.T) {
var x *storagegroupV2.StorageGroup
require.Nil(t, storagegroup.NewFromV2(x))
})
}
func TestStorageGroup_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *storagegroup.StorageGroup
require.Nil(t, x.ToV2())
})
}