Skip to content

Commit 88e921b

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 0b0594a commit 88e921b

29 files changed

+395
-188
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)
@@ -148,7 +144,7 @@ type OpSplice struct {
148144
Replace string
149145
}
150146

151-
func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) error {
147+
func (o OpSplice) encodeMsgpackImpl(enc *Encoder) error {
152148
enc.EncodeArrayLen(5)
153149
enc.EncodeString(o.Op)
154150
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
@@ -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

@@ -267,7 +265,7 @@ func Connect(addr string, opts Opts) (conn *Connection, err error) {
267265
Greeting: &Greeting{},
268266
control: make(chan struct{}),
269267
opts: opts,
270-
dec: msgpack.NewDecoder(&smallBuf{}),
268+
dec: newDecoder(&smallBuf{}),
271269
}
272270
maxprocs := uint32(runtime.GOMAXPROCS(-1))
273271
if conn.opts.Concurrency == 0 || conn.opts.Concurrency > maxprocs*128 {
@@ -468,7 +466,7 @@ func (conn *Connection) dial() (err error) {
468466
return
469467
}
470468

471-
func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32, req Request, res SchemaResolver) (err error) {
469+
func pack(h *smallWBuf, enc *Encoder, reqid uint32, req Request, res SchemaResolver) (err error) {
472470
hl := h.Len()
473471
h.Write([]byte{
474472
0xce, 0, 0, 0, 0, // Length.
@@ -495,7 +493,7 @@ func pack(h *smallWBuf, enc *msgpack.Encoder, reqid uint32, req Request, res Sch
495493
func (conn *Connection) writeAuthRequest(w *bufio.Writer, scramble []byte) (err error) {
496494
var packet smallWBuf
497495
req := newAuthRequest(conn.opts.User, string(scramble))
498-
err = pack(&packet, msgpack.NewEncoder(&packet), 0, req, conn.Schema)
496+
err = pack(&packet, newEncoder(&packet), 0, req, conn.Schema)
499497

500498
if err != nil {
501499
return errors.New("auth: pack error " + err.Error())
@@ -807,7 +805,7 @@ func (conn *Connection) putFuture(fut *Future, req Request) {
807805
firstWritten := shard.buf.Len() == 0
808806
if shard.buf.Cap() == 0 {
809807
shard.buf.b = make([]byte, 0, 128)
810-
shard.enc = msgpack.NewEncoder(&shard.buf)
808+
shard.enc = newEncoder(&shard.buf)
811809
}
812810
blen := shard.buf.Len()
813811
reqid := fut.requestId

datetime/datetime.go

-9
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313
"encoding/binary"
1414
"fmt"
1515
"time"
16-
17-
"gopkg.in/vmihailenco/msgpack.v2"
1816
)
1917

2018
// Datetime MessagePack serialization schema is an MP_EXT extension, which
@@ -101,9 +99,6 @@ func (dtime *Datetime) ToTime() time.Time {
10199
return dtime.time
102100
}
103101

104-
var _ msgpack.Marshaler = (*Datetime)(nil)
105-
var _ msgpack.Unmarshaler = (*Datetime)(nil)
106-
107102
func (dtime *Datetime) MarshalMsgpack() ([]byte, error) {
108103
tm := dtime.ToTime()
109104

@@ -152,7 +147,3 @@ func (tm *Datetime) UnmarshalMsgpack(b []byte) error {
152147
}
153148
return err
154149
}
155-
156-
func init() {
157-
msgpack.RegisterExt(datetime_extId, &Datetime{})
158-
}

datetime/datetime_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
. "github.com/tarantool/go-tarantool"
1313
. "github.com/tarantool/go-tarantool/datetime"
1414
"github.com/tarantool/go-tarantool/test_helpers"
15-
"gopkg.in/vmihailenco/msgpack.v2"
1615
)
1716

1817
var lesserBoundaryTimes = []time.Time{
@@ -270,7 +269,7 @@ type Tuple1 struct {
270269
Datetime Datetime
271270
}
272271

273-
func (t *Tuple1) EncodeMsgpack(e *msgpack.Encoder) error {
272+
func (t *Tuple1) encodeMsgpackImpl(e *Encoder) error {
274273
if err := e.EncodeArrayLen(2); err != nil {
275274
return err
276275
}
@@ -280,7 +279,7 @@ func (t *Tuple1) EncodeMsgpack(e *msgpack.Encoder) error {
280279
return nil
281280
}
282281

283-
func (t *Tuple1) DecodeMsgpack(d *msgpack.Decoder) error {
282+
func (t *Tuple1) decodeMsgpackImpl(d *Decoder) error {
284283
var err error
285284
var l int
286285
if l, err = d.DecodeArrayLen(); err != nil {
@@ -296,7 +295,7 @@ func (t *Tuple1) DecodeMsgpack(d *msgpack.Decoder) error {
296295
return nil
297296
}
298297

299-
func (ev *Event) EncodeMsgpack(e *msgpack.Encoder) error {
298+
func (ev *Event) encodeMsgpackImpl(e *Encoder) error {
300299
if err := e.EncodeArrayLen(2); err != nil {
301300
return err
302301
}
@@ -309,7 +308,7 @@ func (ev *Event) EncodeMsgpack(e *msgpack.Encoder) error {
309308
return nil
310309
}
311310

312-
func (ev *Event) DecodeMsgpack(d *msgpack.Decoder) error {
311+
func (ev *Event) decodeMsgpackImpl(d *Decoder) error {
313312
var err error
314313
var l int
315314
if l, err = d.DecodeArrayLen(); err != nil {
@@ -329,7 +328,7 @@ func (ev *Event) DecodeMsgpack(d *msgpack.Decoder) error {
329328
return nil
330329
}
331330

332-
func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
331+
func (c *Tuple2) encodeMsgpackImpl(e *Encoder) error {
333332
if err := e.EncodeArrayLen(3); err != nil {
334333
return err
335334
}
@@ -343,7 +342,7 @@ func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
343342
return nil
344343
}
345344

346-
func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
345+
func (c *Tuple2) decodeMsgpackImpl(d *Decoder) error {
347346
var err error
348347
var l int
349348
if l, err = d.DecodeArrayLen(); err != nil {
@@ -525,7 +524,7 @@ func TestMPEncode(t *testing.T) {
525524
if err != nil {
526525
t.Fatalf("Unable to create Datetime from %s: %s", tm, err)
527526
}
528-
buf, err := msgpack.Marshal(dt)
527+
buf, err := marshal(dt)
529528
if err != nil {
530529
t.Fatalf("Marshalling failed: %s", err.Error())
531530
}
@@ -549,7 +548,7 @@ func TestMPDecode(t *testing.T) {
549548
}
550549
buf, _ := hex.DecodeString(testcase.mpBuf)
551550
var v Datetime
552-
err = msgpack.Unmarshal(buf, &v)
551+
err = unmarshal(buf, &v)
553552
if err != nil {
554553
t.Fatalf("Unmarshalling failed: %s", err.Error())
555554
}

datetime/msgpack.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package datetime
2+
3+
import (
4+
"gopkg.in/vmihailenco/msgpack.v2"
5+
)
6+
7+
func init() {
8+
msgpack.RegisterExt(datetime_extId, &Datetime{})
9+
}

datetime/msgpack_helper_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package datetime_test
2+
3+
import (
4+
. "github.com/tarantool/go-tarantool"
5+
"gopkg.in/vmihailenco/msgpack.v2"
6+
)
7+
8+
func (t *Tuple1) EncodeMsgpack(e *msgpack.Encoder) error {
9+
return t.encodeMsgpackImpl(&Encoder{Encoder: e})
10+
}
11+
12+
func (t *Tuple1) DecodeMsgpack(d *msgpack.Decoder) error {
13+
return t.decodeMsgpackImpl(&Decoder{Decoder: d})
14+
}
15+
16+
func (ev *Event) EncodeMsgpack(e *msgpack.Encoder) error {
17+
return ev.encodeMsgpackImpl(&Encoder{Encoder: e})
18+
}
19+
20+
func (ev *Event) DecodeMsgpack(d *msgpack.Decoder) error {
21+
return ev.decodeMsgpackImpl(&Decoder{Decoder: d})
22+
}
23+
24+
func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
25+
return c.encodeMsgpackImpl(&Encoder{Encoder: e})
26+
}
27+
28+
func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
29+
return c.decodeMsgpackImpl(&Decoder{Decoder: d})
30+
}
31+
32+
func marshal(v interface{}) ([]byte, error) {
33+
return msgpack.Marshal(v)
34+
}
35+
36+
func unmarshal(data []byte, v interface{}) error {
37+
return msgpack.Unmarshal(data, v)
38+
}

decimal/decimal.go

-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"fmt"
2020

2121
"github.com/shopspring/decimal"
22-
"gopkg.in/vmihailenco/msgpack.v2"
2322
)
2423

2524
// Decimal numbers have 38 digits of precision, that is, the total
@@ -56,9 +55,6 @@ func NewDecimalFromString(src string) (result *Decimal, err error) {
5655
return
5756
}
5857

59-
var _ msgpack.Marshaler = (*Decimal)(nil)
60-
var _ msgpack.Unmarshaler = (*Decimal)(nil)
61-
6258
func (decNum *Decimal) MarshalMsgpack() ([]byte, error) {
6359
one := decimal.NewFromInt(1)
6460
maxSupportedDecimal := decimal.New(1, DecimalPrecision).Sub(one) // 10^DecimalPrecision - 1
@@ -99,7 +95,3 @@ func (decNum *Decimal) UnmarshalMsgpack(b []byte) error {
9995

10096
return nil
10197
}
102-
103-
func init() {
104-
msgpack.RegisterExt(decimalExtID, &Decimal{})
105-
}

0 commit comments

Comments
 (0)