Skip to content

Commit 8bc3a16

Browse files
committed
code health: extract a msgpack code from the code and tests
The patch replaces the msgpack code by internal wrappers. The msgpack usage have been extracted to msgpack.go file for the code and to msgpack_helper_test.go for tests. It is the same logic for submodules. Part of #124
1 parent b3834e8 commit 8bc3a16

19 files changed

+251
-106
lines changed

client_tools.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package tarantool
22

3-
import (
4-
"gopkg.in/vmihailenco/msgpack.v2"
5-
)
6-
73
// IntKey is utility type for passing integer key to Select*, Update* and Delete*.
84
// It serializes to array with single integer element.
95
type IntKey struct {
106
I int
117
}
128

13-
func (k IntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
9+
func (k IntKey) encodeMsgpackImpl(enc *Encoder) error {
1410
enc.EncodeArrayLen(1)
1511
enc.EncodeInt(k.I)
1612
return nil
@@ -22,7 +18,7 @@ type UintKey struct {
2218
I uint
2319
}
2420

25-
func (k UintKey) EncodeMsgpack(enc *msgpack.Encoder) error {
21+
func (k UintKey) encodeMsgpackImpl(enc *Encoder) error {
2622
enc.EncodeArrayLen(1)
2723
enc.EncodeUint(k.I)
2824
return nil
@@ -34,7 +30,7 @@ type StringKey struct {
3430
S string
3531
}
3632

37-
func (k StringKey) EncodeMsgpack(enc *msgpack.Encoder) error {
33+
func (k StringKey) encodeMsgpackImpl(enc *Encoder) error {
3834
enc.EncodeArrayLen(1)
3935
enc.EncodeString(k.S)
4036
return nil
@@ -46,7 +42,7 @@ type IntIntKey struct {
4642
I1, I2 int
4743
}
4844

49-
func (k IntIntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
45+
func (k IntIntKey) encodeMsgpackImpl(enc *Encoder) error {
5046
enc.EncodeArrayLen(2)
5147
enc.EncodeInt(k.I1)
5248
enc.EncodeInt(k.I2)
@@ -60,7 +56,7 @@ type Op struct {
6056
Arg interface{}
6157
}
6258

63-
func (o Op) EncodeMsgpack(enc *msgpack.Encoder) error {
59+
func (o Op) encodeMsgpackImpl(enc *Encoder) error {
6460
enc.EncodeArrayLen(3)
6561
enc.EncodeString(o.Op)
6662
enc.EncodeInt(o.Field)
@@ -75,7 +71,7 @@ type OpSplice struct {
7571
Replace string
7672
}
7773

78-
func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) error {
74+
func (o OpSplice) encodeMsgpackImpl(enc *Encoder) error {
7975
enc.EncodeArrayLen(5)
8076
enc.EncodeString(o.Op)
8177
enc.EncodeInt(o.Field)

connection.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
"sync"
1515
"sync/atomic"
1616
"time"
17-
18-
"gopkg.in/vmihailenco/msgpack.v2"
1917
)
2018

2119
const requestsMap = 128
@@ -137,7 +135,7 @@ type Connection struct {
137135
rlimit chan struct{}
138136
opts Opts
139137
state uint32
140-
dec *msgpack.Decoder
138+
dec *Decoder
141139
lenbuf [PacketLengthBytes]byte
142140
}
143141

@@ -151,7 +149,7 @@ type connShard struct {
151149
}
152150
bufmut sync.Mutex
153151
buf smallWBuf
154-
enc *msgpack.Encoder
152+
enc *Encoder
155153
_pad [16]uint64 //nolint: unused,structcheck
156154
}
157155

@@ -266,7 +264,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
266264
Greeting: &Greeting{},
267265
control: make(chan struct{}),
268266
opts: opts,
269-
dec: msgpack.NewDecoder(&smallBuf{}),
267+
dec: newDecoder(&smallBuf{}),
270268
}
271269
maxprocs := uint32(runtime.GOMAXPROCS(-1))
272270
if conn.opts.Concurrency == 0 || conn.opts.Concurrency > maxprocs*128 {
@@ -473,7 +471,7 @@ func (conn *Connection) writeAuthRequest(w *bufio.Writer, scramble []byte) (err
473471
requestCode: AuthRequest,
474472
}
475473
var packet smallWBuf
476-
err = request.pack(&packet, msgpack.NewEncoder(&packet), func(enc *msgpack.Encoder) error {
474+
err = request.pack(&packet, newEncoder(&packet), func(enc *Encoder) error {
477475
return enc.Encode(map[uint32]interface{}{
478476
KeyUserName: conn.opts.User,
479477
KeyTuple: []interface{}{string("chap-sha1"), string(scramble)},
@@ -747,7 +745,7 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
747745
return
748746
}
749747

750-
func (conn *Connection) putFuture(fut *Future, body func(*msgpack.Encoder) error) {
748+
func (conn *Connection) putFuture(fut *Future, body func(*Encoder) error) {
751749
shardn := fut.requestId & (conn.opts.Concurrency - 1)
752750
shard := &conn.shard[shardn]
753751
shard.bufmut.Lock()
@@ -760,7 +758,7 @@ func (conn *Connection) putFuture(fut *Future, body func(*msgpack.Encoder) error
760758
firstWritten := shard.buf.Len() == 0
761759
if shard.buf.Cap() == 0 {
762760
shard.buf.b = make([]byte, 0, 128)
763-
shard.enc = msgpack.NewEncoder(&shard.buf)
761+
shard.enc = newEncoder(&shard.buf)
764762
}
765763
blen := shard.buf.Len()
766764
if err := fut.pack(&shard.buf, shard.enc, body); err != nil {

example_custom_unpacking_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"time"
77

88
"github.com/tarantool/go-tarantool"
9-
"gopkg.in/vmihailenco/msgpack.v2"
109
)
1110

1211
type Tuple2 struct {
@@ -24,7 +23,7 @@ type Tuple3 struct {
2423
Members []Member
2524
}
2625

27-
func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
26+
func (c *Tuple2) encodeMsgpackImpl(e *tarantool.Encoder) error {
2827
if err := e.EncodeArrayLen(3); err != nil {
2928
return err
3029
}
@@ -38,7 +37,7 @@ func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
3837
return nil
3938
}
4039

41-
func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
40+
func (c *Tuple2) decodeMsgpackImpl(d *tarantool.Decoder) error {
4241
var err error
4342
var l int
4443
if l, err = d.DecodeArrayLen(); err != nil {

msgpack.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package tarantool
2+
3+
import (
4+
"io"
5+
6+
"gopkg.in/vmihailenco/msgpack.v2"
7+
msgpcode "gopkg.in/vmihailenco/msgpack.v2/codes"
8+
)
9+
10+
type Encoder struct {
11+
*msgpack.Encoder
12+
}
13+
14+
type Decoder struct {
15+
*msgpack.Decoder
16+
}
17+
18+
func newEncoder(w io.Writer) *Encoder {
19+
enc := msgpack.NewEncoder(w)
20+
return &Encoder{Encoder: enc}
21+
}
22+
23+
func newDecoder(r io.Reader) *Decoder {
24+
dec := msgpack.NewDecoder(r)
25+
return &Decoder{Decoder: dec}
26+
}
27+
28+
func msgpackIsUint(code byte) bool {
29+
return code == msgpcode.Uint8 || code == msgpcode.Uint16 ||
30+
code == msgpcode.Uint32 || code == msgpcode.Uint64 ||
31+
msgpcode.IsFixedNum(code)
32+
}
33+
34+
func msgpackIsMap(code byte) bool {
35+
return code == msgpcode.Map16 || code == msgpcode.Map32 || msgpcode.IsFixedMap(code)
36+
}
37+
38+
func msgpackIsArray(code byte) bool {
39+
return code == msgpcode.Array16 || code == msgpcode.Array32 ||
40+
msgpcode.IsFixedArray(code)
41+
}
42+
43+
func msgpackIsString(code byte) bool {
44+
return msgpcode.IsFixedString(code) || code == msgpcode.Str8 ||
45+
code == msgpcode.Str16 || code == msgpcode.Str32
46+
}
47+
48+
func (s *single) DecodeMsgpack(d *msgpack.Decoder) error {
49+
return s.decodeMsgpackImpl(&Decoder{d})
50+
}
51+
52+
func (space *Space) DecodeMsgpack(d *msgpack.Decoder) error {
53+
return space.decodeMsgpackImpl(&Decoder{d})
54+
}
55+
56+
func (field *Field) DecodeMsgpack(d *msgpack.Decoder) error {
57+
return field.decodeMsgpackImpl(&Decoder{d})
58+
}
59+
60+
func (index *Index) DecodeMsgpack(d *msgpack.Decoder) error {
61+
return index.decodeMsgpackImpl(&Decoder{d})
62+
}
63+
64+
func (indexField *IndexField) DecodeMsgpack(d *msgpack.Decoder) error {
65+
return indexField.decodeMsgpackImpl(&Decoder{d})
66+
}
67+
68+
func (meta *ColumnMetaData) DecodeMsgpack(d *msgpack.Decoder) error {
69+
return meta.decodeMsgpackImpl(&Decoder{d})
70+
}
71+
72+
func (info *SQLInfo) DecodeMsgpack(d *msgpack.Decoder) error {
73+
return info.decodeMsgpackImpl(&Decoder{d})
74+
}
75+
76+
func (k IntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
77+
return k.encodeMsgpackImpl(&Encoder{enc})
78+
}
79+
80+
func (k UintKey) EncodeMsgpack(enc *msgpack.Encoder) error {
81+
return k.encodeMsgpackImpl(&Encoder{enc})
82+
}
83+
84+
func (k StringKey) EncodeMsgpack(enc *msgpack.Encoder) error {
85+
return k.encodeMsgpackImpl(&Encoder{enc})
86+
}
87+
88+
func (k IntIntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
89+
return k.encodeMsgpackImpl(&Encoder{enc})
90+
}
91+
92+
func (o Op) EncodeMsgpack(enc *msgpack.Encoder) error {
93+
return o.encodeMsgpackImpl(&Encoder{enc})
94+
}
95+
96+
func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) error {
97+
return o.encodeMsgpackImpl(&Encoder{enc})
98+
}

msgpack_helper_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package tarantool_test
2+
3+
import (
4+
. "github.com/tarantool/go-tarantool"
5+
"gopkg.in/vmihailenco/msgpack.v2"
6+
)
7+
8+
func (m *Member) EncodeMsgpack(e *msgpack.Encoder) error {
9+
return m.encodeMsgpackImpl(&Encoder{e})
10+
}
11+
12+
func (m *Member) DecodeMsgpack(d *msgpack.Decoder) error {
13+
return m.decodeMsgpackImpl(&Decoder{d})
14+
}
15+
16+
func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
17+
return c.encodeMsgpackImpl(&Encoder{e})
18+
}
19+
20+
func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
21+
return c.decodeMsgpackImpl(&Decoder{d})
22+
}

queue/example_msgpack_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,21 @@ import (
1515

1616
"github.com/tarantool/go-tarantool"
1717
"github.com/tarantool/go-tarantool/queue"
18-
"gopkg.in/vmihailenco/msgpack.v2"
1918
)
2019

2120
type dummyData struct {
2221
Dummy bool
2322
}
2423

25-
func (c *dummyData) DecodeMsgpack(d *msgpack.Decoder) error {
24+
func (c *dummyData) decodeMsgpackImpl(d *tarantool.Decoder) error {
2625
var err error
2726
if c.Dummy, err = d.DecodeBool(); err != nil {
2827
return err
2928
}
3029
return nil
3130
}
3231

33-
func (c *dummyData) EncodeMsgpack(e *msgpack.Encoder) error {
32+
func (c *dummyData) encodeMsgpackImpl(e *tarantool.Encoder) error {
3433
return e.EncodeBool(c.Dummy)
3534
}
3635

queue/msgpack.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package queue
2+
3+
import (
4+
"github.com/tarantool/go-tarantool"
5+
"gopkg.in/vmihailenco/msgpack.v2"
6+
)
7+
8+
func (qd *queueData) DecodeMsgpack(d *msgpack.Decoder) error {
9+
return qd.decodeMsgpackImpl(&tarantool.Decoder{Decoder: d})
10+
}
11+
12+
func (t *Task) DecodeMsgpack(d *msgpack.Decoder) error {
13+
return t.decodeMsgpackImpl(&tarantool.Decoder{Decoder: d})
14+
}

queue/msgpack_helper_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package queue_test
2+
3+
import (
4+
. "github.com/tarantool/go-tarantool"
5+
"gopkg.in/vmihailenco/msgpack.v2"
6+
)
7+
8+
func (c *customData) DecodeMsgpack(d *msgpack.Decoder) error {
9+
return c.decodeMsgpackImpl(&Decoder{Decoder: d})
10+
}
11+
12+
func (c *customData) EncodeMsgpack(e *msgpack.Encoder) error {
13+
return c.encodeMsgpackImpl(&Encoder{Encoder: e})
14+
}
15+
16+
func (c *dummyData) DecodeMsgpack(d *msgpack.Decoder) error {
17+
return c.decodeMsgpackImpl(&Decoder{Decoder: d})
18+
}
19+
20+
func (c *dummyData) EncodeMsgpack(e *msgpack.Encoder) error {
21+
return c.encodeMsgpackImpl(&Encoder{Encoder: e})
22+
}

queue/queue.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"time"
1414

1515
"github.com/tarantool/go-tarantool"
16-
msgpack "gopkg.in/vmihailenco/msgpack.v2"
1716
)
1817

1918
// Queue is a handle to Tarantool queue's tube.
@@ -339,7 +338,7 @@ type queueData struct {
339338
result interface{}
340339
}
341340

342-
func (qd *queueData) DecodeMsgpack(d *msgpack.Decoder) error {
341+
func (qd *queueData) decodeMsgpackImpl(d *tarantool.Decoder) error {
343342
var err error
344343
var l int
345344
if l, err = d.DecodeArrayLen(); err != nil {

queue/queue_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
. "github.com/tarantool/go-tarantool"
1212
"github.com/tarantool/go-tarantool/queue"
1313
"github.com/tarantool/go-tarantool/test_helpers"
14-
"gopkg.in/vmihailenco/msgpack.v2"
1514
)
1615

1716
var server = "127.0.0.1:3013"
@@ -215,7 +214,7 @@ type customData struct {
215214
customField string
216215
}
217216

218-
func (c *customData) DecodeMsgpack(d *msgpack.Decoder) error {
217+
func (c *customData) decodeMsgpackImpl(d *Decoder) error {
219218
var err error
220219
var l int
221220
if l, err = d.DecodeArrayLen(); err != nil {
@@ -230,7 +229,7 @@ func (c *customData) DecodeMsgpack(d *msgpack.Decoder) error {
230229
return nil
231230
}
232231

233-
func (c *customData) EncodeMsgpack(e *msgpack.Encoder) error {
232+
func (c *customData) encodeMsgpackImpl(e *Encoder) error {
234233
if err := e.EncodeArrayLen(1); err != nil {
235234
return err
236235
}

queue/task.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package queue
33
import (
44
"fmt"
55

6-
msgpack "gopkg.in/vmihailenco/msgpack.v2"
6+
"github.com/tarantool/go-tarantool"
77
)
88

99
// Task represents a task from Tarantool queue's tube.
@@ -14,7 +14,7 @@ type Task struct {
1414
q *queue
1515
}
1616

17-
func (t *Task) DecodeMsgpack(d *msgpack.Decoder) error {
17+
func (t *Task) decodeMsgpackImpl(d *tarantool.Decoder) error {
1818
var err error
1919
var l int
2020
if l, err = d.DecodeArrayLen(); err != nil {

0 commit comments

Comments
 (0)