diff --git a/kafka/protocol/types.py b/kafka/protocol/types.py index 812c5e74d..7889e06d5 100644 --- a/kafka/protocol/types.py +++ b/kafka/protocol/types.py @@ -1,5 +1,6 @@ import struct from struct import error +import uuid from kafka.protocol.abstract import AbstractType @@ -88,6 +89,20 @@ def decode(cls, data): return _unpack(cls._unpack, data.read(8)) +class UUID(AbstractType): + ZERO_UUID = uuid.UUID(int=0) + + @classmethod + def encode(cls, value): + if isinstance(value, uuid.UUID): + return value.bytes + return uuid.UUID(value).bytes + + @classmethod + def decode(cls, data): + return uuid.UUID(bytes=data.read(16)) + + class String(AbstractType): def __init__(self, encoding='utf-8'): self.encoding = encoding @@ -346,7 +361,6 @@ def encode(cls, value): class CompactArray(Array): - def encode(self, items): if items is None: return UnsignedVarInt32.encode(0)