[#189] sdk/netmap: Implement NodeAttribute type

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-05 14:39:50 +03:00 committed by Alex Vanin
parent d4f5c27d47
commit 6347f846eb
2 changed files with 92 additions and 0 deletions

View file

@ -26,6 +26,9 @@ type (
// NodeState is an enumeration of various states of the NeoFS node. // NodeState is an enumeration of various states of the NeoFS node.
type NodeState uint32 type NodeState uint32
// NodeAttribute represents v2 compatible attribute of the NeoFS Storage Node.
type NodeAttribute netmap.Attribute
const ( const (
_ NodeState = iota _ NodeState = iota
@ -146,3 +149,54 @@ func (s NodeState) String() string {
return "ONLINE" return "ONLINE"
} }
} }
// NewNodeAttribute creates and returns new NodeAttribute instance.
func NewNodeAttribute() *NodeAttribute {
return NewNodeAttributeFromV2(new(netmap.Attribute))
}
// NodeAttributeFromV2 converts v2 node Attribute to NodeAttribute.
func NewNodeAttributeFromV2(a *netmap.Attribute) *NodeAttribute {
return (*NodeAttribute)(a)
}
// ToV2 converts NodeAttribute to v2 node Attribute.
func (a *NodeAttribute) ToV2() *netmap.Attribute {
return (*netmap.Attribute)(a)
}
// Key returns key to the node attribute.
func (a *NodeAttribute) Key() string {
return (*netmap.Attribute)(a).
GetKey()
}
// SetKey sets key to the node attribute.
func (a *NodeAttribute) SetKey(key string) {
(*netmap.Attribute)(a).
SetKey(key)
}
// Value returns value of the node attribute.
func (a *NodeAttribute) Value() string {
return (*netmap.Attribute)(a).
GetValue()
}
// SetValue sets value of the node attribute.
func (a *NodeAttribute) SetValue(val string) {
(*netmap.Attribute)(a).
SetValue(val)
}
// ParentKeys returns list of parent keys.
func (a *NodeAttribute) ParentKeys() []string {
return (*netmap.Attribute)(a).
GetParents()
}
// SetParentKeys sets list of parent keys.
func (a *NodeAttribute) SetParentKeys(keys ...string) {
(*netmap.Attribute)(a).
SetParents(keys)
}

View file

@ -42,3 +42,41 @@ func TestNodeStateFromV2(t *testing.T) {
require.Equal(t, item.sV2, item.s.ToV2()) require.Equal(t, item.sV2, item.s.ToV2())
} }
} }
func TestNodeAttributeFromV2(t *testing.T) {
aV2 := new(netmap.Attribute)
aV2.SetKey("key")
aV2.SetValue("value")
aV2.SetParents([]string{"par1", "par2"})
a := NewNodeAttributeFromV2(aV2)
require.Equal(t, aV2, a.ToV2())
}
func TestNodeAttribute_Key(t *testing.T) {
a := NewNodeAttribute()
key := "some key"
a.SetKey(key)
require.Equal(t, key, a.Key())
}
func TestNodeAttribute_Value(t *testing.T) {
a := NewNodeAttribute()
val := "some value"
a.SetValue(val)
require.Equal(t, val, a.Value())
}
func TestNodeAttribute_ParentKeys(t *testing.T) {
a := NewNodeAttribute()
keys := []string{"par1", "par2"}
a.SetParentKeys(keys...)
require.Equal(t, keys, a.ParentKeys())
}