frostfs-api-go/v2/netmap/json.go
Alex Vanin e4d94bbe03 [#186] v2/netmap: Add JSON converter for placement policy
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
2020-11-05 10:38:04 +03:00

60 lines
1.2 KiB
Go

package netmap
import (
"errors"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"google.golang.org/protobuf/encoding/protojson"
)
var (
errEmptyInput = errors.New("empty input")
)
func NodeInfoToJSON(n *NodeInfo) ([]byte, error) {
if n == nil {
return nil, errEmptyInput
}
msg := NodeInfoToGRPCMessage(n)
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
}
func NodeInfoFromJSON(data []byte) (*NodeInfo, error) {
if len(data) == 0 {
return nil, errEmptyInput
}
msg := new(netmap.NodeInfo)
if err := protojson.Unmarshal(data, msg); err != nil {
return nil, err
}
return NodeInfoFromGRPCMessage(msg), nil
}
func PlacementPolicyToJSON(n *PlacementPolicy) ([]byte, error) {
if n == nil {
return nil, errEmptyInput
}
msg := PlacementPolicyToGRPCMessage(n)
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
}
func PlacementPolicyFromJSON(data []byte) (*PlacementPolicy, error) {
if len(data) == 0 {
return nil, errEmptyInput
}
msg := new(netmap.PlacementPolicy)
if err := protojson.Unmarshal(data, msg); err != nil {
return nil, err
}
return PlacementPolicyFromGRPCMessage(msg), nil
}