-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tarantool supports UUID type since version 2.4.1 [1]. This patch introduced the support of Tarantool UUID type in msgpack decoders and encoders. The Tarantool UUID type is mapped to the native Python uuid.UUID type. 1. tarantool/tarantool#4268 Closed #202
- Loading branch information
1 parent
b0ed8f0
commit c70dfa6
Showing
8 changed files
with
230 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
from decimal import Decimal | ||
from uuid import UUID | ||
from msgpack import ExtType | ||
|
||
import tarantool.msgpack_ext.decimal as ext_decimal | ||
import tarantool.msgpack_ext.uuid as ext_uuid | ||
|
||
encoders = [ | ||
{'type': Decimal, 'ext': ext_decimal}, | ||
{'type': UUID, 'ext': ext_uuid }, | ||
] | ||
|
||
def default(obj): | ||
if isinstance(obj, Decimal): | ||
return ExtType(ext_decimal.EXT_ID, ext_decimal.encode(obj)) | ||
for encoder in encoders: | ||
if isinstance(obj, encoder['type']): | ||
return ExtType(encoder['ext'].EXT_ID, encoder['ext'].encode(obj)) | ||
raise TypeError("Unknown type: %r" % (obj,)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import tarantool.msgpack_ext.decimal as ext_decimal | ||
import tarantool.msgpack_ext.uuid as ext_uuid | ||
|
||
decoders = { | ||
ext_decimal.EXT_ID: ext_decimal.decode, | ||
ext_uuid.EXT_ID : ext_uuid.decode , | ||
} | ||
|
||
def ext_hook(code, data): | ||
if code == ext_decimal.EXT_ID: | ||
return ext_decimal.decode(data) | ||
if code in decoders: | ||
return decoders[code](data) | ||
raise NotImplementedError("Unknown msgpack type: %d" % (code,)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from uuid import UUID | ||
|
||
# https://www.tarantool.io/en/doc/latest/dev_guide/internals/msgpack_extensions/#the-uuid-type | ||
# | ||
# The UUID MessagePack representation looks like this: | ||
# +--------+------------+-----------------+ | ||
# | MP_EXT | MP_UUID | UuidValue | | ||
# | = d8 | = 2 | = 16-byte value | | ||
# +--------+------------+-----------------+ | ||
|
||
EXT_ID = 2 | ||
|
||
def encode(obj): | ||
return obj.bytes | ||
|
||
def decode(data): | ||
return UUID(bytes=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.