Some checks failed
DCO action / DCO (pull_request) Failing after 2m20s
Tests and linters / Tests (1.19) (pull_request) Successful in 2m43s
Tests and linters / Tests (1.20) (pull_request) Successful in 2m50s
Tests and linters / Tests with -race (pull_request) Successful in 3m7s
Tests and linters / Lint (pull_request) Failing after 3m53s
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
package message
|
|
|
|
import (
|
|
"encoding/json"
|
|
)
|
|
|
|
// GRPCConvertedMessage is an interface
|
|
// of the gRPC message that is used
|
|
// for Message encoding/decoding.
|
|
type GRPCConvertedMessage interface {
|
|
UnmarshalProtobuf([]byte) error
|
|
}
|
|
|
|
// 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 := gm.UnmarshalProtobuf(data); err != nil {
|
|
return err
|
|
}
|
|
|
|
return m.FromGRPCMessage(gm)
|
|
// 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 json.Marshal(m.ToGRPCMessage())
|
|
// 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 any) error {
|
|
if err := json.Unmarshal(data, gm); err != nil {
|
|
return err
|
|
}
|
|
return m.FromGRPCMessage(gm)
|
|
// if err := protojson.Unmarshal(data, gm); err != nil {
|
|
// return err
|
|
// }
|
|
|
|
// return m.FromGRPCMessage(gm)
|
|
}
|