From 40d7c9cd938bb5db48e465ea49ddf14b44404103 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 5 Nov 2020 15:20:07 +0300 Subject: [PATCH] [#189] sdk/netmap: Implement NodeInfo JSON encode/decode functions Signed-off-by: Leonard Lyubich --- pkg/netmap/node_info.go | 15 +++++++++++++++ pkg/netmap/node_info_test.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/pkg/netmap/node_info.go b/pkg/netmap/node_info.go index 327e77ab..81f27679 100644 --- a/pkg/netmap/node_info.go +++ b/pkg/netmap/node_info.go @@ -214,6 +214,21 @@ func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo { 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. func (i *NodeInfo) ToV2() *netmap.NodeInfo { return (*netmap.NodeInfo)(i) diff --git a/pkg/netmap/node_info_test.go b/pkg/netmap/node_info_test.go index 75de15dc..366b9ffa 100644 --- a/pkg/netmap/node_info_test.go +++ b/pkg/netmap/node_info_test.go @@ -140,3 +140,19 @@ func TestNodeInfo_Attributes(t *testing.T) { 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) +}