Parsing the response into the right format with eval
Hi! We use eval in one of our scripts. The script returns an Multibulk which should (at least we expected!) be translated into a JS object, rather than an array. For some reason eval yields an array. We assume that's because node_redis has no way to know exactly what to expect from an eval. Is there a way to tell it that it's going to be a hash?
http://redis.io/commands/eval
See "Conversion between Lua and Redis data types".
Wel, yes, but what I'm interested in is conversion between Redis and Javascript types :) When hgetall returns a Multi bulk response, node_redis converts it to a hash, when eval returns a Multi bulk response, node_redis converts it to an array.
@julien51 Could you provide an example script?
Yes;
var redis = require("redis"),
client = redis.createClient();
client.on("error", function (err) {
console.log("Error " + err);
});
client.hmset("hash", "k1", "v1", "k2", "v2", function(e, o) {
console.log(e, o);
client.hgetall("hash", function(e, o) {
console.log(e, o);
client.eval("return redis.call('hgetall', KEYS[1])", 1, "hash", function(e, o) {
console.log(e, o);
});
});
});
This will yield the following:
$ node redisdebug.js
null 'OK'
null { k1: 'v1', k2: 'v2' }
null [ 'k1', 'v1', 'k2', 'v2' ]
I would expect both the 2nd and 3rd line to be the same because obviously they're both the result of an hgetallcall.
Ahh -- the javascript client's hgetall command is special-cased to convert the reply into an object. As the client has no way to know what the return type of an eval command is, it doesn't know what to give you, and just gives you the raw redis reply, which is key1, value1, key2, value2.
Exactly. Now, node_redis does the conversion internally. It'd just be nice if there was an option to tell node_redis: this is an eval, please yield an object.
The refactoring work I'm doing should support something like this, but it will still be up to the user, as not all eval scripts return objects.
I would be open for a pull request for this.