[#168] netmap: Implement binary and JSON encoders/decoders on Filter

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 11:45:59 +03:00 committed by Alex Vanin
parent 874a4b937f
commit e721734599
14 changed files with 628 additions and 196 deletions

View file

@ -193,6 +193,38 @@ func (a *NodeAttribute) SetParentKeys(keys ...string) {
SetParents(keys)
}
// Marshal marshals NodeAttribute into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (a *NodeAttribute) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return (*netmap.Attribute)(a).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of NodeAttribute.
func (a *NodeAttribute) Unmarshal(data []byte) error {
return (*netmap.Attribute)(a).
Unmarshal(data)
}
// MarshalJSON encodes NodeAttribute to protobuf JSON format.
func (a *NodeAttribute) MarshalJSON() ([]byte, error) {
return (*netmap.Attribute)(a).
MarshalJSON()
}
// UnmarshalJSON decodes NodeAttribute from protobuf JSON format.
func (a *NodeAttribute) UnmarshalJSON(data []byte) error {
return (*netmap.Attribute)(a).
UnmarshalJSON(data)
}
// NewNodeInfo creates and returns new NodeInfo instance.
func NewNodeInfo() *NodeInfo {
return NewNodeInfoFromV2(new(netmap.NodeInfo))
@ -203,21 +235,6 @@ 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)
@ -286,3 +303,35 @@ func (i *NodeInfo) SetState(s NodeState) {
(*netmap.NodeInfo)(i).
SetState(s.ToV2())
}
// Marshal marshals NodeInfo into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (i *NodeInfo) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return (*netmap.NodeInfo)(i).
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of NodeInfo.
func (i *NodeInfo) Unmarshal(data []byte) error {
return (*netmap.NodeInfo)(i).
Unmarshal(data)
}
// MarshalJSON encodes NodeInfo to protobuf JSON format.
func (i *NodeInfo) MarshalJSON() ([]byte, error) {
return (*netmap.NodeInfo)(i).
MarshalJSON()
}
// UnmarshalJSON decodes NodeInfo from protobuf JSON format.
func (i *NodeInfo) UnmarshalJSON(data []byte) error {
return (*netmap.NodeInfo)(i).
UnmarshalJSON(data)
}