2020-10-19 18:27:38 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2020-10-28 12:10:54 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-10-19 18:27:38 +00:00
|
|
|
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
|
2020-10-19 19:09:13 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2020-10-19 18:27:38 +00:00
|
|
|
)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
var (
|
|
|
|
errEmptyInput = errors.New("empty input")
|
|
|
|
)
|
|
|
|
|
|
|
|
func NodeInfoToJSON(n *NodeInfo) ([]byte, error) {
|
2020-10-19 18:27:38 +00:00
|
|
|
if n == nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 18:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := NodeInfoToGRPCMessage(n)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
|
2020-10-19 18:27:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
func NodeInfoFromJSON(data []byte) (*NodeInfo, error) {
|
2020-10-19 18:27:38 +00:00
|
|
|
if len(data) == 0 {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 18:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := new(netmap.NodeInfo)
|
|
|
|
|
2020-10-19 19:09:13 +00:00
|
|
|
if err := protojson.Unmarshal(data, msg); err != nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, err
|
2020-10-19 18:27:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return NodeInfoFromGRPCMessage(msg), nil
|
2020-10-19 18:27:38 +00:00
|
|
|
}
|
2020-10-29 17:13:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|