Add default values to Message attributes

This ensures no attributes are None and simplifies some checks within the function
Otherwise, if, for instance, there were no 'data' field in a message, when deserializing, the caller would have to check for None
pull/2018/head
Matthew D. Scholefield 2019-02-26 02:44:19 -06:00
parent e51e35f622
commit 42ea785fd8
1 changed files with 7 additions and 6 deletions

View File

@ -36,10 +36,9 @@ class Message:
Message objects will be used to send information back and fourth
bettween processes of mycroft service, voice, skill and cli
"""
data = data or {}
self.type = type
self.data = data
self.context = context
self.data = data or {}
self.context = context or {}
def serialize(self):
"""This returns a string of the message info.
@ -73,7 +72,9 @@ class Message:
value(str): This is the string received from the websocket
"""
obj = json.loads(value)
return Message(obj.get('type'), obj.get('data'), obj.get('context'))
return Message(obj.get('type') or '',
obj.get('data') or {},
obj.get('context') or {})
def reply(self, type, data=None, context=None):
"""Construct a reply message for a given message
@ -98,7 +99,7 @@ class Message:
data = data or {}
context = context or {}
new_context = self.context if self.context else {}
new_context = self.context
for key in context:
new_context[key] = context[key]
if 'target' in data:
@ -138,7 +139,7 @@ class Message:
Message: Message object to publish
"""
context = context or {}
new_context = self.context.copy() if self.context else {}
new_context = self.context.copy()
for key in context:
new_context[key] = context[key]