Skip to content
This repository was archived by the owner on May 8, 2019. It is now read-only.

Commit 34f3100

Browse files
committed
improved decode methods by removing extra blank assignment
1 parent b35ce40 commit 34f3100

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

gen/decode.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@ type decodeGen struct {
1919

2020
func (d *decodeGen) Method() Method { return Decode }
2121

22-
func (d *decodeGen) needsField() {
23-
if d.hasField {
24-
return
25-
}
26-
d.p.declare("field", "[]byte")
27-
d.p.blankAssign("field")
28-
d.hasField = true
29-
}
30-
3122
func (d *decodeGen) Execute(p Elem) error {
3223
p = d.applyAll(p)
3324
if p == nil {
@@ -63,7 +54,7 @@ func (d *decodeGen) gStruct(s *Struct) {
6354
return
6455
}
6556

66-
func (d *decodeGen) assignAndCheck(name string, typ string) {
57+
func (d *decodeGen) assignAndCheck(name, typ string) {
6758
if !d.p.ok() {
6859
return
6960
}
@@ -87,12 +78,21 @@ func (d *decodeGen) structAsTuple(s *Struct) {
8778
}
8879

8980
func (d *decodeGen) structAsMap(s *Struct) {
90-
d.needsField()
81+
82+
if !d.hasField {
83+
d.p.declare("field", "[]byte")
84+
d.hasField = true
85+
}
86+
87+
// Declare the variable that will contain the map length.
9188
sz := randIdent()
9289
d.p.declare(sz, u32)
90+
91+
// Assign to the sz variable the length of the map.
9392
d.assignAndCheck(sz, mapHeader)
9493

95-
d.p.printf("\nfor %s > 0 {\n%s--", sz, sz)
94+
d.p.printf("\nfor %s > 0 {", sz)
95+
d.p.printf("\n%s--", sz)
9696
d.assignAndCheck("field", mapKey)
9797
d.p.print("\nswitch string(field) {")
9898
for i := range s.Fields {
@@ -104,8 +104,10 @@ func (d *decodeGen) structAsMap(s *Struct) {
104104
}
105105
d.p.print("\ndefault:\nerr = dc.Skip()")
106106
d.p.print(errCheck)
107-
d.p.closeBlock() // close switch
107+
108+
d.p.closeBlock() // close switch block
108109
d.p.closeBlock() // close for loop
110+
109111
}
110112

111113
func (d *decodeGen) gBase(b *BaseElem) {

gen/unmarshal.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (u *unmarshalGen) gStruct(s *Struct) {
6060
if s.AsTuple {
6161
u.tuple(s)
6262
} else {
63-
u.mapStruct(s)
63+
u.structAsMap(s)
6464
}
6565
return
6666
}
@@ -78,7 +78,7 @@ func (u *unmarshalGen) tuple(s *Struct) {
7878
}
7979
}
8080

81-
func (u *unmarshalGen) mapStruct(s *Struct) {
81+
func (u *unmarshalGen) structAsMap(s *Struct) {
8282

8383
if !u.hasField {
8484
u.p.declare("field", "[]byte")
@@ -108,7 +108,8 @@ func (u *unmarshalGen) mapStruct(s *Struct) {
108108
u.p.print("\ndefault:\nbts, err = msgp.Skip(bts)")
109109
u.p.print(errCheck)
110110

111-
u.p.print("\n}\n}") // Close switch block and for loop
111+
u.p.closeBlock() // close switch block
112+
u.p.closeBlock() // close for loop
112113

113114
}
114115

0 commit comments

Comments
 (0)