go-zero/gateway/internal/eventhandler.go

48 lines
1023 B
Go
Raw Normal View History

2022-08-27 11:39:42 +08:00
package internal
import (
"io"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/jhump/protoreflect/desc"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type EventHandler struct {
Status *status.Status
writer io.Writer
marshaler jsonpb.Marshaler
}
func NewEventHandler(writer io.Writer, resolver jsonpb.AnyResolver) *EventHandler {
return &EventHandler{
writer: writer,
marshaler: jsonpb.Marshaler{
EmitDefaults: true,
AnyResolver: resolver,
},
}
}
func (h *EventHandler) OnReceiveResponse(message proto.Message) {
if err := h.marshaler.Marshal(h.writer, message); err != nil {
logx.Error(err)
}
}
func (h *EventHandler) OnReceiveTrailers(status *status.Status, _ metadata.MD) {
h.Status = status
}
2022-09-18 23:17:21 +08:00
func (h *EventHandler) OnResolveMethod(_ *desc.MethodDescriptor) {
}
func (h *EventHandler) OnSendHeaders(_ metadata.MD) {
}
func (h *EventHandler) OnReceiveHeaders(_ metadata.MD) {
}