[#302] pkg/netmap: Document default values set in NewNodeInfo and NewNodeAttribute

Document field values of instance constructed via `NewNodeInfo` and `NewNodeAttribute`.
Assert the values in corresponding unit test.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-06-08 18:06:03 +03:00 committed by Alex Vanin
parent 05f48d9394
commit df4d610fad
2 changed files with 57 additions and 0 deletions

View file

@ -188,6 +188,11 @@ func (s NodeState) String() string {
}
// NewNodeAttribute creates and returns new NodeAttribute instance.
//
// Defaults:
// - key: "";
// - value: "";
// - parents: nil.
func NewNodeAttribute() *NodeAttribute {
return NewNodeAttributeFromV2(new(netmap.Attribute))
}
@ -275,6 +280,12 @@ func (a *NodeAttribute) UnmarshalJSON(data []byte) error {
}
// NewNodeInfo creates and returns new NodeInfo instance.
//
// Defaults:
// - publicKey: nil;
// - address: "";
// - attributes nil;
// - state: 0.
func NewNodeInfo() *NodeInfo {
return NewNodeInfoFromV2(new(netmap.NodeInfo))
}
@ -319,9 +330,17 @@ func (i *NodeInfo) SetAddress(addr string) {
// Attributes returns list of the node attributes.
func (i *NodeInfo) Attributes() []*NodeAttribute {
if i == nil {
return nil
}
as := (*netmap.NodeInfo)(i).
GetAttributes()
if as == nil {
return nil
}
res := make([]*NodeAttribute, 0, len(as))
for i := range as {

View file

@ -202,3 +202,41 @@ func TestNodeInfoEncoding(t *testing.T) {
require.Equal(t, i, i2)
})
}
func TestNewNodeAttribute(t *testing.T) {
t.Run("default values", func(t *testing.T) {
attr := NewNodeAttribute()
// check initial values
require.Empty(t, attr.Key())
require.Empty(t, attr.Value())
require.Nil(t, attr.ParentKeys())
// convert to v2 message
attrV2 := attr.ToV2()
require.Empty(t, attrV2.GetKey())
require.Empty(t, attrV2.GetValue())
require.Nil(t, attrV2.GetParents())
})
}
func TestNewNodeInfo(t *testing.T) {
t.Run("default values", func(t *testing.T) {
ni := NewNodeInfo()
// check initial values
require.Nil(t, ni.PublicKey())
require.Empty(t, ni.Address())
require.Nil(t, ni.Attributes())
require.Zero(t, ni.State())
// convert to v2 message
niV2 := ni.ToV2()
require.Nil(t, niV2.GetPublicKey())
require.Empty(t, niV2.GetAddress())
require.Nil(t, niV2.GetAttributes())
require.EqualValues(t, netmap.UnspecifiedState, niV2.GetState())
})
}