Skip to content

fix(fcm): String representation in Message class #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2019
Merged

Conversation

lahirumaramba
Copy link
Member

@lahirumaramba lahirumaramba commented Sep 23, 2019

Discussion

  • Add string representation in Message class
  • Use MessageEncoder to convert Message objects to valid JSON strings

Testing

  • None

API Changes

  • None

Resolves: #349

RELEASE NOTE: Message class now implements the __str__() contract which can be used to obtain a string representation of a Message instance.

@lahirumaramba
Copy link
Member Author

Sample output:

>>> from firebase_admin import messaging
>>> message = messaging.Message(
        data={
            'score': '850',
            'time': '2:45',
        },
        token='token',
    )
>>> print(message)
{"token": "token", "data": {"score": "850", "time": "2:45"}}
>>> str(message)
'{"token": "token", "data": {"score": "850", "time": "2:45"}}'
>>>

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One suggestion added.

This also brings up the question whether we should support __str__ on all public classes in this module (e.g. AndroidConfig, Notification etc). But Message is probably enough to begin with. After all, other objects are never used by themselves -- they are always part of a Message.

Also please add a test case.

@@ -57,6 +57,9 @@ def __init__(self, data=None, notification=None, android=None, webpush=None, apn
self.topic = topic
self.condition = condition

def __str__(self):
return json.dumps(MessageEncoder().default(self))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this work?

json.dumps(self, cls=MessageEncoder)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thank you.
Will update with test cases soon.

@hiranya911 hiranya911 changed the title String representation in Message class fix(fcm): String representation in Message class Sep 23, 2019
@lahirumaramba
Copy link
Member Author

lahirumaramba commented Sep 24, 2019

@hiranya911 :

One suggestion added.

This also brings up the question whether we should support __str__ on all public classes in this module (e.g. AndroidConfig, Notification etc). But Message is probably enough to begin with. After all, other objects are never used by themselves -- they are always part of a Message.

Also please add a test case.

  • Good point! I also thought about adding __str__ support to all other classes but like you said they will always be part of a Message. I think we can start with Message for now.

  • PR is updated with test cases.

  • One other thing to note is that since we use MessageEncoder in the implementation, __str__ inherits the validation checks from MessageEncoder... I do not think this is a problem but wanted to mention as this could be different from the expected behavior of a str() function.

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :+1

@lahirumaramba lahirumaramba merged commit 3cab0c1 into master Sep 25, 2019
@lahirumaramba lahirumaramba deleted the lm-msg-str branch September 25, 2019 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: string representation
2 participants