-
Notifications
You must be signed in to change notification settings - Fork 34
/
tsfm_protobuf.go
74 lines (59 loc) · 1.9 KB
/
tsfm_protobuf.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package transformer
import (
"context"
"io"
"net/textproto"
"reflect"
"github.com/golang/protobuf/proto"
"github.com/pkg/errors"
"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator"
"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
)
var (
ErrEncodeDataNotProtobuf = errors.New("encode data must be `proto.Message`")
ErrDecodeTargetNotProtobuf = errors.New("decode target must be `proto.Message`")
)
func init() { DefaultFactory.Register(&Protobuf{}) }
type Protobuf struct{}
func (Protobuf) Names() []string { return []string{httpx.MIME_PROTOBUF, "protobuf", "x-protobuf"} }
func (Protobuf) NamedByTag() string { return "protobuf" }
func (t *Protobuf) String() string { return httpx.MIME_PROTOBUF }
func (Protobuf) New(context.Context, typesx.Type) (Transformer, error) { return &Protobuf{}, nil }
func (t *Protobuf) EncodeTo(ctx context.Context, w io.Writer, v interface{}) error {
if rv, ok := v.(reflect.Value); ok {
v = rv.Interface()
}
httpx.MaybeWriteHeader(ctx, w, t.String(), map[string]string{})
pv, ok := v.(proto.Message)
if !ok {
return ErrEncodeDataNotProtobuf
}
data, err := proto.Marshal(pv)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func (t *Protobuf) DecodeFrom(ctx context.Context, r io.Reader, v interface{}, _ ...textproto.MIMEHeader) error {
if rv, ok := v.(reflect.Value); ok {
if rv.Kind() != reflect.Ptr && rv.CanAddr() {
rv = rv.Addr()
}
v = rv.Interface()
}
pv, ok := v.(proto.Message)
if !ok {
return ErrDecodeTargetNotProtobuf
}
data, err := io.ReadAll(r)
if err != nil {
return err
}
return proto.Unmarshal(data, pv)
}
// NewValidator returns empty validator to implements interface `MayValidate` to skip protobuf struct validation
func (t *Protobuf) NewValidator(_ context.Context, _ typesx.Type) (validator.Validator, error) {
return nil, nil
}