[#302] pkg/netmap: Convert nil `NodeInfo` and `NodeAttribute` to nil message

Document that `NodeAttribute.ToV2` and `NodeInfo.ToV2`
method return `nil` when called on `nil`. Document that
`NodeAttributeFromV2` and `NodeInfoFromV2` functions
return `nil` when called on `nil`. Write corresponding
unit tests.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
remotes/KirillovDenis/feature/refactor-sig-rpc
Pavel Karpy 2021-06-08 17:47:12 +03:00 committed by Alex Vanin
parent 19cf3c2d0b
commit 05f48d9394
2 changed files with 46 additions and 15 deletions

View File

@ -193,11 +193,15 @@ func NewNodeAttribute() *NodeAttribute {
}
// NodeAttributeFromV2 converts v2 node Attribute to NodeAttribute.
//
// Nil netmap.Attribute converts to nil.
func NewNodeAttributeFromV2(a *netmap.Attribute) *NodeAttribute {
return (*NodeAttribute)(a)
}
// ToV2 converts NodeAttribute to v2 node Attribute.
//
// Nil NodeAttribute converts to nil.
func (a *NodeAttribute) ToV2() *netmap.Attribute {
return (*netmap.Attribute)(a)
}
@ -276,11 +280,15 @@ func NewNodeInfo() *NodeInfo {
}
// NewNodeInfoFromV2 converts v2 NodeInfo to NodeInfo.
//
// Nil netmap.NodeInfo converts to nil.
func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo {
return (*NodeInfo)(i)
}
// ToV2 converts NodeInfo to v2 NodeInfo.
//
// Nil NodeInfo converts to nil.
func (i *NodeInfo) ToV2() *netmap.NodeInfo {
return (*netmap.NodeInfo)(i)
}

View File

@ -4,6 +4,7 @@ import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
testv2 "github.com/nspcc-dev/neofs-api-go/v2/netmap/test"
"github.com/stretchr/testify/require"
)
@ -31,14 +32,27 @@ func TestNodeStateFromV2(t *testing.T) {
}
func TestNodeAttributeFromV2(t *testing.T) {
aV2 := new(netmap.Attribute)
aV2.SetKey("key")
aV2.SetValue("value")
aV2.SetParents([]string{"par1", "par2"})
t.Run("from nil", func(t *testing.T) {
var x *netmap.Attribute
a := NewNodeAttributeFromV2(aV2)
require.Nil(t, NewNodeAttributeFromV2(x))
})
require.Equal(t, aV2, a.ToV2())
t.Run("from non-nil", func(t *testing.T) {
aV2 := testv2.GenerateAttribute(false)
a := NewNodeAttributeFromV2(aV2)
require.Equal(t, aV2, a.ToV2())
})
}
func TestNodeAttribute_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *NodeAttribute
require.Nil(t, x.ToV2())
})
}
func TestNodeAttribute_Key(t *testing.T) {
@ -78,18 +92,27 @@ func testNodeAttribute() *NodeAttribute {
}
func TestNodeInfoFromV2(t *testing.T) {
iV2 := new(netmap.NodeInfo)
iV2.SetPublicKey([]byte{1, 2, 3})
iV2.SetAddress("456")
iV2.SetState(netmap.Online)
iV2.SetAttributes([]*netmap.Attribute{
testNodeAttribute().ToV2(),
testNodeAttribute().ToV2(),
t.Run("from nil", func(t *testing.T) {
var x *netmap.NodeInfo
require.Nil(t, NewNodeInfoFromV2(x))
})
i := NewNodeInfoFromV2(iV2)
t.Run("from non-nil", func(t *testing.T) {
iV2 := testv2.GenerateNodeInfo(false)
require.Equal(t, iV2, i.ToV2())
i := NewNodeInfoFromV2(iV2)
require.Equal(t, iV2, i.ToV2())
})
}
func TestNodeInfo_ToV2(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var x *NodeInfo
require.Nil(t, x.ToV2())
})
}
func TestNodeInfo_PublicKey(t *testing.T) {