forked from tinylib/msgp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
374 lines (330 loc) · 7.93 KB
/
decode.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package gen
import (
"fmt"
"github.com/tinylib/msgp/cfg"
"io"
"strconv"
"strings"
)
func decode(w io.Writer, cfg *cfg.MsgpConfig) *decodeGen {
return &decodeGen{
p: printer{w: w},
hasfield: false,
cfg: cfg,
}
}
type decodeGen struct {
passes
p printer
hasfield bool
depth int
cfg *cfg.MsgpConfig
lifo []bool
post postDefs
}
type postDefs struct {
varnames map[string]int
endlines []string // var declarations declared after method defitions.
}
func (d *postDefs) add(key string, format string, args ...interface{}) {
if len(d.varnames) == 0 {
d.varnames = make(map[string]int)
d.varnames[key] = 0
} else {
_, already := d.varnames[key]
if already {
return
}
}
d.endlines = append(d.endlines, fmt.Sprintf(format, args...))
}
func (d *postDefs) reset() {
d.varnames = nil
d.endlines = d.endlines[:0]
}
func (d *decodeGen) postLines() {
lines := strings.Join(d.post.endlines, "\n")
d.p.printf("\n%s\n", lines)
d.post.reset()
}
func (d *decodeGen) Method() Method { return Decode }
func (d *decodeGen) needsField() {
if d.hasfield {
return
}
d.p.print("\nvar field []byte; _ = field")
d.hasfield = true
}
func (d *decodeGen) Execute(p Elem) error {
p = d.applyall(p)
if p == nil {
return nil
}
d.hasfield = false
if !d.p.ok() {
return d.p.err
}
if !IsPrintable(p) {
return nil
}
d.p.comment("DecodeMsg implements msgp.Decodable")
d.p.comment("We treat empty fields as if we read a Nil from the wire.")
d.p.printf("\nfunc (%s %s) DecodeMsg(dc *msgp.Reader) (err error) {\n", p.Varname(), methodReceiver(p))
d.p.printf(`var sawTopNil bool
if dc.IsNil() {
sawTopNil = true
err = dc.ReadNil()
if err != nil {
return
}
dc.PushAlwaysNil()
}
`)
// next will increment k, but we want the first, top level DecodeMsg
// to refer to this same k ...
next(d, p)
d.p.printf(`
if sawTopNil {
dc.PopAlwaysNil()
}
`)
d.p.nakedReturn()
unsetReceiver(p)
d.postLines()
return d.p.err
}
func (d *decodeGen) gStruct(s *Struct) {
d.depth++
defer func() {
d.depth--
}()
if !d.p.ok() {
return
}
if s.AsTuple {
d.structAsTuple(s)
} else {
d.structAsMap(s)
}
return
}
func (d *decodeGen) assignAndCheck(name string, typ string) {
if !d.p.ok() {
return
}
d.p.printf("\n%s, err = dc.Read%s()", name, typ)
d.p.print(errcheck)
}
func (d *decodeGen) structAsTuple(s *Struct) {
nfields := len(s.Fields)
sz := randIdent()
d.p.declare(sz, u32)
d.assignAndCheck(sz, arrayHeader)
d.p.arrayCheck(strconv.Itoa(nfields), sz, "")
for i := range s.Fields {
if !d.p.ok() {
return
}
next(d, s.Fields[i].FieldElem)
}
}
/* func (d *decodeGen) structAsMap(s *Struct):
//
// Missing (empty) field handling logic:
//
// The approach to missing field handling is to
// keep the logic the same whether the field is
// missing or nil on the wire. To do so we use
// the Reader.PushAlwaysNil() method to tell
// the Reader to pretend to supply
// only nils until further notice. The further
// notice comes from the terminating dc.PopAlwaysNil()
// calls emptying the LIFO. The stack is
// needed because multiple struct decodes may
// be nested due to inlining.
*/
func (d *decodeGen) structAsMap(s *Struct) {
n := len(s.Fields)
if n == 0 {
return
}
d.needsField()
k := genSerial()
tmpl, nStr := genDecodeMsgTemplate(k)
fieldOrder := fmt.Sprintf("\n var decodeMsgFieldOrder%s = []string{", nStr)
for i := range s.Fields {
fieldOrder += fmt.Sprintf("%q,", s.Fields[i].FieldTag)
}
fieldOrder += "}\n"
varname := strings.Replace(s.TypeName(), "\n", ";", -1)
d.post.add(varname, "\n// fields of %s%s", varname, fieldOrder)
d.p.printf("\n const maxFields%s = %d\n", nStr, n)
found := "found" + nStr
d.p.printf(tmpl)
// after printing tmpl, we are at this point:
// switch curField_ {
// -- templateDecodeMsg ends here --
for i := range s.Fields {
d.p.printf("\ncase \"%s\":", s.Fields[i].FieldTag)
d.p.printf("\n%s[%d]=true;", found, i)
//d.p.printf("\n fmt.Printf(\"I found field '%s' at depth=%d. dc.AlwaysNil = %%v\", dc.AlwaysNil);\n", s.Fields[i].FieldTag, d.depth)
d.depth++
next(d, s.Fields[i].FieldElem)
d.depth--
if !d.p.ok() {
return
}
}
d.p.print("\ndefault:\nerr = dc.Skip()")
d.p.print(errcheck)
d.p.closeblock() // close switch
d.p.closeblock() // close for loop
d.p.printf("\n if nextMiss%s != -1 {dc.PopAlwaysNil(); }\n", nStr)
}
func (d *decodeGen) gBase(b *BaseElem) {
if !d.p.ok() {
return
}
// open block for 'tmp'
var tmp string
if b.Convert {
tmp = randIdent()
d.p.printf("\n{ var %s %s", tmp, b.BaseType())
}
vname := b.Varname() // e.g. "z.FieldOne"
bname := b.BaseName() // e.g. "Float64"
// handle special cases
// for object type.
switch b.Value {
case Bytes:
if b.Convert {
d.p.printf("\n%s, err = dc.ReadBytes([]byte(%s))", tmp, vname)
} else {
d.p.printf("\n%s, err = dc.ReadBytes(%s)", vname, vname)
}
case IDENT:
d.p.printf("\nerr = %s.DecodeMsg(dc)", vname)
case Ext:
d.p.printf("\n if !dc.IsNil() {")
d.p.printf("\nerr = dc.ReadExtension(%s)\n} else { err = dc.ReadNil() }\n", vname)
default:
if b.Convert {
d.p.printf("\n%s, err = dc.Read%s()", tmp, bname)
} else {
d.p.printf("\n%s, err = dc.Read%s()", vname, bname)
}
}
// close block for 'tmp'
if b.Convert {
d.p.printf("\n%s = %s(%s)\n}", vname, b.FromBase(), tmp)
}
d.p.print(errcheck)
}
func (d *decodeGen) gMap(m *Map) {
d.depth++
defer func() {
d.depth--
}()
if !d.p.ok() {
return
}
sz := randIdent()
// resize or allocate map
d.p.declare(sz, u32)
d.assignAndCheck(sz, mapHeader)
d.p.resizeMap(sz, m)
// for element in map, read string/value
// pair and assign
d.p.printf("\nfor %s > 0 {\n%s--", sz, sz)
d.p.declare(m.Keyidx, "string")
d.p.declare(m.Validx, m.Value.TypeName())
d.assignAndCheck(m.Keyidx, stringTyp)
next(d, m.Value)
d.p.mapAssign(m)
d.p.closeblock()
}
func (d *decodeGen) gSlice(s *Slice) {
if !d.p.ok() {
return
}
sz := randIdent()
d.p.declare(sz, u32)
d.assignAndCheck(sz, arrayHeader)
d.p.resizeSlice(sz, s)
d.p.rangeBlock(s.Index, s.Varname(), d, s.Els)
}
func (d *decodeGen) gArray(a *Array) {
if !d.p.ok() {
return
}
d.p.printf(`
if dc.AlwaysNil {
// nothing more here
} else if dc.IsNil() {
err = dc.ReadNil()
if err != nil {
return
}
}`) // possible else next
// special case if we have [const]byte
if be, ok := a.Els.(*BaseElem); ok && (be.Value == Byte || be.Value == Uint8) {
d.p.printf("\nerr = dc.ReadExactBytes(%s[:])", a.Varname())
d.p.print(errcheck)
return
} else {
d.p.printf(" else {\n")
}
sz := randIdent()
d.p.declare(sz, u32)
d.assignAndCheck(sz, arrayHeader)
d.p.arrayCheck(a.Size, sz, "!dc.IsNil() && ")
d.p.closeblock()
d.p.rangeBlock(a.Index, a.Varname(), d, a.Els)
}
func (d *decodeGen) gPtr(p *Ptr) {
if !d.p.ok() {
return
}
d.p.printf(`
if dc.IsNil() {
err = dc.ReadNil()
if err != nil {
return
}
`)
vname := p.Varname()
base, isBase := p.Value.(*BaseElem)
if isBase {
//d.p.printf("\n // we have a BaseElem: %#v \n", base)
switch base.Value {
case IDENT:
//d.p.printf("\n // we have an IDENT: \n")
d.p.printf(
`
if %s != nil {
dc.PushAlwaysNil()
err = %s.DecodeMsg(dc)
if err != nil {
return
}
dc.PopAlwaysNil()
}
} else {
// not Nil, we have something to read
`, vname, vname)
case Ext:
d.p.printf("\n // we have an base.Value of Ext: replace the Ext iff already allocated")
d.p.printf("\nif %s != nil {\n %s = new(msgp.RawExtension) } \n"+
" } else {\n // we have bytes in dc to read\n", vname, vname)
default:
//d.p.printf("\n // we have an unknown base.Value type= %T/val=%#v: \n", base.Value, base.Value)
d.p.printf("\n } else { \n")
}
} else {
// !isBase
d.p.printf("\n%s = nil\n} else {", vname)
}
d.p.initPtr(p)
next(d, p.Value)
d.p.closeblock()
}