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

Commit 05f6fde

Browse files
committed
various cleanups, improvements to code generator
1 parent 34f3100 commit 05f6fde

File tree

6 files changed

+11
-25
lines changed

6 files changed

+11
-25
lines changed

gen/decode.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func (d *decodeGen) gStruct(s *Struct) {
5151
} else {
5252
d.structAsMap(s)
5353
}
54-
return
5554
}
5655

5756
func (d *decodeGen) assignAndCheck(name, typ string) {
@@ -63,12 +62,10 @@ func (d *decodeGen) assignAndCheck(name, typ string) {
6362
}
6463

6564
func (d *decodeGen) structAsTuple(s *Struct) {
66-
nfields := len(s.Fields)
67-
6865
sz := randIdent()
6966
d.p.declare(sz, u32)
7067
d.assignAndCheck(sz, arrayHeader)
71-
d.p.arrayCheck(strconv.Itoa(nfields), sz)
68+
d.p.arrayCheck(strconv.Itoa(len(s.Fields)), sz)
7269
for i := range s.Fields {
7370
if !d.p.ok() {
7471
return

gen/encode.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (e *encodeGen) writeAndCheck(typ string, argfmt string, arg interface{}) {
3232

3333
func (e *encodeGen) fuseHook() {
3434
if len(e.fuse) > 0 {
35-
e.appendraw(e.fuse)
35+
e.appendRaw(e.fuse)
3636
e.fuse = e.fuse[:0]
3737
}
3838
}
@@ -72,14 +72,13 @@ func (e *encodeGen) gStruct(s *Struct) {
7272
return
7373
}
7474
if s.AsTuple {
75-
e.tuple(s)
75+
e.structAsTuple(s)
7676
} else {
77-
e.structmap(s)
77+
e.structAsMap(s)
7878
}
79-
return
8079
}
8180

82-
func (e *encodeGen) tuple(s *Struct) {
81+
func (e *encodeGen) structAsTuple(s *Struct) {
8382
nfields := len(s.Fields)
8483
data := msgp.AppendArrayHeader(nil, uint32(nfields))
8584
e.p.printf("\n// array header, size %d", nfields)
@@ -95,18 +94,18 @@ func (e *encodeGen) tuple(s *Struct) {
9594
}
9695
}
9796

98-
func (e *encodeGen) appendraw(bts []byte) {
97+
func (e *encodeGen) appendRaw(bts []byte) {
9998
e.p.print("\nerr = en.Append(")
10099
for i, b := range bts {
101-
if i != 0 {
100+
if i > 0 {
102101
e.p.print(", ")
103102
}
104103
e.p.printf("0x%x", b)
105104
}
106105
e.p.print(")\nif err != nil { return }")
107106
}
108107

109-
func (e *encodeGen) structmap(s *Struct) {
108+
func (e *encodeGen) structAsMap(s *Struct) {
110109
nfields := len(s.Fields)
111110
data := msgp.AppendMapHeader(nil, uint32(nfields))
112111
e.p.printf("\n// map header, size %d", nfields)

gen/marshal.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func (m *marshalGen) gStruct(s *Struct) {
8080
} else {
8181
m.mapstruct(s)
8282
}
83-
return
8483
}
8584

8685
func (m *marshalGen) tuple(s *Struct) {

gen/size.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ func builtinSize(typ string) string {
4444
return "msgp." + typ + "Size"
4545
}
4646

47-
// this lets us chain together addition
48-
// operations where possible
47+
// this lets us chain together addition operations where possible
4948
func (s *sizeGen) addConstant(sz string) {
5049
if !s.p.ok() {
5150
return
@@ -187,8 +186,8 @@ func (s *sizeGen) gBase(b *BaseElem) {
187186
vname := randIdent()
188187
s.p.declare(vname, b.BaseType())
189188

190-
// Ensure we don't get "unused variable" warnings from outer slice iterations.
191-
s.p.blankAssign(b.Varname())
189+
// Ensure we don't get "unused variable" errors from outer slice iterations.
190+
s.p.print("\n_ = " + b.Varname())
192191

193192
s.p.printf("\ns += %s", baseSizeExpr(b.Value, vname, b.BaseName()))
194193
s.state = expr

gen/spec.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,6 @@ func (p *printer) declare(name, typ string) {
284284
p.printf("\nvar %s %s", name, typ)
285285
}
286286

287-
// blankAssign writes a newline and an assignment of varName to the blank identifier.
288-
// This pattern is used (for now) to ensure that we don't get "unused variable" errors
289-
// from the compiler.
290-
func (p *printer) blankAssign(varName string) {
291-
p.print("\n_ = " + varName)
292-
}
293-
294287
// resizeMap does:
295288
// if m == nil && size > 0 {
296289
// m = make(type, size)

gen/unmarshal.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func (u *unmarshalGen) gStruct(s *Struct) {
6262
} else {
6363
u.structAsMap(s)
6464
}
65-
return
6665
}
6766

6867
func (u *unmarshalGen) tuple(s *Struct) {

0 commit comments

Comments
 (0)