Skip to content

Commit 6038440

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 eb72cb3 commit 6038440

19 files changed

+251
-106
lines changed

client_tools.go

+6-10
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

+6-8
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
@@ -132,7 +130,7 @@ type Connection struct {
132130
rlimit chan struct{}
133131
opts Opts
134132
state uint32
135-
dec *msgpack.Decoder
133+
dec *Decoder
136134
lenbuf [PacketLengthBytes]byte
137135
}
138136

@@ -146,7 +144,7 @@ type connShard struct {
146144
}
147145
bufmut sync.Mutex
148146
buf smallWBuf
149-
enc *msgpack.Encoder
147+
enc *Encoder
150148
_pad [16]uint64 //nolint: unused,structcheck
151149
}
152150

@@ -235,7 +233,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
235233
Greeting: &Greeting{},
236234
control: make(chan struct{}),
237235
opts: opts,
238-
dec: msgpack.NewDecoder(&smallBuf{}),
236+
dec: newDecoder(&smallBuf{}),
239237
}
240238
maxprocs := uint32(runtime.GOMAXPROCS(-1))
241239
if conn.opts.Concurrency == 0 || conn.opts.Concurrency > maxprocs*128 {
@@ -434,7 +432,7 @@ func (conn *Connection) writeAuthRequest(w *bufio.Writer, scramble []byte) (err
434432
requestCode: AuthRequest,
435433
}
436434
var packet smallWBuf
437-
err = request.pack(&packet, msgpack.NewEncoder(&packet), func(enc *msgpack.Encoder) error {
435+
err = request.pack(&packet, newEncoder(&packet), func(enc *Encoder) error {
438436
return enc.Encode(map[uint32]interface{}{
439437
KeyUserName: conn.opts.User,
440438
KeyTuple: []interface{}{string("chap-sha1"), string(scramble)},
@@ -708,7 +706,7 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
708706
return
709707
}
710708

711-
func (conn *Connection) putFuture(fut *Future, body func(*msgpack.Encoder) error) {
709+
func (conn *Connection) putFuture(fut *Future, body func(*Encoder) error) {
712710
shardn := fut.requestId & (conn.opts.Concurrency - 1)
713711
shard := &conn.shard[shardn]
714712
shard.bufmut.Lock()
@@ -721,7 +719,7 @@ func (conn *Connection) putFuture(fut *Future, body func(*msgpack.Encoder) error
721719
firstWritten := shard.buf.Len() == 0
722720
if shard.buf.Cap() == 0 {
723721
shard.buf.b = make([]byte, 0, 128)
724-
shard.enc = msgpack.NewEncoder(&shard.buf)
722+
shard.enc = newEncoder(&shard.buf)
725723
}
726724
blen := shard.buf.Len()
727725
if err := fut.pack(&shard.buf, shard.enc, body); err != nil {

example_custom_unpacking_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tarantool_test
33
import (
44
"fmt"
55
"github.com/tarantool/go-tarantool"
6-
"gopkg.in/vmihailenco/msgpack.v2"
76
"log"
87
"time"
98
)
@@ -23,7 +22,7 @@ type Tuple3 struct {
2322
Members []Member
2423
}
2524

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

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

msgpack.go

+98
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

+22
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

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@ import (
1414

1515
"github.com/tarantool/go-tarantool"
1616
"github.com/tarantool/go-tarantool/queue"
17-
"gopkg.in/vmihailenco/msgpack.v2"
1817
"log"
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

+14
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

+22
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

+1-2
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

+2-3
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

+2-2
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)