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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
package codec
import (
"fmt"
"math"
"reflect"
"sort"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/jhump/protoreflect/desc"
)
// EncodeZigZag64 does zig-zag encoding to convert the given
// signed 64-bit integer into a form that can be expressed
// efficiently as a varint, even for negative values.
func EncodeZigZag64(v int64) uint64 {
return (uint64(v) << 1) ^ uint64(v>>63)
}
// EncodeZigZag32 does zig-zag encoding to convert the given
// signed 32-bit integer into a form that can be expressed
// efficiently as a varint, even for negative values.
func EncodeZigZag32(v int32) uint64 {
return uint64((uint32(v) << 1) ^ uint32((v >> 31)))
}
func (cb *Buffer) EncodeFieldValue(fd *desc.FieldDescriptor, val interface{}) error {
if fd.IsMap() {
mp := val.(map[interface{}]interface{})
entryType := fd.GetMessageType()
keyType := entryType.FindFieldByNumber(1)
valType := entryType.FindFieldByNumber(2)
var entryBuffer Buffer
if cb.IsDeterministic() {
entryBuffer.SetDeterministic(true)
keys := make([]interface{}, 0, len(mp))
for k := range mp {
keys = append(keys, k)
}
sort.Sort(sortable(keys))
for _, k := range keys {
v := mp[k]
entryBuffer.Reset()
if err := entryBuffer.encodeFieldElement(keyType, k); err != nil {
return err
}
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || !rv.IsNil() {
if err := entryBuffer.encodeFieldElement(valType, v); err != nil {
return err
}
}
if err := cb.EncodeTagAndWireType(fd.GetNumber(), proto.WireBytes); err != nil {
return err
}
if err := cb.EncodeRawBytes(entryBuffer.Bytes()); err != nil {
return err
}
}
} else {
for k, v := range mp {
entryBuffer.Reset()
if err := entryBuffer.encodeFieldElement(keyType, k); err != nil {
return err
}
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || !rv.IsNil() {
if err := entryBuffer.encodeFieldElement(valType, v); err != nil {
return err
}
}
if err := cb.EncodeTagAndWireType(fd.GetNumber(), proto.WireBytes); err != nil {
return err
}
if err := cb.EncodeRawBytes(entryBuffer.Bytes()); err != nil {
return err
}
}
}
return nil
} else if fd.IsRepeated() {
sl := val.([]interface{})
wt, err := getWireType(fd.GetType())
if err != nil {
return err
}
if isPacked(fd) && len(sl) > 0 &&
(wt == proto.WireVarint || wt == proto.WireFixed32 || wt == proto.WireFixed64) {
// packed repeated field
var packedBuffer Buffer
for _, v := range sl {
if err := packedBuffer.encodeFieldValue(fd, v); err != nil {
return err
}
}
if err := cb.EncodeTagAndWireType(fd.GetNumber(), proto.WireBytes); err != nil {
return err
}
return cb.EncodeRawBytes(packedBuffer.Bytes())
} else {
// non-packed repeated field
for _, v := range sl {
if err := cb.encodeFieldElement(fd, v); err != nil {
return err
}
}
return nil
}
} else {
return cb.encodeFieldElement(fd, val)
}
}
func isPacked(fd *desc.FieldDescriptor) bool {
opts := fd.AsFieldDescriptorProto().GetOptions()
// if set, use that value
if opts != nil && opts.Packed != nil {
return opts.GetPacked()
}
// if unset: proto2 defaults to false, proto3 to true
return fd.GetFile().IsProto3()
}
// sortable is used to sort map keys. Values will be integers (int32, int64, uint32, and uint64),
// bools, or strings.
type sortable []interface{}
func (s sortable) Len() int {
return len(s)
}
func (s sortable) Less(i, j int) bool {
vi := s[i]
vj := s[j]
switch reflect.TypeOf(vi).Kind() {
case reflect.Int32:
return vi.(int32) < vj.(int32)
case reflect.Int64:
return vi.(int64) < vj.(int64)
case reflect.Uint32:
return vi.(uint32) < vj.(uint32)
case reflect.Uint64:
return vi.(uint64) < vj.(uint64)
case reflect.String:
return vi.(string) < vj.(string)
case reflect.Bool:
return !vi.(bool) && vj.(bool)
default:
panic(fmt.Sprintf("cannot compare keys of type %v", reflect.TypeOf(vi)))
}
}
func (s sortable) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (b *Buffer) encodeFieldElement(fd *desc.FieldDescriptor, val interface{}) error {
wt, err := getWireType(fd.GetType())
if err != nil {
return err
}
if err := b.EncodeTagAndWireType(fd.GetNumber(), wt); err != nil {
return err
}
if err := b.encodeFieldValue(fd, val); err != nil {
return err
}
if wt == proto.WireStartGroup {
return b.EncodeTagAndWireType(fd.GetNumber(), proto.WireEndGroup)
}
return nil
}
func (b *Buffer) encodeFieldValue(fd *desc.FieldDescriptor, val interface{}) error {
switch fd.GetType() {
case descriptorpb.FieldDescriptorProto_TYPE_BOOL:
v := val.(bool)
if v {
return b.EncodeVarint(1)
}
return b.EncodeVarint(0)
case descriptorpb.FieldDescriptorProto_TYPE_ENUM,
descriptorpb.FieldDescriptorProto_TYPE_INT32:
v := val.(int32)
return b.EncodeVarint(uint64(v))
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED32:
v := val.(int32)
return b.EncodeFixed32(uint64(v))
case descriptorpb.FieldDescriptorProto_TYPE_SINT32:
v := val.(int32)
return b.EncodeVarint(EncodeZigZag32(v))
case descriptorpb.FieldDescriptorProto_TYPE_UINT32:
v := val.(uint32)
return b.EncodeVarint(uint64(v))
case descriptorpb.FieldDescriptorProto_TYPE_FIXED32:
v := val.(uint32)
return b.EncodeFixed32(uint64(v))
case descriptorpb.FieldDescriptorProto_TYPE_INT64:
v := val.(int64)
return b.EncodeVarint(uint64(v))
case descriptorpb.FieldDescriptorProto_TYPE_SFIXED64:
v := val.(int64)
return b.EncodeFixed64(uint64(v))
case descriptorpb.FieldDescriptorProto_TYPE_SINT64:
v := val.(int64)
return b.EncodeVarint(EncodeZigZag64(v))
case descriptorpb.FieldDescriptorProto_TYPE_UINT64:
v := val.(uint64)
return b.EncodeVarint(v)
case descriptorpb.FieldDescriptorProto_TYPE_FIXED64:
v := val.(uint64)
return b.EncodeFixed64(v)
case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE:
v := val.(float64)
return b.EncodeFixed64(math.Float64bits(v))
case descriptorpb.FieldDescriptorProto_TYPE_FLOAT:
v := val.(float32)
return b.EncodeFixed32(uint64(math.Float32bits(v)))
case descriptorpb.FieldDescriptorProto_TYPE_BYTES:
v := val.([]byte)
return b.EncodeRawBytes(v)
case descriptorpb.FieldDescriptorProto_TYPE_STRING:
v := val.(string)
return b.EncodeRawBytes(([]byte)(v))
case descriptorpb.FieldDescriptorProto_TYPE_MESSAGE:
return b.EncodeDelimitedMessage(val.(proto.Message))
case descriptorpb.FieldDescriptorProto_TYPE_GROUP:
// just append the nested message to this buffer
return b.EncodeMessage(val.(proto.Message))
// whosoever writeth start-group tag (e.g. caller) is responsible for writing end-group tag
default:
return fmt.Errorf("unrecognized field type: %v", fd.GetType())
}
}
func getWireType(t descriptorpb.FieldDescriptorProto_Type) (int8, error) {
switch t {
case descriptorpb.FieldDescriptorProto_TYPE_ENUM,
descriptorpb.FieldDescriptorProto_TYPE_BOOL,
descriptorpb.FieldDescriptorProto_TYPE_INT32,
descriptorpb.FieldDescriptorProto_TYPE_SINT32,
descriptorpb.FieldDescriptorProto_TYPE_UINT32,
descriptorpb.FieldDescriptorProto_TYPE_INT64,
descriptorpb.FieldDescriptorProto_TYPE_SINT64,
descriptorpb.FieldDescriptorProto_TYPE_UINT64:
return proto.WireVarint, nil
case descriptorpb.FieldDescriptorProto_TYPE_FIXED32,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED32,
descriptorpb.FieldDescriptorProto_TYPE_FLOAT:
return proto.WireFixed32, nil
case descriptorpb.FieldDescriptorProto_TYPE_FIXED64,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED64,
descriptorpb.FieldDescriptorProto_TYPE_DOUBLE:
return proto.WireFixed64, nil
case descriptorpb.FieldDescriptorProto_TYPE_BYTES,
descriptorpb.FieldDescriptorProto_TYPE_STRING,
descriptorpb.FieldDescriptorProto_TYPE_MESSAGE:
return proto.WireBytes, nil
case descriptorpb.FieldDescriptorProto_TYPE_GROUP:
return proto.WireStartGroup, nil
default:
return 0, ErrBadWireType
}
}
|