[#168] netmap: Implement binary and JSON encoders/decoders on Filter
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
874a4b937f
commit
e721734599
14 changed files with 628 additions and 196 deletions
|
@ -1,60 +1,126 @@
|
|||
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 (p *PlacementPolicy) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
PlacementPolicyToGRPCMessage(p),
|
||||
)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (p *PlacementPolicy) UnmarshalJSON(data []byte) error {
|
||||
msg := new(netmap.PlacementPolicy)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
return PlacementPolicyFromGRPCMessage(msg), nil
|
||||
*p = *PlacementPolicyFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Filter) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
FilterToGRPCMessage(f),
|
||||
)
|
||||
}
|
||||
|
||||
func (f *Filter) UnmarshalJSON(data []byte) error {
|
||||
msg := new(netmap.Filter)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*f = *FilterFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Selector) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
SelectorToGRPCMessage(s),
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Selector) UnmarshalJSON(data []byte) error {
|
||||
msg := new(netmap.Selector)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*s = *SelectorFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Replica) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
ReplicaToGRPCMessage(r),
|
||||
)
|
||||
}
|
||||
|
||||
func (r *Replica) UnmarshalJSON(data []byte) error {
|
||||
msg := new(netmap.Replica)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*r = *ReplicaFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Attribute) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
AttributeToGRPCMessage(a),
|
||||
)
|
||||
}
|
||||
|
||||
func (a *Attribute) UnmarshalJSON(data []byte) error {
|
||||
msg := new(netmap.NodeInfo_Attribute)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*a = *AttributeFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ni *NodeInfo) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
NodeInfoToGRPCMessage(ni),
|
||||
)
|
||||
}
|
||||
|
||||
func (ni *NodeInfo) UnmarshalJSON(data []byte) error {
|
||||
msg := new(netmap.NodeInfo)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*ni = *NodeInfoFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue