/
forward.go
53 lines (50 loc) · 1.27 KB
/
forward.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package web
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
type Forward interface {
Request(counted uint64, messageType int, p []byte) error
Response(counted uint64) (e error)
CloseSend() error
}
type forward struct {
req func(counted uint64, messageType int, p []byte) error
resp func(counted uint64) (e error)
closeSend func() error
}
func NewForward(req func(counted uint64, messageType int, p []byte) error,
resp func(counted uint64) (e error),
closeSend func() error,
) Forward {
return &forward{
req: req,
resp: resp,
closeSend: closeSend,
}
}
func (f *forward) Request(counted uint64, messageType int, p []byte) error {
return f.req(counted, messageType, p)
}
func (f *forward) Response(counted uint64) (e error) {
return f.resp(counted)
}
func (f *forward) CloseSend() error {
return f.closeSend()
}
func Unmarshal(b []byte, m proto.Message) error {
e := protojson.UnmarshalOptions{
DiscardUnknown: true,
}.Unmarshal(b, m)
if e != nil {
e = status.Error(codes.InvalidArgument, e.Error())
}
return e
}
func Marshal(m proto.Message) ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(m)
}