[#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>
This commit is contained in:
parent
cf765a61a6
commit
c61656a43f
3 changed files with 159 additions and 0 deletions
43
rpc/message/message.go
Normal file
43
rpc/message/message.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neofs-api-go/rpc/grpc"
|
||||
)
|
||||
|
||||
// Message represents raw Protobuf message
|
||||
// that can be transmitted via several
|
||||
// transport protocols.
|
||||
type Message interface {
|
||||
// Must return gRPC message that can
|
||||
// be used for gRPC protocol transmission.
|
||||
ToGRPCMessage() grpc.Message
|
||||
|
||||
// Must restore the message from related
|
||||
// gRPC message.
|
||||
//
|
||||
// If gRPC message is not a related one,
|
||||
// ErrUnexpectedMessageType can be returned
|
||||
// to indicate this.
|
||||
FromGRPCMessage(grpc.Message) error
|
||||
}
|
||||
|
||||
// ErrUnexpectedMessageType is an error that
|
||||
// is used to indicate message mismatch.
|
||||
type ErrUnexpectedMessageType struct {
|
||||
exp, act interface{}
|
||||
}
|
||||
|
||||
// NewUnexpectedMessageType initializes an error about message mismatch
|
||||
// between act and exp.
|
||||
func NewUnexpectedMessageType(act, exp interface{}) ErrUnexpectedMessageType {
|
||||
return ErrUnexpectedMessageType{
|
||||
exp: exp,
|
||||
act: act,
|
||||
}
|
||||
}
|
||||
|
||||
func (e ErrUnexpectedMessageType) Error() string {
|
||||
return fmt.Sprintf("unexpected message type %T: expected %T", e.act, e.exp)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue