Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

csaroff
Copy link

@csaroff csaroff commented Sep 30, 2022

🎩 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

Comment on lines 125 to 126
data = self._subscription._deserialize(message)
except Exception as e:
Copy link
Author

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

rele/subscription.py Outdated Show resolved Hide resolved
@csaroff csaroff requested a review from andrewgy8 October 3, 2022 03:21
@csaroff
Copy link
Author

csaroff commented Oct 12, 2022

@andrewgy8 Any other thoughts?

@jonasae
Copy link
Contributor

jonasae commented Oct 13, 2022

What do you think about changing the proposal to rely on an interface?

This would provide some benefits like:

  • Anyone could provide a particular deserializer implementation
  • deserializer.deserialize() could rise a DeserializingException and then avoid catching a generic exception
  • Avoid passing a lambda as a default argument
  • As we will be calling a method, we don't need to check if its a callable anymore
class DeserializingException(Exception)

class JsonDeserializer:
  def deserialize(message): 
    try:
      return json.loads(x.data.decode("utf-8"))
    except json.JSONDecodeError as e:
      raise DeserializingException(e) 

Then in the subscription's constructor would look like this:

def __init__(
        self,
        func,
        topic,
        prefix="",
        suffix="",
        filter_by=None,
        backend_filter_by=None,
        deserializer=JsonDeserializer,
    ):
        ...
        self._deserializer = deserializer
        ...

And finally Callback.__call__:

def __call__(self, message):
       ...
        try:
            data = self._subscription._deserializer.deserialize(message)
        except DeseralizingException as e:
          ...

@csaroff
Copy link
Author

csaroff commented Oct 13, 2022

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.

@jonasae
Copy link
Contributor

jonasae commented Oct 14, 2022

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 _init_deserializer method won't be needed).

In terms of lines of code, both solutions are similar ;-)

@andrewgy8
Copy link
Contributor

andrewgy8 commented Oct 14, 2022

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

@csaroff
Copy link
Author

csaroff commented Oct 14, 2022

At least we are removing the responsibility of checking if the passed argument is a callable (the _init_deserializer method won't be needed).

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'

@jonasae
Copy link
Contributor

jonasae commented Oct 17, 2022

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

Good point. For consistency sake, I'm buying it.
But please, don't pass a lambda directly in the definition as a default callable.

@jonasae
Copy link
Contributor

jonasae commented Oct 17, 2022

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?

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.

@csaroff
Copy link
Author

csaroff commented Apr 19, 2023

Looks like master received a force-push last month. I rebased my branch so I think the changes should be good to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants