Skip to content

Commit

Permalink
New msgpack api (#3605)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyaksenov authored Aug 8, 2023
1 parent 2535467 commit d21a9a7
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 154 deletions.
20 changes: 20 additions & 0 deletions doc/code_snippets/test/msgpack/msgpack_object_index_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local msgpack = require('msgpack')

local mp_from_array = msgpack.object({ 10, 20, 30 })
local mp_from_table = msgpack.object({ band_name = 'The Beatles', year = 1960 })
local mp_from_tuple = msgpack.object(box.tuple.new(1, 'The Beatles', 1960))

-- Get MsgPack data by the specified index or key
local mp_array_get_by_index = mp_from_array[1] -- Returns 10
local mp_table_get_by_key = mp_from_table['band_name'] -- Returns 'The Beatles'
local mp_table_get_by_nonexistent_key = mp_from_table['rating'] -- Returns nil
local mp_tuple_get_by_index = mp_from_tuple[3] -- Returns 1960

local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_index = function()
luatest.assert_equals(mp_array_get_by_index, 10)
luatest.assert_equals(mp_table_get_by_key, 'The Beatles')
luatest.assert_equals(mp_table_get_by_nonexistent_key, nil)
luatest.assert_equals(mp_tuple_get_by_index, 1960)
end
14 changes: 14 additions & 0 deletions doc/code_snippets/test/msgpack/msgpack_object_is_object_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local msgpack = require('msgpack')

local mp_from_string = msgpack.object('hello world')

-- Check if the given argument is a MsgPack object
local mp_is_object = msgpack.is_object(mp_from_string) -- Returns true
local string_is_object = msgpack.is_object('hello world') -- Returns false

local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_is_object = function()
luatest.assert_equals(mp_is_object, true)
luatest.assert_equals(string_is_object, false)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
local msgpack = require('msgpack')

local mp_map = msgpack.object({ foo = 123 })
local mp_map_iterator = mp_map:iterator()

local size = mp_map_iterator:decode_map_header() -- returns 1
local first = mp_map_iterator:decode() -- returns 'foo'
local second = mp_map_iterator:decode() -- returns '123'

local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_iterator_decode = function()
luatest.assert_equals(size, 1)
luatest.assert_equals(first, 'foo')
luatest.assert_equals(second, 123)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local msgpack = require('msgpack')

local mp_array = msgpack.object({ 10, 20, 30, 40 })
local mp_array_iterator = mp_array:iterator()

local size = mp_array_iterator:decode_array_header() -- returns 4
local first = mp_array_iterator:decode() -- returns 10
local mp_array_new = mp_array_iterator:take_array(2)
local mp_array_new_decoded = mp_array_new:decode() -- returns {20, 30}
local fourth = mp_array_iterator:decode() -- returns 40

local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_iterator_take_array = function()
luatest.assert_equals(size, 4)
luatest.assert_equals(first, 10)
luatest.assert_equals(mp_array_new_decoded, {20, 30})
luatest.assert_equals(fourth, 40)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local msgpack = require('msgpack')

local mp_array = msgpack.object({ 10, 20, 30 })
local mp_array_iterator = mp_array:iterator()

local size = mp_array_iterator:decode_array_header() -- returns 3
local first = mp_array_iterator:decode() -- returns 10
mp_array_iterator:skip() -- returns none, skips 20
local mp_value_under_cursor = mp_array_iterator:take()
local third = mp_value_under_cursor:decode() -- returns 30

local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_iterator_take = function()
luatest.assert_equals(size, 3)
luatest.assert_equals(first, 10)
luatest.assert_equals(third, 30)
end
19 changes: 19 additions & 0 deletions doc/code_snippets/test/msgpack/msgpack_object_iterator_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local msgpack = require('msgpack')

local mp_array = msgpack.object({ 10, 20, 30, 40 })
local mp_array_iterator = mp_array:iterator()

local size = mp_array_iterator:decode_array_header() -- returns 4
local first = mp_array_iterator:decode() -- returns 10
local second = mp_array_iterator:decode() -- returns 20
mp_array_iterator:skip() -- returns none, skips 30
local fourth = mp_array_iterator:decode() -- returns 40

local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_iterator_decode = function()
luatest.assert_equals(size, 4)
luatest.assert_equals(first, 10)
luatest.assert_equals(second, 20)
luatest.assert_equals(fourth, 40)
end
31 changes: 31 additions & 0 deletions doc/code_snippets/test/msgpack/msgpack_object_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local msgpack = require('msgpack')

-- Create a MsgPack object from a Lua object of any type
local mp_from_number = msgpack.object(123)
local mp_from_string = msgpack.object('hello world')
local mp_from_array = msgpack.object({ 10, 20, 30 })
local mp_from_table = msgpack.object({ band_name = 'The Beatles', year = 1960 })
local mp_from_tuple = msgpack.object(box.tuple.new(1, 'The Beatles', 1960))

-- Create a MsgPack object from a raw MsgPack string
local raw_mp_string = msgpack.encode({ 10, 20, 30 })
local mp_from_mp_string = msgpack.object_from_raw(raw_mp_string)

-- Create a MsgPack object from a raw MsgPack string using buffer
local buffer = require('buffer')
local ibuf = buffer.ibuf()
msgpack.encode({ 10, 20, 30 }, ibuf)
local mp_from_mp_string_pt = msgpack.object_from_raw(ibuf.buf, ibuf:size())

-- Decode MsgPack data
local mp_number_decoded = mp_from_number:decode() -- Returns 123
local mp_string_decoded = mp_from_string:decode() -- Returns 'hello world'

local luatest = require('luatest')
local test_group = luatest.group()
test_group.test_msgpack_decode = function()
luatest.assert_equals(mp_from_mp_string:decode(), { 10, 20, 30 })
luatest.assert_equals(mp_from_mp_string_pt:decode(), { 10, 20, 30 })
luatest.assert_equals(mp_number_decoded, 123)
luatest.assert_equals(mp_string_decoded, 'hello world')
end
Loading

0 comments on commit d21a9a7

Please sign in to comment.