Skip to content
Snippets Groups Projects
Commit 7db0dfe0 authored by Roman Tsisyk's avatar Roman Tsisyk
Browse files

Fix #506: Additional parameters to tuple:unpack()

parent b4963295
No related branches found
No related tags found
No related merge requests found
......@@ -114,23 +114,38 @@ local tuple_totable_mt = {
__serialize = 'seq'; -- enables flow mode for yaml
}
local function tuple_totable(tuple)
local function tuple_totable(tuple, i, j)
-- use a precreated iterator for tuple_next
builtin.tuple_rewind(next_it, tuple)
local ret = {}
while true do
local field = builtin.tuple_next(next_it)
if field == nil then
break
local field
if i ~= nil then
if i < 1 then
error('tuple.totable: invalid second argument')
end
field = builtin.tuple_seek(next_it, i - 1)
else
i = 1
field = builtin.tuple_next(next_it)
end
if j ~= nil then
if j <= 0 then
error('tuple.totable: invalid third argument')
end
else
j = 4294967295
end
local ret = {}
while field ~= nil and i <= j do
local val = msgpackffi.decode_unchecked(field)
table.insert(ret, val)
i = i + 1
field = builtin.tuple_next(next_it)
end
return setmetatable(ret, tuple_totable_mt)
end
local function tuple_unpack(tuple)
return unpack(tuple_totable(tuple))
local function tuple_unpack(tuple, i, j)
return unpack(tuple_totable(tuple, i, j))
end
local function tuple_find(tuple, offset, val)
......
......@@ -227,24 +227,92 @@ box.tuple.new{'x', 'y', 'z', {1, 2}, {c = 3, d = 4}, {e = 5, f = 6}}
---
- ['x', 'y', 'z', [1, 2], {'c': 3, 'd': 4}, {'e': 5, 'f': 6}]
...
--
-- A test case for #107 "box.tuple.unpack asserts on extra arguments"
--
t=box.tuple.new{'a','b','c'}
---
...
t:unpack(5)
t:totable()
---
- ['a', 'b', 'c']
...
t:unpack()
---
- a
- b
- c
...
t:unpack(1, 2, 3, 4, 5)
t:totable(1)
---
- ['a', 'b', 'c']
...
t:unpack(1)
---
- a
- b
- c
...
t:totable(2)
---
- ['b', 'c']
...
t:unpack(2)
---
- b
- c
...
t:totable(1, 3)
---
- ['a', 'b', 'c']
...
t:unpack(1, 3)
---
- a
- b
- c
...
t:totable(2, 3)
---
- ['b', 'c']
...
t:unpack(2, 3)
---
- b
- c
...
t:totable(2, 4)
---
- ['b', 'c']
...
t:unpack(2, 4)
---
- b
- c
...
t:totable(nil, 2)
---
- ['a', 'b']
...
t:unpack(nil, 2)
---
- a
- b
...
t:totable(2, 1)
---
- []
...
t:unpack(2, 1)
---
...
t:totable(0)
---
- error: '[string "-- tuple.lua (internal file)..."]:123: tuple.totable: invalid second
argument'
...
t:totable(1, 0)
---
- error: '[string "-- tuple.lua (internal file)..."]:132: tuple.totable: invalid third
argument'
...
--
-- Check that tuple:totable correctly sets serializer hints
--
......@@ -735,12 +803,12 @@ t = box.tuple.new({'a', 'b', 'c', 'd', 'e'})
...
t:update()
---
- error: '[string "-- tuple.lua (internal file)..."]:157: Usage: tuple:update({ {
- error: '[string "-- tuple.lua (internal file)..."]:172: Usage: tuple:update({ {
op, field, arg}+ })'
...
t:update(10)
---
- error: '[string "-- tuple.lua (internal file)..."]:157: Usage: tuple:update({ {
- error: '[string "-- tuple.lua (internal file)..."]:172: Usage: tuple:update({ {
op, field, arg}+ })'
...
t:update({})
......
......@@ -66,12 +66,26 @@ box.tuple.new{{1, 2}, 'x', 'y', 'z', {c = 3, d = 4}, {e = 5, f = 6}}
box.tuple.new('x', 'y', 'z', {1, 2}, {c = 3, d = 4}, {e = 5, f = 6})
box.tuple.new{'x', 'y', 'z', {1, 2}, {c = 3, d = 4}, {e = 5, f = 6}}
--
-- A test case for #107 "box.tuple.unpack asserts on extra arguments"
--
t=box.tuple.new{'a','b','c'}
t:unpack(5)
t:unpack(1, 2, 3, 4, 5)
t:totable()
t:unpack()
t:totable(1)
t:unpack(1)
t:totable(2)
t:unpack(2)
t:totable(1, 3)
t:unpack(1, 3)
t:totable(2, 3)
t:unpack(2, 3)
t:totable(2, 4)
t:unpack(2, 4)
t:totable(nil, 2)
t:unpack(nil, 2)
t:totable(2, 1)
t:unpack(2, 1)
t:totable(0)
t:totable(1, 0)
--
-- Check that tuple:totable correctly sets serializer hints
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment