forked from yarpc/yarpc-go
/
marshal.go
112 lines (100 loc) · 3.35 KB
/
marshal.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package protobuf
import (
"bytes"
"io"
"sync"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"go.uber.org/yarpc/api/transport"
"go.uber.org/yarpc/internal/bufferpool"
"go.uber.org/yarpc/yarpcerrors"
)
var (
_jsonMarshaler = &jsonpb.Marshaler{}
_jsonUnmarshaler = &jsonpb.Unmarshaler{AllowUnknownFields: true}
_bufferPool = sync.Pool{
New: func() interface{} {
return proto.NewBuffer(make([]byte, 1024))
},
}
)
func unmarshal(encoding transport.Encoding, reader io.Reader, message proto.Message) error {
buf := bufferpool.Get()
defer bufferpool.Put(buf)
if _, err := buf.ReadFrom(reader); err != nil {
return err
}
body := buf.Bytes()
if len(body) == 0 {
return nil
}
switch encoding {
case Encoding:
return unmarshalProto(body, message)
case JSONEncoding:
return unmarshalJSON(body, message)
default:
return yarpcerrors.Newf(yarpcerrors.CodeInternal, "encoding.Expect should have handled encoding %q but did not", encoding)
}
}
func unmarshalProto(body []byte, message proto.Message) error {
return proto.Unmarshal(body, message)
}
func unmarshalJSON(body []byte, message proto.Message) error {
return _jsonUnmarshaler.Unmarshal(bytes.NewReader(body), message)
}
func marshal(encoding transport.Encoding, message proto.Message) ([]byte, func(), error) {
switch encoding {
case Encoding:
return marshalProto(message)
case JSONEncoding:
return marshalJSON(message)
default:
return nil, nil, yarpcerrors.Newf(yarpcerrors.CodeInternal, "encoding.Expect should have handled encoding %q but did not", encoding)
}
}
func marshalProto(message proto.Message) ([]byte, func(), error) {
protoBuffer := getBuffer()
cleanup := func() { putBuffer(protoBuffer) }
if err := protoBuffer.Marshal(message); err != nil {
cleanup()
return nil, nil, err
}
return protoBuffer.Bytes(), cleanup, nil
}
func marshalJSON(message proto.Message) ([]byte, func(), error) {
buf := bufferpool.Get()
cleanup := func() { bufferpool.Put(buf) }
if err := _jsonMarshaler.Marshal(buf, message); err != nil {
cleanup()
return nil, nil, err
}
return buf.Bytes(), cleanup, nil
}
func getBuffer() *proto.Buffer {
buf := _bufferPool.Get().(*proto.Buffer)
buf.Reset()
return buf
}
func putBuffer(buf *proto.Buffer) {
_bufferPool.Put(buf)
}