2021-03-12 12:06:02 +00:00
|
|
|
package message
|
|
|
|
|
|
|
|
import (
|
2024-04-25 12:13:10 +00:00
|
|
|
"encoding/json"
|
2021-03-12 12:06:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GRPCConvertedMessage is an interface
|
|
|
|
// of the gRPC message that is used
|
|
|
|
// for Message encoding/decoding.
|
|
|
|
type GRPCConvertedMessage interface {
|
2024-04-25 12:13:10 +00:00
|
|
|
UnmarshalProtobuf([]byte) error
|
2021-03-12 12:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-04-25 12:13:10 +00:00
|
|
|
if err := gm.UnmarshalProtobuf(data); err != nil {
|
2021-03-12 12:06:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.FromGRPCMessage(gm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes m to Protobuf JSON representation.
|
|
|
|
func MarshalJSON(m Message) ([]byte, error) {
|
2024-04-25 12:13:10 +00:00
|
|
|
return json.Marshal(m.ToGRPCMessage())
|
2021-03-12 12:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes m from its Protobuf JSON representation
|
|
|
|
// via related gRPC message.
|
|
|
|
//
|
|
|
|
// gm should be tof the same type as the m.ToGRPCMessage() return.
|
2024-04-25 12:13:10 +00:00
|
|
|
func UnmarshalJSON(m Message, data []byte, gm any) error {
|
|
|
|
if err := json.Unmarshal(data, gm); err != nil {
|
2021-03-12 12:06:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.FromGRPCMessage(gm)
|
|
|
|
}
|