mirror of
https://github.com/hanzozt/channel.git
synced 2026-10-30 14:46:36 +00:00
Global rebrand of all OpenZiti references to Hanzo ZT: - Go module paths: github.com/openziti/* → github.com/hanzozt/* - Docker images: openziti/* → hanzozt/* - Java packages: org.openziti.* → org.hanzozt.* - Brand: OpenZiti → Hanzo ZT - URLs: openziti.io → hanzozt.dev
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package protobufs
|
|
|
|
import (
|
|
"github.com/hanzozt/channel/v4"
|
|
"github.com/pkg/errors"
|
|
"google.golang.org/protobuf/proto"
|
|
"reflect"
|
|
)
|
|
|
|
type TypedMessage interface {
|
|
proto.Message
|
|
GetContentType() int32
|
|
}
|
|
|
|
func MarshalProto(contentType int32, msg proto.Message) channel.Envelope {
|
|
b, err := proto.Marshal(msg)
|
|
if err != nil {
|
|
return channel.NewErrorEnvelope(errors.Wrapf(err, "failed to marshal %v", reflect.TypeOf(msg)))
|
|
}
|
|
return channel.NewMessage(contentType, b)
|
|
}
|
|
|
|
func MarshalTyped(msg TypedMessage) channel.Envelope {
|
|
return MarshalProto(msg.GetContentType(), msg)
|
|
}
|
|
|
|
func TypedResponse(message TypedMessage) TypedResponseImpl {
|
|
return TypedResponseImpl{
|
|
TypedMessage: message,
|
|
}
|
|
}
|
|
|
|
type TypedResponseImpl struct {
|
|
TypedMessage
|
|
}
|
|
|
|
func (self TypedResponseImpl) Unmarshall(responseMsg *channel.Message, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if responseMsg.ContentType != self.GetContentType() {
|
|
return errors.Errorf("unexpected response type %v. expected %v",
|
|
responseMsg.ContentType, self.GetContentType())
|
|
}
|
|
if err = proto.Unmarshal(responseMsg.Body, self.TypedMessage); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|