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
|
package gen
import (
"io"
"strconv"
)
func unmarshal(w io.Writer) *unmarshalGen {
return &unmarshalGen{
p: printer{w: w},
}
}
type unmarshalGen struct {
passes
p printer
hasfield bool
}
func (u *unmarshalGen) Method() Method { return Unmarshal }
func (u *unmarshalGen) needsField() {
if u.hasfield {
return
}
u.p.print("\nvar field []byte; _ = field")
u.hasfield = true
}
func (u *unmarshalGen) Execute(p Elem) error {
u.hasfield = false
if !u.p.ok() {
return u.p.err
}
if !IsPrintable(p) {
return nil
}
u.p.comment("UnmarshalMsg implements msgp.Unmarshaler")
u.p.printf("\nfunc (%s %s) UnmarshalMsg(bts []byte) (o []byte, err error) {", p.Varname(), methodReceiver(p))
next(u, p)
u.p.print("\no = bts")
u.p.nakedReturn()
unsetReceiver(p)
return u.p.err
}
// does assignment to the variable "name" with the type "base"
func (u *unmarshalGen) assignAndCheck(name string, base string) {
if !u.p.ok() {
return
}
u.p.printf("\n%s, bts, err = msgp.Read%sBytes(bts)", name, base)
u.p.print(errcheck)
}
func (u *unmarshalGen) gStruct(s *Struct) {
if !u.p.ok() {
return
}
if s.AsTuple {
u.tuple(s)
} else {
u.mapstruct(s)
}
return
}
func (u *unmarshalGen) tuple(s *Struct) {
// open block
u.p.print("\n{")
u.p.declare(structArraySizeVar, u32)
u.assignAndCheck(structArraySizeVar, arrayHeader)
u.p.arrayCheck(strconv.Itoa(len(s.Fields)), structArraySizeVar)
u.p.closeblock() // close 'ssz' block
for i := range s.Fields {
if !u.p.ok() {
return
}
next(u, s.Fields[i].FieldElem)
}
}
func (u *unmarshalGen) mapstruct(s *Struct) {
u.needsField()
u.p.declare(structMapSizeVar, u32)
u.assignAndCheck(structMapSizeVar, mapHeader)
u.p.print("\nfor isz > 0 {")
u.p.print("\nisz--; field, bts, err = msgp.ReadMapKeyZC(bts)")
u.p.print(errcheck)
u.p.print("\nswitch msgp.UnsafeString(field) {")
for i := range s.Fields {
if !u.p.ok() {
return
}
u.p.printf("\ncase \"%s\":", s.Fields[i].FieldTag)
next(u, s.Fields[i].FieldElem)
}
u.p.print("\ndefault:\nbts, err = msgp.Skip(bts)")
u.p.print(errcheck)
u.p.print("\n}\n}") // close switch and for loop
}
func (u *unmarshalGen) gBase(b *BaseElem) {
if !u.p.ok() {
return
}
refname := b.Varname() // assigned to
lowered := b.Varname() // passed as argument
if b.Convert {
// begin 'tmp' block
refname = "tmp"
lowered = b.ToBase() + "(" + lowered + ")"
u.p.printf("\n{\nvar tmp %s", b.BaseType())
}
switch b.Value {
case Bytes:
u.p.printf("\n%s, bts, err = msgp.ReadBytesBytes(bts, %s)", refname, lowered)
case Ext:
u.p.printf("\nbts, err = msgp.ReadExtensionBytes(bts, %s)", lowered)
case IDENT:
u.p.printf("\nbts, err = %s.UnmarshalMsg(bts)", lowered)
default:
u.p.printf("\n%s, bts, err = msgp.Read%sBytes(bts)", refname, b.BaseName())
}
if b.Convert {
// close 'tmp' block
u.p.printf("\n%s = %s(tmp)\n}", b.Varname(), b.FromBase())
}
u.p.print(errcheck)
}
func (u *unmarshalGen) gArray(a *Array) {
if !u.p.ok() {
return
}
// special case for [const]byte objects
// see decode.go for symmetry
if be, ok := a.Els.(*BaseElem); ok && be.Value == Byte {
u.p.printf("\nbts, err = msgp.ReadExactBytes(bts, %s[:])", a.Varname())
u.p.print(errcheck)
return
}
u.p.declare(arraySizeVar, u32)
u.assignAndCheck(arraySizeVar, arrayHeader)
u.p.arrayCheck(a.Size, arraySizeVar)
u.p.rangeBlock(a.Index, a.Varname(), u, a.Els)
}
func (u *unmarshalGen) gSlice(s *Slice) {
if !u.p.ok() {
return
}
u.p.declare(sliceSizeVar, u32)
u.assignAndCheck(sliceSizeVar, arrayHeader)
u.p.resizeSlice(sliceSizeVar, s)
u.p.rangeBlock(s.Index, s.Varname(), u, s.Els)
}
func (u *unmarshalGen) gMap(m *Map) {
if !u.p.ok() {
return
}
u.p.declare(mapSizeVar, u32)
u.assignAndCheck(mapSizeVar, mapHeader)
// allocate or clear map
u.p.resizeMap(mapSizeVar, m)
// loop and get key,value
u.p.print("\nfor msz > 0 {")
u.p.printf("\nvar %s string; var %s %s; msz--", m.Keyidx, m.Validx, m.Value.TypeName())
u.assignAndCheck(m.Keyidx, stringTyp)
next(u, m.Value)
u.p.mapAssign(m)
u.p.closeblock()
}
func (u *unmarshalGen) gPtr(p *Ptr) {
u.p.printf("\nif msgp.IsNil(bts) { bts, err = msgp.ReadNilBytes(bts); if err != nil { return }; %s = nil; } else { ", p.Varname())
u.p.initPtr(p)
next(u, p.Value)
u.p.closeblock()
}
|