RedisJSON/redisjson-py

Method setDecoder is not working

bentsku opened this issue · 0 comments

Hello,

The method setDecoder is not working properly, if you use it after initialising your Client instance, it won't be used in the callbacks.

Passing the decoder as a parameter when initialising the Client instance works as expected.

I believe it is because we set the callbacks in the __init__ method, and when we set the new decoder, we reassign self._decode with the new decoder decode method, but the callbacks are still pointing to the old function.

Those callbacks are not recreated with the set_response_callback method.

# Set the module commands' callbacks
MODULE_CALLBACKS = {
'JSON.DEL': long,
'JSON.GET': self._decode,
'JSON.MGET': bulk_of_jsons(self._decode),
'JSON.SET': lambda r: r and nativestr(r) == 'OK',
'JSON.NUMINCRBY': self._decode,
'JSON.NUMMULTBY': self._decode,
'JSON.STRAPPEND': long,
'JSON.STRLEN': long,
'JSON.ARRAPPEND': long,
'JSON.ARRINDEX': long,
'JSON.ARRINSERT': long,
'JSON.ARRLEN': long,
'JSON.ARRPOP': self._decode,
'JSON.ARRTRIM': long,
'JSON.OBJLEN': long,
}
for k, v in six.iteritems(MODULE_CALLBACKS):
self.set_response_callback(k, v)

I guess several fixes are possible, having _decode being a declared method of the class, which would be calling the _decode_function property (I don't like the name, what should it be ?) that would be set in the setDecoder method. The callbacks would always be pointing to the right method.

A quick fix that would also be fixing the TypeError for every command would be in the form of this snippet.

class Client(StrictRedis):
    _encoder = None
    _encode = None
    _decoder = None
    _decode_function = None

   [...]

    def _decode(self, s, *args, **kwargs):
        try:
            return self._decode_function(s, *args, **kwargs)
        except TypeError:
            if s is not None:
               raise
            return None

This would fix the callback problem (tested). I can open a pull request if you'd like this fix.

Thank you