[#189] sdk/netmap: Implement NodeInfo JSON encode/decode functions

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-05 15:20:07 +03:00 committed by Alex Vanin
parent 8367b498d9
commit 40d7c9cd93
2 changed files with 31 additions and 0 deletions

View file

@ -214,6 +214,21 @@ func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo {
return (*NodeInfo)(i) return (*NodeInfo)(i)
} }
// NodeInfoToJSON encodes NodeInfo to JSON format.
func NodeInfoToJSON(i *NodeInfo) ([]byte, error) {
return netmap.NodeInfoToJSON(i.ToV2())
}
// NodeInfoFromJSON decodes NodeInfo from JSON-encoded data.
func NodeInfoFromJSON(data []byte) (*NodeInfo, error) {
i, err := netmap.NodeInfoFromJSON(data)
if err != nil {
return nil, err
}
return NewNodeInfoFromV2(i), nil
}
// ToV2 converts NodeInfo to v2 NodeInfo. // ToV2 converts NodeInfo to v2 NodeInfo.
func (i *NodeInfo) ToV2() *netmap.NodeInfo { func (i *NodeInfo) ToV2() *netmap.NodeInfo {
return (*netmap.NodeInfo)(i) return (*netmap.NodeInfo)(i)

View file

@ -140,3 +140,19 @@ func TestNodeInfo_Attributes(t *testing.T) {
require.Equal(t, as, i.Attributes()) require.Equal(t, as, i.Attributes())
} }
func TestNodeInfoJSON(t *testing.T) {
i := NewNodeInfo()
i.SetPublicKey([]byte{1, 2, 3})
i.SetAddress("some node address")
i.SetState(NodeStateOnline)
i.SetAttributes(testNodeAttribute(), testNodeAttribute())
j, err := NodeInfoToJSON(i)
require.NoError(t, err)
i2, err := NodeInfoFromJSON(j)
require.NoError(t, err)
require.Equal(t, i, i2)
}