-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathpacker.py
51 lines (40 loc) · 1.61 KB
/
packer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Tarantool `extension`_ types encoding support.
.. _extension: https://www.tarantool.io/en/doc/latest/dev_guide/internals/msgpack_extensions/
"""
from decimal import Decimal
from uuid import UUID
from msgpack import ExtType
from tarantool.types import BoxError
from tarantool.msgpack_ext.types.datetime import Datetime
from tarantool.msgpack_ext.types.interval import Interval
import tarantool.msgpack_ext.decimal as ext_decimal
import tarantool.msgpack_ext.uuid as ext_uuid
import tarantool.msgpack_ext.error as ext_error
import tarantool.msgpack_ext.datetime as ext_datetime
import tarantool.msgpack_ext.interval as ext_interval
encoders = [
{'type': Decimal, 'ext': ext_decimal },
{'type': UUID, 'ext': ext_uuid },
{'type': BoxError, 'ext': ext_error },
{'type': Datetime, 'ext': ext_datetime},
{'type': Interval, 'ext': ext_interval},
]
def default(obj, packer=None):
"""
:class:`msgpack.Packer` encoder.
:param obj: Object to encode.
:type obj: :class:`decimal.Decimal` or :class:`uuid.UUID` or
or :class:`tarantool.BoxError` or :class:`tarantool.Datetime`
or :class:`tarantool.Interval`
:param packer: msgpack packer to work with common types
(like dictionary in extended error payload)
:type packer: :class:`msgpack.Packer`, optional
:return: Encoded value.
:rtype: :class:`msgpack.ExtType`
:raise: :exc:`~TypeError`
"""
for encoder in encoders:
if isinstance(obj, encoder['type']):
return ExtType(encoder['ext'].EXT_ID, encoder['ext'].encode(obj, packer))
raise TypeError("Unknown type: %r" % (obj,))