[#172] v2/netmap: Add JSON converter for node info

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-19 21:27:38 +03:00 committed by Alex Vanin
parent 2e1096200e
commit b681b28e33
2 changed files with 58 additions and 0 deletions

36
v2/netmap/json.go Normal file
View file

@ -0,0 +1,36 @@
package netmap
import (
"github.com/golang/protobuf/jsonpb"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
)
func NodeInfoToJSON(n *NodeInfo) []byte {
if n == nil {
return nil
}
msg := NodeInfoToGRPCMessage(n)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
if err != nil {
return nil
}
return []byte(s)
}
func NodeInfoFromJSON(data []byte) *NodeInfo {
if len(data) == 0 {
return nil
}
msg := new(netmap.NodeInfo)
if err := jsonpb.UnmarshalString(string(data), msg); err != nil {
return nil
}
return NodeInfoFromGRPCMessage(msg)
}

22
v2/netmap/json_test.go Normal file
View file

@ -0,0 +1,22 @@
package netmap_test
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
"github.com/stretchr/testify/require"
)
func TestNodeInfoJSON(t *testing.T) {
exp := generateNodeInfo("public key", "/multi/addr", 2)
t.Run("non empty", func(t *testing.T) {
data := netmap.NodeInfoToJSON(exp)
require.NotNil(t, data)
got := netmap.NodeInfoFromJSON(data)
require.NotNil(t, got)
require.Equal(t, exp, got)
})
}