Skip to content

Commit 497983b

Browse files
committed
msgpack: fix number types conversions after decoding
msgpack v2.9.2 decodes all numbers as uint[1] or int[2], but msgpack.v5 decodes numbers as a MessagePack type[3]. We need to unify the type conversions in the code. 1. https://github.com/vmihailenco/msgpack/blob/23644a15054d8b9f8a9fca3e041f3419504b9c21/decode.go#L283 2. https://github.com/vmihailenco/msgpack/blob/23644a15054d8b9f8a9fca3e041f3419504b9c21/decode.go#L285 3. https://github.com/vmihailenco/msgpack/blob/233c977ae92b215a9aa7eb420bbe67da99f05ab6/decode.go#L410 Part of #124
1 parent 9bf96b9 commit 497983b

File tree

10 files changed

+98
-86
lines changed

10 files changed

+98
-86
lines changed

call_16_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestConnection_Call(t *testing.T) {
2222
if err != nil {
2323
t.Errorf("Failed to use Call")
2424
}
25-
if resp.Data[0].([]interface{})[0].(uint64) != 2 {
25+
if val, err := convertUint64(resp.Data[0].([]interface{})[0]); err != nil || val != 2 {
2626
t.Errorf("result is not {{1}} : %v", resp.Data)
2727
}
2828
}
@@ -39,7 +39,7 @@ func TestCallRequest(t *testing.T) {
3939
if err != nil {
4040
t.Errorf("Failed to use Call")
4141
}
42-
if resp.Data[0].([]interface{})[0].(uint64) != 2 {
42+
if val, err := convertUint64(resp.Data[0].([]interface{})[0]); err != nil || val != 2 {
4343
t.Errorf("result is not {{1}} : %v", resp.Data)
4444
}
4545
}

call_17_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestConnection_Call(t *testing.T) {
2222
if err != nil {
2323
t.Errorf("Failed to use Call")
2424
}
25-
if resp.Data[0].(uint64) != 2 {
25+
if val, err := convertUint64(resp.Data[0]); err != nil || val != 2 {
2626
t.Errorf("result is not {{1}} : %v", resp.Data)
2727
}
2828
}
@@ -39,7 +39,7 @@ func TestCallRequest(t *testing.T) {
3939
if err != nil {
4040
t.Errorf("Failed to use Call")
4141
}
42-
if resp.Data[0].(uint64) != 2 {
42+
if val, err := convertUint64(resp.Data[0]); err != nil || val != 2 {
4343
t.Errorf("result is not {{1}} : %v", resp.Data)
4444
}
4545
}

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ func ExampleFuture_GetIterator() {
240240
resp := it.Value()
241241
if resp.Code == tarantool.PushCode {
242242
// It is a push message.
243-
fmt.Printf("push message: %d\n", resp.Data[0].(uint64))
243+
fmt.Printf("push message: %v\n", resp.Data[0])
244244
} else if resp.Code == tarantool.OkCode {
245245
// It is a regular response.
246-
fmt.Printf("response: %d", resp.Data[0].(uint64))
246+
fmt.Printf("response: %v", resp.Data[0])
247247
} else {
248248
fmt.Printf("an unexpected response code %d", resp.Code)
249249
}

multi/call_16_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func TestCall(t *testing.T) {
2323
defer multiConn.Close()
2424

2525
// Call16
26-
resp, err = multiConn.Call("simple_incr", []interface{}{1})
26+
resp, err = multiConn.Call("simple_concat", []interface{}{"t"})
2727
if err != nil {
2828
t.Fatalf("Failed to use Call: %s", err.Error())
2929
}
30-
if resp.Data[0].([]interface{})[0].(uint64) != 2 {
30+
if resp.Data[0].([]interface{})[0].(string) != "tt" {
3131
t.Fatalf("result is not {{1}} : %v", resp.Data)
3232
}
3333
}

multi/call_17_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ func TestCall(t *testing.T) {
2323
defer multiConn.Close()
2424

2525
// Call17
26-
resp, err = multiConn.Call("simple_incr", []interface{}{1})
26+
resp, err = multiConn.Call("simple_concat", []interface{}{"t"})
2727
if err != nil {
2828
t.Fatalf("Failed to use Call: %s", err.Error())
2929
}
30-
if resp.Data[0].(uint64) != 2 {
30+
if resp.Data[0].(string) != "tt" {
3131
t.Fatalf("result is not {{1}} : %v", resp.Data)
3232
}
3333
}

multi/config.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ box.once("init", function()
3030
box.schema.user.grant('test', 'create', 'sequence')
3131
end)
3232

33-
local function simple_incr(a)
34-
return a + 1
33+
local function simple_concat(a)
34+
return a .. a
3535
end
3636

37-
rawset(_G, 'simple_incr', simple_incr)
37+
rawset(_G, 'simple_concat', simple_concat)
3838

3939
-- Set listen only when every other thing is configured.
4040
box.cfg{

multi/multi_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ func TestCall17(t *testing.T) {
226226
defer multiConn.Close()
227227

228228
// Call17
229-
resp, err = multiConn.Call17("simple_incr", []interface{}{1})
229+
resp, err = multiConn.Call17("simple_concat", []interface{}{"s"})
230230
if err != nil {
231231
t.Fatalf("Failed to use Call: %s", err.Error())
232232
}
233-
if resp.Data[0].(uint64) != 2 {
233+
if resp.Data[0].(string) != "ss" {
234234
t.Fatalf("result is not {{1}} : %v", resp.Data)
235235
}
236236
}

queue/msgpack.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"gopkg.in/vmihailenco/msgpack.v2"
66
)
77

8+
func (r *kickResult) DecodeMsgpack(d *msgpack.Decoder) error {
9+
return r.decodeMsgpackImpl(&tarantool.Decoder{Decoder: d})
10+
}
11+
812
func (qd *queueData) DecodeMsgpack(d *msgpack.Decoder) error {
913
return qd.decodeMsgpackImpl(&tarantool.Decoder{Decoder: d})
1014
}

queue/queue.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,27 @@ func (q *queue) produce(cmd string, params ...interface{}) (string, error) {
283283
return qd.task.status, nil
284284
}
285285

286+
type kickResult struct {
287+
id uint64
288+
}
289+
290+
func (r *kickResult) decodeMsgpackImpl(d *tarantool.Decoder) (err error) {
291+
var l int
292+
if l, err = d.DecodeArrayLen(); err != nil {
293+
return err
294+
}
295+
if l > 1 {
296+
return fmt.Errorf("array len doesn't match for queue kick data: %d", l)
297+
}
298+
r.id, err = d.DecodeUint64()
299+
return
300+
}
301+
286302
// Reverse the effect of a bury request on one or more tasks.
287303
func (q *queue) Kick(count uint64) (uint64, error) {
288-
resp, err := q.conn.Call17(q.cmds.kick, []interface{}{count})
289-
var id uint64
290-
if err == nil {
291-
id = resp.Data[0].(uint64)
292-
}
293-
return id, err
304+
var r kickResult
305+
err := q.conn.Call17Typed(q.cmds.kick, []interface{}{count}, &r)
306+
return r.id, err
294307
}
295308

296309
// Delete the task identified by its id.

0 commit comments

Comments
 (0)