Skip to content

Commit 0519abf

Browse files
committed
New msgpack api
1 parent a1d5352 commit 0519abf

File tree

9 files changed

+397
-152
lines changed

9 files changed

+397
-152
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local msgpack = require('msgpack')
2+
3+
local mp_from_array = msgpack.object({ 10, 20, 30 })
4+
local mp_from_table = msgpack.object({ band_name = 'The Beatles', year = 1960 })
5+
local mp_from_tuple = msgpack.object(box.tuple.new(1, 'The Beatles', 1960))
6+
7+
-- Get MsgPack data by the specified index or key
8+
local mp_array_get_by_index = mp_from_array[1] -- Returns 10
9+
local mp_table_get_by_key = mp_from_table['band_name'] -- Returns 'The Beatles'
10+
local mp_table_get_by_nonexistent_key = mp_from_table['rating'] -- Returns nil
11+
local mp_tuple_get_by_index = mp_from_tuple[3] -- Returns 1960
12+
13+
local luatest = require('luatest')
14+
local test_group = luatest.group()
15+
test_group.test_returns_status = function()
16+
luatest.assert_equals(mp_array_get_by_index, 10)
17+
luatest.assert_equals(mp_table_get_by_key, 'The Beatles')
18+
luatest.assert_equals(mp_table_get_by_nonexistent_key, nil)
19+
luatest.assert_equals(mp_tuple_get_by_index, 1960)
20+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local msgpack = require('msgpack')
2+
3+
local mp_from_string = msgpack.object('hello world')
4+
5+
-- Check if the given argument is a MsgPack object
6+
local mp_is_object = msgpack.is_object(mp_from_string) -- Returns true
7+
local string_is_object = msgpack.is_object('hello world') -- Returns false
8+
9+
local luatest = require('luatest')
10+
local test_group = luatest.group()
11+
test_group.test_returns_status = function()
12+
luatest.assert_equals(mp_is_object, true)
13+
luatest.assert_equals(string_is_object, false)
14+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local msgpack = require('msgpack')
2+
3+
local mp_map = msgpack.object({ foo = 'a',
4+
bar = { 'b1', 'b2', 'b3' } })
5+
local mp_map_iterator = mp_map:iterator()
6+
7+
local size = mp_map_iterator:decode_map_header() -- returns 2
8+
local first = mp_map_iterator:decode() -- returns 'foo'
9+
local second = mp_map_iterator:decode() -- returns 'a'
10+
mp_map_iterator:skip() -- returns none, skips 'bar'
11+
local fourth = mp_map_iterator:decode() -- returns ['b1', 'b2', 'b3']
12+
13+
local luatest = require('luatest')
14+
local test_group = luatest.group()
15+
test_group.test_returns_status = function()
16+
luatest.assert_equals(size, 2)
17+
luatest.assert_equals(first, 'foo')
18+
luatest.assert_equals(second, 'a')
19+
luatest.assert_equals(fourth, { 'b1', 'b2', 'b3' })
20+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local msgpack = require('msgpack')
2+
3+
local mp_array = msgpack.object({ 10, 20, 30, 40 })
4+
local mp_array_iterator = mp_array:iterator()
5+
6+
local size = mp_array_iterator:decode_array_header() -- returns 4
7+
local first = mp_array_iterator:decode() -- returns 10
8+
local mp_array_new = mp_array_iterator:take_array(2)
9+
local mp_array_new_decoded = mp_array_new:decode() -- returns {20, 30}
10+
local fourth = mp_array_iterator:decode() -- returns 40
11+
12+
local luatest = require('luatest')
13+
local test_group = luatest.group()
14+
test_group.test_returns_status = function()
15+
luatest.assert_equals(size, 4)
16+
luatest.assert_equals(first, 10)
17+
luatest.assert_equals(mp_array_new_decoded, {20, 30})
18+
luatest.assert_equals(fourth, 40)
19+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local msgpack = require('msgpack')
2+
3+
local mp_array = msgpack.object({ 10, 20, 30 })
4+
local mp_array_iterator = mp_array:iterator()
5+
6+
local size = mp_array_iterator:decode_array_header() -- returns 3
7+
local first = mp_array_iterator:decode() -- returns 10
8+
mp_array_iterator:skip() -- returns none, skips 20
9+
local mp_value_under_cursor = mp_array_iterator:take()
10+
local third = mp_value_under_cursor:decode() -- returns 30
11+
12+
local luatest = require('luatest')
13+
local test_group = luatest.group()
14+
test_group.test_returns_status = function()
15+
luatest.assert_equals(size, 3)
16+
luatest.assert_equals(first, 10)
17+
luatest.assert_equals(third, 30)
18+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local msgpack = require('msgpack')
2+
3+
local mp_array = msgpack.object({ 10, 20, 30, 40 })
4+
local mp_array_iterator = mp_array:iterator()
5+
6+
local size = mp_array_iterator:decode_array_header() -- returns 4
7+
local first = mp_array_iterator:decode() -- returns 10
8+
local second = mp_array_iterator:decode() -- returns 20
9+
mp_array_iterator:skip() -- returns none, skips 30
10+
local fourth = mp_array_iterator:decode() -- returns 40
11+
12+
local luatest = require('luatest')
13+
local test_group = luatest.group()
14+
test_group.test_returns_status = function()
15+
luatest.assert_equals(size, 4)
16+
luatest.assert_equals(first, 10)
17+
luatest.assert_equals(second, 20)
18+
luatest.assert_equals(fourth, 40)
19+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local msgpack = require('msgpack')
2+
3+
-- Create a MsgPack object from a Lua object of any type
4+
local mp_from_number = msgpack.object(123)
5+
local mp_from_string = msgpack.object('hello world')
6+
local mp_from_array = msgpack.object({ 10, 20, 30 })
7+
local mp_from_table = msgpack.object({ band_name = 'The Beatles', year = 1960 })
8+
local mp_from_tuple = msgpack.object(box.tuple.new(1, 'The Beatles', 1960))
9+
10+
-- Create a MsgPack object from a raw MsgPack string
11+
local raw_mp_string = msgpack.encode({ 10, 20, 30 })
12+
local mp_from_mp_string = msgpack.object_from_raw(raw_mp_string)
13+
14+
-- Create a MsgPack object from a raw MsgPack string using buffer
15+
local buffer = require('buffer')
16+
local ibuf = buffer.ibuf()
17+
msgpack.encode({ 10, 20, 30 }, ibuf)
18+
local mp_from_mp_string_pt = msgpack.object_from_raw(ibuf.buf, ibuf:size())
19+
20+
-- Decode MsgPack data
21+
local mp_number_decoded = mp_from_number:decode() -- Returns 123
22+
local mp_string_decoded = mp_from_string:decode() -- Returns 'hello world'
23+
24+
local luatest = require('luatest')
25+
local test_group = luatest.group()
26+
test_group.test_returns_status = function()
27+
luatest.assert_equals(mp_from_mp_string:decode(), { 10, 20, 30 })
28+
luatest.assert_equals(mp_from_mp_string_pt:decode(), { 10, 20, 30 })
29+
luatest.assert_equals(mp_number_decoded, 123)
30+
luatest.assert_equals(mp_string_decoded, 'hello world')
31+
end

0 commit comments

Comments
 (0)