Skip to content

Commit

Permalink
Merge pull request #14 from compas-dev/fix-more-stuff
Browse files Browse the repository at this point in the history
Fix stuff
  • Loading branch information
gonzalocasas authored May 1, 2024
2 parents 66fdf63 + 83ca4d9 commit 8ceb89c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

* Fix/add json serialization support to `Message` class.
* Fix get/set attr/item recursion bug.

### Removed


Expand Down
9 changes: 9 additions & 0 deletions src/compas_eve/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,21 @@ def __str__(self):
def __getattr__(self, name):
return self.data[name]

def __setattr__(self, key, value):
if key == "data" or key in self.__dict__:
super(Message, self).__setattr__(key, value)
else:
self.data[key] = value

def __getitem__(self, key):
return self.data[key]

def __setitem__(self, key, value):
self.data[key] = value

def __jsondump__(self, minimal=False):
return self.data

@classmethod
def parse(cls, value):
instance = cls(**value)
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def test_compas_data_as_message():

class Header(Data):
def __init__(self, sequence_id=None):
super(Header, self).__init__()
self.sequence_id = sequence_id

@property
Expand All @@ -118,6 +119,7 @@ def __data__(self):

class DataTestMessage(Data):
def __init__(self, name=None, location=None, header=None):
super(DataTestMessage, self).__init__()
self.name = name
self.location = location
self.header = header or Header(1)
Expand Down Expand Up @@ -157,6 +159,47 @@ def callback(msg):
assert result["value"].header.sequence_id == 1


def test_nested_message_types():

class Header(Message):
def __init__(self, sequence_id=None):
super(Header, self).__init__()
self["sequence_id"] = sequence_id

class DataTestMessage(Message):
def __init__(self, name=None, location=None, header=None):
super(DataTestMessage, self).__init__()
self["name"] = name
self["location"] = location
self["header"] = header or Header(1)

@classmethod
def parse(cls, value):
return cls(
name=value["name"],
location=value["location"],
header=Header(**value["header"]),
)

result = dict(value=None, event=Event())

def callback(msg):
result["value"] = msg
result["event"].set()

tx = MqttTransport(HOST)
topic = Topic("/messages_compas_eve_test/test_nested_message_types/", DataTestMessage)

Subscriber(topic, callback, transport=tx).subscribe()
Publisher(topic, transport=tx).publish(DataTestMessage(name="Jazz", location=1.334))

received = result["event"].wait(timeout=3)
assert received, "Message not received"
assert result["value"].name == "Jazz"
assert result["value"].location == 1.334
assert result["value"].header.sequence_id == 1


def test_dict_as_message():
result = dict(value=None, event=Event())

Expand Down

0 comments on commit 8ceb89c

Please sign in to comment.