/
string.go
321 lines (305 loc) · 8.43 KB
/
string.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Extensions for Protocol Buffers to create more go like structures.
//
// Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
// http://github.com/gogo/protobuf/gogoproto
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package fieldpath
import (
"fmt"
"io"
"strings"
descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
)
type stringWriter struct {
tabs int
w io.Writer
}
func (this *stringWriter) tab() {
for i := 0; i < this.tabs; i++ {
io.WriteString(this.w, "\t")
}
}
func (this *stringWriter) Float64(v float64) {
io.WriteString(this.w, fmt.Sprintf(`%f`, v))
}
func (this *stringWriter) Float32(v float32) {
io.WriteString(this.w, fmt.Sprintf(`%f`, v))
}
func (this *stringWriter) Int64(v int64) {
io.WriteString(this.w, fmt.Sprintf(`%d`, v))
}
func (this *stringWriter) Int32(v int32) {
io.WriteString(this.w, fmt.Sprintf(`%d`, v))
}
func (this *stringWriter) Uint64(v uint64) {
io.WriteString(this.w, fmt.Sprintf(`%d`, v))
}
func (this *stringWriter) Uint32(v uint32) {
io.WriteString(this.w, fmt.Sprintf(`%d`, v))
}
func (this *stringWriter) Bool(v bool) {
io.WriteString(this.w, fmt.Sprintf(`%v`, v))
}
func (this *stringWriter) Bytes(v []byte) {
io.WriteString(this.w, fmt.Sprintf(`%v`, v))
}
func (this *stringWriter) String(v string) {
io.WriteString(this.w, v)
}
//Converts a marshalled protocol buffer to string.
//It does this pretty effienctly by not combining slices.
//It rather prints every element of the slice seperately.
//This is typically used for debugging.
func ToString(rootPkg string, rootMsg string, desc *descriptor.FileDescriptorSet, fpath string, buf []byte, tabs int, writer io.Writer) error {
w := &stringWriter{tabs, writer}
messagePkg := rootPkg
messageName := rootMsg
if len(fpath) > 0 {
fd, err := new(rootPkg, rootMsg, desc, fpath)
if err != nil {
panic(err)
}
messagePkg = fd.rootPkg
messageName = fd.rootMsg
}
msg := desc.GetMessage(messagePkg, messageName)
if msg == nil {
panic("message not found: " + messagePkg + "." + messageName)
}
offset := 0
for offset < len(buf) {
key, n, err := decodeVarint(buf, offset)
if err != nil {
panic(err)
}
offset += n
fieldNum := int32(key >> 3)
found := false
for _, f := range msg.GetField() {
if f.IsPacked() {
panic("not implemented")
}
if f.GetNumber() != fieldNum {
continue
}
found = true
if f.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
length, nn, err := decodeVarint(buf, offset)
if err != nil {
panic(err)
}
offset += nn
w.tab()
w.String(f.GetName() + `:{` + "\n")
err = ToString(messagePkg, messageName, desc, f.GetName(), buf[offset:offset+int(length)], tabs+1, writer)
if err != nil {
return err
}
offset += int(length)
w.tab()
w.String(`}` + "\n")
break
} else {
w.tab()
w.String(f.GetName() + `:`)
var m int
var err error
switch f.GetType() {
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
m, err = DecFloat64(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
m, err = DecFloat32(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_INT64:
m, err = DecInt64(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_UINT64:
m, err = DecUint64(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_INT32:
m, err = DecInt32(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_FIXED64:
m, err = DecFixed64(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_FIXED32:
m, err = DecFixed32(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_BOOL:
m, err = DecBool(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_STRING:
m, err = DecString(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_GROUP:
panic("not implemented")
case descriptor.FieldDescriptorProto_TYPE_BYTES:
m, err = DecBytes(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_UINT32:
m, err = DecUint32(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_ENUM:
m, err = DecInt32(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_SFIXED32:
m, err = DecSfixed32(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_SFIXED64:
m, err = DecSfixed64(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_SINT32:
m, err = DecSint32(buf, offset, w)
case descriptor.FieldDescriptorProto_TYPE_SINT64:
m, err = DecSint64(buf, offset, w)
default:
panic("unreachable")
}
offset += m
if err != nil {
panic(err)
}
w.String(",\n")
break
}
}
if !found {
for _, extRange := range msg.GetExtensionRange() {
if extRange.GetStart() <= fieldNum && fieldNum <= extRange.GetEnd() {
found = true
break
}
}
w.tab()
offset -= n
nn, err := protoSkip(buf[offset:])
if err != nil {
panic(err)
}
if !found {
w.String("XXX_unrecognized:")
} else {
w.String("XXX_extensions:")
extPkg, ext := desc.FindExtensionByFieldNumber(messagePkg, messageName, fieldNum)
if ext != nil && ext.IsMessage() {
names := strings.Split(ext.GetTypeName(), ".")
if len(names) == 3 {
_, nnn, err := decodeVarint(buf, offset+n)
if err != nil {
panic(err)
}
w.tab()
w.String(extPkg + "." + names[2] + `:{` + "\n")
err = ToString(extPkg, names[2], desc, "", buf[offset+n+nnn:offset+nn], tabs+1, writer)
if err != nil {
return err
}
offset += nn
w.tab()
w.String(`}` + "\n")
break
} else {
panic(ext.GetTypeName())
}
}
}
w.Bytes(buf[offset : offset+nn])
w.String("\n")
offset += nn
}
}
return nil
}
func protoSkip(data []byte) (n int, err error) {
l := len(data)
index := 0
for index < l {
var wire uint64
for shift := uint(0); ; shift += 7 {
if index >= l {
return 0, io.ErrUnexpectedEOF
}
b := data[index]
index++
wire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
wireType := int(wire & 0x7)
switch wireType {
case 0:
for {
if index >= l {
return 0, io.ErrUnexpectedEOF
}
index++
if data[index-1] < 0x80 {
break
}
}
return index, nil
case 1:
index += 8
return index, nil
case 2:
var length int
for shift := uint(0); ; shift += 7 {
if index >= l {
return 0, io.ErrUnexpectedEOF
}
b := data[index]
index++
length |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
index += length
return index, nil
case 3:
for {
var innerWire uint64
var start int = index
for shift := uint(0); ; shift += 7 {
if index >= l {
return 0, io.ErrUnexpectedEOF
}
b := data[index]
index++
innerWire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
innerWireType := int(innerWire & 0x7)
if innerWireType == 4 {
break
}
next, err := protoSkip(data[start:])
if err != nil {
return 0, err
}
index = start + next
}
return index, nil
case 4:
return index, nil
case 5:
index += 4
return index, nil
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
}
panic("unreachable")
}