-
Notifications
You must be signed in to change notification settings - Fork 43
New msgpack api (2.11 and 2.10) #3605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
3194898
to
5ee66d1
Compare
a16772e
to
313e50d
Compare
313e50d
to
df2858c
Compare
@@ -241,7 +236,7 @@ Below is a list of all ``msgpack`` functions and members. | |||
|
|||
.. function:: decode_array_header(byte-array, size) | |||
|
|||
Call the mp_decode_array function in the `MsgPuck <http://rtsisyk.github.io/msgpuck/>`_ library | |||
Call the `MsgPuck <https://rtsisyk.github.io/msgpuck/>`_'s ``mp_decode_array`` function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth mentioning here that this function expects the data to be a MsgPack array. If it isn't, an exception is raised. Same comment for decode_map_header
.
@@ -528,7 +538,7 @@ userdata/cdata, you can use this code: | |||
.. NOTE:: | |||
|
|||
To achieve the same effect for only one call to ``msgpack.encode()`` | |||
(i.e. without changing the configuration permanently), you can use | |||
(that is without changing the configuration permanently), you can use | |||
``msgpack.encode({1, x, y, 2}, {encode_invalid_numbers = true})``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't true:
tarantool> msgpack.encode({1, 2}, {encode_invalid_numbers = true})
---
- error: 'msgpack.encode: argument 2 must be of type ''struct ibuf'''
...
It's possible to do this though:
msgpack.new({encode_invalid_numbers = true}).encode({1, 2})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Also found out the the example above with httpc.curl
doesn't work:
tarantool> msgpack.encode(httpc.curl)
---
- error: unsupported Lua type 'userdata'
...
tarantool> msgpack.encode(httpc.curl, {encode_use_tostring=true})
---
- error: 'msgpack.encode: argument 2 must be of type ''struct ibuf'''
...
Changed to msgpack.cfg{encode_use_tostring = true}
.
* if a MsgPack object is an associative array, ``key`` is the string value that specifies the element key. | ||
|
||
:return: an element of the MsgPack array. | ||
If the specified key is missing in the array, `get` returns ``nil``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be worth mentioning that this function raises an error in case the value stored in the object isn't a MsgPack array or map.
local first = mp_map_iterator:decode() -- returns 'foo' | ||
local second = mp_map_iterator:decode() -- returns 'a' | ||
mp_map_iterator:skip() -- returns none, skips 'bar' | ||
local fourth = mp_map_iterator:decode() -- returns ['b1', 'b2', 'b3'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, map elements will not necessarily be returned in this order. A Lua map is unordered so the order in which it's encoded in MsgPack depends on Lua internals. It may be better to rework the example to put decoded elements back into a map/set.
.. code-block:: lua | ||
.. literalinclude:: /code_snippets/test/msgpack/msgpack_object_test.lua | ||
:language: lua | ||
:lines: 1,29-32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like you forgot line 5: line 31 refers to mp_from_string
defined at line 5.
It's difficult to review these examples. IMO better inline them in the text.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed. We decided to use testable examples to make sure we don't miss any breaking changes that might break the code snippets used in docs. Looks like more 'atomic' examples that show only the minimal API usage would be more convenient for review - msgpack_object_test.lua
actually mixes examples that can be split up.
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_array_new = mp_array_iterator:take() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it called mp_array_new
? It's a MsgPack object that contains 30, i.e. it doesn't have anything to with MsgPack arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mixed up with take_array
, thanks for pointing out
fb14e6f
to
0519abf
Compare
7918deb
to
4f48bae
Compare
3f1c765
to
755768e
Compare
755768e
to
a0b61f6
Compare
|
||
* - :ref:`msgpack.decode_map_header(byte-array, size) <msgpack-decode_map_header>` | ||
- Skip a map header in a raw MsgPack string | ||
- Call the `MsgPuck <https://rtsisyk.github.io/msgpuck/>`_'s ``mp_decode_map`` function and return the map size and a pointer to the first map component. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Call the `MsgPuck <https://rtsisyk.github.io/msgpuck/>`_'s ``mp_decode_map`` function and return the map size and a pointer to the first map component. | |
- Call the `MsgPuck <https://rtsisyk.github.io/msgpuck/>`_'s ``mp_decode_map`` function and return the map size and a pointer to the first map component |
@@ -301,8 +305,8 @@ The MsgPack output structure can be specified with the ``__serialize`` parameter | |||
|
|||
* 'seq', 'sequence', 'array' - table encoded as an array | |||
* 'map', 'mappping' - table encoded as a map | |||
* function - the meta-method called to unpack serializable representation | |||
of table, cdata or userdata objects | |||
* function - the meta-method called to unpack the serializable representation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* function - the meta-method called to unpack the serializable representation | |
* function -- the meta-method called to unpack the serializable representation |
Same for lines 306-307
excessively sparse arrays will be handled as maps. | ||
|
||
**msgpack.cfg() example 1:** | ||
|
||
If ``msgpack.cfg.encode_invalid_numbers = true`` (the default), | ||
then NaN and Inf are legal values. If that is not desirable, then | ||
ensure that ``msgpack.encode()`` will not accept them, by saying | ||
ensure that ``msgpack.encode()`` do not accept them, by saying |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ensure that ``msgpack.encode()`` do not accept them, by saying | |
ensure that ``msgpack.encode()`` does not accept them, by saying |
|
||
The index key used to get the array element might be one of the following: | ||
|
||
* if a MsgPack object is an array, ``key`` is the integer value (start with 1) that specifies the element index. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* if a MsgPack object is an array, ``key`` is the integer value (start with 1) that specifies the element index. | |
* if a MsgPack object is an array, the ``key`` is an integer value (starting with 1) that specifies the element index. |
|
||
:param number/string key: the index key used to get the array element, which might be one of the following: | ||
|
||
* if a MsgPack object is an array, ``key`` is the integer value (start with 1) that specifies the element index. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* if a MsgPack object is an array, ``key`` is the integer value (start with 1) that specifies the element index. | |
* if a MsgPack object is an array, the ``key`` is an integer value (starting with 1) that specifies the element index. |
a0b61f6
to
a43fb40
Compare
Documented the following API members:
msgpack_object[key]
and the correspondingmsgpack_object:get(key)
function:iterator_object:take_array(count)
function: https://docs.d.tarantool.io/en/doc/2.11-msgpack-api/reference/reference_lua/msgpack/#lua-function.iterator_object.take_arraycfg.encode_error_as_ext
configuration property: https://docs.d.tarantool.io/en/doc/2.11-msgpack-api/reference/reference_lua/msgpack/#msgpack-cfgNote that updated docs reference testable code snippets for making sure the code shown in docs is correct.