Repository: xconnio/wampproto-python (language: python)

Issue:
CBOR serializer
Need cbor serializer for serializing and deserializing wamp messages. Use https://pypi.org/project/cbor2/


Discussion / hints:

Fix patch:
diff --git a/requirements-dev.txt b/requirements-dev.txt
index 2e177ff..6d455de 100644
--- a/requirements-dev.txt
+++ b/requirements-dev.txt
@@ -1,3 +1,3 @@
-ruff
+-r requirements.txt
 pytest
 coverage
diff --git a/requirements-pinned.txt b/requirements-pinned.txt
index ee20484..8ac9c9e 100644
--- a/requirements-pinned.txt
+++ b/requirements-pinned.txt
@@ -1,3 +1,4 @@
+cbor2==5.6.2
 coverage==7.4.3
 exceptiongroup==1.2.0
 iniconfig==2.0.0
diff --git a/requirements.txt b/requirements.txt
index af3ee57..f34c61b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,1 +1,2 @@
 ruff
+cbor2
diff --git a/wamp/serializers/cbor.py b/wamp/serializers/cbor.py
new file mode 100644
index 0000000..07beb45
--- /dev/null
+++ b/wamp/serializers/cbor.py
@@ -0,0 +1,14 @@
+import cbor2
+
+from wamp.messages.message import Message
+from wamp.serializers.serializer import to_message
+from wamp.serializers.serializer import Serializer
+
+
+class CBORSerializer(Serializer):
+    def serialize(self, message: Message) -> bytes:
+        return cbor2.dumps(message.marshal())
+
+    def deserialize(self, data: bytes) -> Message:
+        wamp_message = cbor2.loads(data)
+        return to_message(wamp_message)


Test patch:
diff --git a/tests/serializers/cbor.py b/tests/serializers/cbor.py
new file mode 100644
index 0000000..ff2b02a
--- /dev/null
+++ b/tests/serializers/cbor.py
@@ -0,0 +1,15 @@
+from wamp.messages.hello import Hello
+from wamp.serializers.cbor import CBORSerializer
+
+
+def test_serializer():
+    serializer = CBORSerializer()
+    hello = Hello.parse([1, "realm1", {"roles": {"caller": {}}}])
+
+    data = serializer.serialize(hello)
+    assert isinstance(data, bytes)
+
+    obj = serializer.deserialize(data)
+    assert isinstance(obj, Hello)
+    assert obj.realm == hello.realm
+    assert obj.roles == hello.roles