Skip to content

fixes issues #44 #45 setDecoder not working, TypeError when reJSON returns None #46

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 7 commits into from
Aug 3, 2021

Conversation

bentsku
Copy link
Contributor

@bentsku bentsku commented Apr 14, 2021

One possibility to fix both #44 and #45. I am not 100% certain on the ramifications, though I doubt it would create any problem in a normal usage of rejson-py. I've tested locally with the json module, and ujson.

@bentsku bentsku changed the title fixes #44 #45 setDecoder not working, TypeError when reJSON returns None fixes issues #44 #45 setDecoder not working, TypeError when reJSON returns None Apr 14, 2021
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@gkorland gkorland requested review from rafie and itamarhaber April 16, 2021 19:38
rafie
rafie previously approved these changes Apr 17, 2021
@gkorland gkorland linked an issue Apr 22, 2021 that may be closed by this pull request
@rafie rafie requested a review from oshadmi May 30, 2021 07:42
rejson/client.py Outdated
Comment on lines 93 to 99
def _decode(self, s, *args, **kwargs):
try:
return self._decoder.decode(s, *args, **kwargs)
except TypeError:
if s is not None:
raise
return None
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _decode(self, s, *args, **kwargs):
try:
return self._decoder.decode(s, *args, **kwargs)
except TypeError:
if s is not None:
raise
return None
def _decode(self, s, *args, **kwargs):
try:
return self._decoder.decode(s, *args, **kwargs) if s is not None else None
except TypeError:
raise

Copy link
Contributor

Choose a reason for hiding this comment

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

If None always raises a TypeError in decode, can we skip the attempt to decode it?

Copy link
Contributor Author

@bentsku bentsku May 31, 2021

Choose a reason for hiding this comment

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

I guess we can, I did this to not change the behaviour of the decoder, in the case the user uses a custom decoder and wanted to return a custom value. With this, we override the implementation of the decoder.
But in the general case, this is much better, as we don't even need the try...except clause. If that is okay to override the decoder implementation, then I prefer it too.

Copy link
Contributor

Choose a reason for hiding this comment

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

The default decoder would skip attempting to decode None.
If, for some reason, users would like to handle None in the decoder, they can use their own decoder with setDecoder, so all options are still available.

Copy link
Contributor Author

@bentsku bentsku May 31, 2021

Choose a reason for hiding this comment

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

If they use their own decoder, it would still call the function _decode, and if the query result was None, would not attempt to use it, and return None before. This function is called before the call to the default or custom decoder decode function. But it only prevents users to handle None, which can be fine, they can handle it later on.

Copy link
Contributor

@oshadmi oshadmi May 31, 2021

Choose a reason for hiding this comment

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

Right, what I should have written is they could still use set_response_callback, e.g.,
set_response_callback('json.get', self.myDecoderMethod)
(but that should be done per command)
And then they can still do their own decoding, if they really insist,
But it could also simply be handled later on as you suggested (after the default decoding)

Since this PR is also fixing setDecoder, do you have a test case also for it?

Copy link
Contributor Author

@bentsku bentsku Jun 1, 2021

Choose a reason for hiding this comment

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

Perfect, yes if there are very specific user needs, it can always be done.
I believe I often code with the EAFP python way, but the performance in case of a None value of your solution are 10x better, and about the same if the value isn't None.
I just pushed the test for setDecoder and also one more operation on the pipeline test (issue #44). Thanks again for the review!

Copy link
Contributor

Choose a reason for hiding this comment

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

@bentsku LGTM. Thank you for your contribution and for a fruitful discussion!

@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 1, 2021

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Contributor

@oshadmi oshadmi left a comment

Choose a reason for hiding this comment

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

Thanks 👍🏼

@sonarqubecloud
Copy link

sonarqubecloud bot commented Aug 3, 2021

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

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.

Method setDecoder is not working
5 participants