[#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>
This commit is contained in:
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. // NodeAttributeFromV2 converts v2 node Attribute to NodeAttribute.
//
// Nil netmap.Attribute converts to nil.
func NewNodeAttributeFromV2(a *netmap.Attribute) *NodeAttribute { func NewNodeAttributeFromV2(a *netmap.Attribute) *NodeAttribute {
return (*NodeAttribute)(a) return (*NodeAttribute)(a)
} }
// ToV2 converts NodeAttribute to v2 node Attribute. // ToV2 converts NodeAttribute to v2 node Attribute.
//
// Nil NodeAttribute converts to nil.
func (a *NodeAttribute) ToV2() *netmap.Attribute { func (a *NodeAttribute) ToV2() *netmap.Attribute {
return (*netmap.Attribute)(a) return (*netmap.Attribute)(a)
} }
@ -276,11 +280,15 @@ func NewNodeInfo() *NodeInfo {
} }
// NewNodeInfoFromV2 converts v2 NodeInfo to NodeInfo. // NewNodeInfoFromV2 converts v2 NodeInfo to NodeInfo.
//
// Nil netmap.NodeInfo converts to nil.
func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo { func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo {
return (*NodeInfo)(i) return (*NodeInfo)(i)
} }
// ToV2 converts NodeInfo to v2 NodeInfo. // ToV2 converts NodeInfo to v2 NodeInfo.
//
// Nil NodeInfo converts to nil.
func (i *NodeInfo) ToV2() *netmap.NodeInfo { func (i *NodeInfo) ToV2() *netmap.NodeInfo {
return (*netmap.NodeInfo)(i) return (*netmap.NodeInfo)(i)
} }

View file

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