-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add support for arbitrary deserializers #230
base: master
Are you sure you want to change the base?
Conversation
rele/subscription.py
Outdated
data = self._subscription._deserialize(message) | ||
except Exception as e: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm slightly concerned about catching arbitrary exceptions here. Feedback welcome
@andrewgy8 Any other thoughts? |
What do you think about changing the proposal to rely on an interface? This would provide some benefits like:
Then in the subscription's constructor would look like this:
And finally
|
I like the exception handling where we catch a DeserializationException internally and ask users to raise that exception within their deserializer. I don't really see the point of wrapping it in a class though. Seems slightly more complicated and I don't see what it adds. |
Setting aside the benefits of using interfaces when injecting dependencies that might have different implementations and must fulfil a contract, It is slightly more complicated. At least we are removing the responsibility of checking if the passed argument is a callable (the In terms of lines of code, both solutions are similar ;-) |
Id say both solutions are identical. In the end they are callables. You could imagine passing a function, class or lambda in there would be perfectly feasible. And they would all work. The other argument to keeping it a generic callable rather than a specific interface is the parity with the filter_by argument in the sub decorator. Which can also be any callable. https://mercadonarele.readthedocs.io/en/latest/guides/filters.html#filter-by-parameter |
Maybe I'm missing something, but are we? If someone passed in a string or an int as a deserializer, wouldn't they just get an exception when we try to call their deserialize method? >>> deserializer = 1
>>> deserializer.deserialize()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'deserialize' |
Good point. For consistency sake, I'm buying it. |
No, you are not. In fact, it's me who missed the parenthesis in the proposal. I meant to pass an instance, not a class name. |
Looks like master received a force-push last month. I rebased my branch so I think the changes should be good to go! |
🎩 What?
Allow custom deserializers other than the default json deserializer.
🤔 Why?
Some of GCP's pub/sub notifications have messages that are simple strings which can't be deserialized as json.
For example, notifications that a record was inserted into the dicom store comes through with a string indicating the path to that record and nothing more.
🔗 #229