You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1023 B
Go
48 lines
1023 B
Go
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
|
|
}
|
|
|
|
func (h *EventHandler) OnResolveMethod(_ *desc.MethodDescriptor) {
|
|
}
|
|
|
|
func (h *EventHandler) OnSendHeaders(_ metadata.MD) {
|
|
}
|
|
|
|
func (h *EventHandler) OnReceiveHeaders(_ metadata.MD) {
|
|
}
|