From b6fa19d5907465f31fe7827857ab65bab40cc3ae Mon Sep 17 00:00:00 2001 From: hamsh Date: Tue, 17 Sep 2024 13:05:37 +0000 Subject: [PATCH 1/5] Update protocol.py add rpushb to push bytes data --- asyncio_redis/protocol.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/asyncio_redis/protocol.py b/asyncio_redis/protocol.py index db1d62e..8db922f 100644 --- a/asyncio_redis/protocol.py +++ b/asyncio_redis/protocol.py @@ -1646,6 +1646,17 @@ def rpush(self, tr, key: NativeType, values: ListOf(NativeType)) -> int: *map(self.encode_from_native, values), ) + @_query_command + def rpushb(self, tr, key: NativeType, values: bytes) -> int: + """Append one or multiple values to a list + """ + return self._query( + tr, + b"rpush", + self.encode_from_native(key), + values, + ) + @_query_command def rpushx(self, tr, key: NativeType, value: NativeType) -> int: """Append a value to a list, only if the list exists From 8136d0ffb168187100f624c2af2dcb5c24be9a05 Mon Sep 17 00:00:00 2001 From: hamsh Date: Wed, 18 Sep 2024 07:20:44 +0000 Subject: [PATCH 2/5] Update protocol.py Add `xadd` function --- asyncio_redis/protocol.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/asyncio_redis/protocol.py b/asyncio_redis/protocol.py index 8db922f..885d7ef 100644 --- a/asyncio_redis/protocol.py +++ b/asyncio_redis/protocol.py @@ -1500,6 +1500,26 @@ def sadd(self, tr, key: NativeType, members: ListOf(NativeType)) -> int: *map(self.encode_from_native, members), ) + @_query_command + def xadd( + self, tr, stream: NativeType, id: NativeType, fields: ListOf(NativeType) + ) -> NativeType: + """ + Appends a new entry to a stream. + + :param stream: The name of the stream. + :param id: The ID of the entry. Use '*' to auto-generate an ID. + :param fields: A list of field-value pairs. + :return: The ID of the added entry. + """ + return self._query( + tr, + b"xadd", + self.encode_from_native(stream), + self.encode_from_native(id), + *map(self.encode_from_native, fields), + ) + @_query_command def srem(self, tr, key: NativeType, members: ListOf(NativeType)) -> int: """Remove one or more members from a set From be840499e98519d4a0f4a53fcd41e7168e7584c5 Mon Sep 17 00:00:00 2001 From: hamsh Date: Thu, 19 Sep 2024 10:15:10 +0000 Subject: [PATCH 3/5] Update protocol.py --- asyncio_redis/protocol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncio_redis/protocol.py b/asyncio_redis/protocol.py index 885d7ef..0da20fe 100644 --- a/asyncio_redis/protocol.py +++ b/asyncio_redis/protocol.py @@ -1502,7 +1502,7 @@ def sadd(self, tr, key: NativeType, members: ListOf(NativeType)) -> int: @_query_command def xadd( - self, tr, stream: NativeType, id: NativeType, fields: ListOf(NativeType) + self, tr, stream: NativeType, id: NativeType, fields: dict, ) -> NativeType: """ Appends a new entry to a stream. From d3f462e8455901baf02a9ea90ba77ee082f76807 Mon Sep 17 00:00:00 2001 From: hamsh Date: Thu, 19 Sep 2024 11:31:20 +0000 Subject: [PATCH 4/5] Update protocol.py --- asyncio_redis/protocol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asyncio_redis/protocol.py b/asyncio_redis/protocol.py index 0da20fe..ba2fcfe 100644 --- a/asyncio_redis/protocol.py +++ b/asyncio_redis/protocol.py @@ -1502,7 +1502,7 @@ def sadd(self, tr, key: NativeType, members: ListOf(NativeType)) -> int: @_query_command def xadd( - self, tr, stream: NativeType, id: NativeType, fields: dict, + self, tr, stream: NativeType, fields: dict, id: NativeType='*', ) -> NativeType: """ Appends a new entry to a stream. From cb00a6abb85e19c21e967b29582299dc12403978 Mon Sep 17 00:00:00 2001 From: hamedsh Date: Fri, 20 Sep 2024 11:02:46 +0000 Subject: [PATCH 5/5] Update encoders and streamline xadd handling Refactor StringEncoder to handle different data types more safely. Flatten the fields dictionary in the xadd method to ensure correct query formation and avoid potential issues with nested data structures. --- asyncio_redis/encoders.py | 8 ++++++-- asyncio_redis/protocol.py | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/asyncio_redis/encoders.py b/asyncio_redis/encoders.py index 68a493e..52d93b6 100644 --- a/asyncio_redis/encoders.py +++ b/asyncio_redis/encoders.py @@ -62,11 +62,15 @@ class StringEncoder(BaseEncoder): def encode_from_native(self, data): """ string to bytes """ - return data.encode(self.encoding) + if isinstance(data, str): + return data.encode(self.encoding) + return data def decode_to_native(self, data): """ bytes to string """ - return data.decode(self.encoding) + if isinstance(data, bytes): + return data.decode(self.encoding) + return data class UTF8Encoder(StringEncoder): diff --git a/asyncio_redis/protocol.py b/asyncio_redis/protocol.py index ba2fcfe..4a02e15 100644 --- a/asyncio_redis/protocol.py +++ b/asyncio_redis/protocol.py @@ -1512,12 +1512,13 @@ def xadd( :param fields: A list of field-value pairs. :return: The ID of the added entry. """ + flattened_list = [item for sublist in fields.items() for item in sublist] return self._query( tr, b"xadd", self.encode_from_native(stream), self.encode_from_native(id), - *map(self.encode_from_native, fields), + *map(self.encode_from_native, flattened_list), ) @_query_command