frostfs-api-go-pogpp/rpc/message/encoding.go
Leonard Lyubich c61656a43f [#263] Define generic protobuf message
Define `Message` interface of the generic protobuf message. In the initial
implementation, the message should be convertible to and from related gRPC
message.

Implement encoding functions over the `Message` that can:

 * unmarshal the `Message` via related gRPC message;
 * decode the `Message` from JSON format via related gRPC message;
 * encode the `Message` to JSON.

Implement nested `test` package for testing the implementation.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2021-03-17 14:03:49 +03:00

48 lines
1.2 KiB
Go

package message
import (
"github.com/nspcc-dev/neofs-api-go/rpc/grpc"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
// GRPCConvertedMessage is an interface
// of the gRPC message that is used
// for Message encoding/decoding.
type GRPCConvertedMessage interface {
grpc.Message
proto.Message
}
// Unmarshal decodes m from its Protobuf binary representation
// via related gRPC message.
//
// gm should be tof the same type as the m.ToGRPCMessage() return.
func Unmarshal(m Message, data []byte, gm GRPCConvertedMessage) error {
if err := proto.Unmarshal(data, gm); err != nil {
return err
}
return m.FromGRPCMessage(gm)
}
// MarshalJSON encodes m to Protobuf JSON representation.
func MarshalJSON(m Message) ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
m.ToGRPCMessage().(proto.Message),
)
}
// UnmarshalJSON decodes m from its Protobuf JSON representation
// via related gRPC message.
//
// gm should be tof the same type as the m.ToGRPCMessage() return.
func UnmarshalJSON(m Message, data []byte, gm GRPCConvertedMessage) error {
if err := protojson.Unmarshal(data, gm); err != nil {
return err
}
return m.FromGRPCMessage(gm)
}