Skip to content
Browse files

added the typename method for converting the numeric type values retu…

…rned by "parse_reply" and "parse_replies" to textual type names.
  • Loading branch information...
1 parent 54f07af commit 9332b2f8e36a67e179fb2a96657f61fb9e70f4a7 @agentzh agentzh committed
Showing with 52 additions and 13 deletions.
  1. +29 −1 redis-parser.c
  2. +23 −12 t/sanity.t
View
30 redis-parser.c
@@ -24,7 +24,7 @@ enum {
};
-static const char *redis_reply_typenames = {
+static const char *redis_typenames[] = {
"bad reply",
"status reply",
"error reply",
@@ -50,12 +50,18 @@ static int parse_multi_bulk_reply(lua_State *L, char **src,
const char *last);
static size_t get_num_size(size_t i);
static char *sprintf_num(char *dst, int64_t ui64);
+static int redis_typename(lua_State *L);
+
+
+#define redis_nelems(arr) \
+ (sizeof(arr) / sizeof(arr[0]))
static const struct luaL_Reg redis_parser[] = {
{"parse_reply", redis_parse_reply},
{"parse_replies", redis_parse_replies},
{"build_query", redis_build_query},
+ {"typename", redis_typename},
{NULL, NULL}
};
@@ -655,3 +661,25 @@ sprintf_num(char *dst, int64_t ui64)
return dst + len;
}
+
+static int
+redis_typename(lua_State *L)
+{
+ int typ;
+
+ if (lua_gettop(L) != 1) {
+ return luaL_error(L, "expecting one argument but got %d",
+ lua_gettop(L));
+ }
+
+ typ = (int) luaL_checkinteger(L, 1);
+
+ if (typ < 0 || typ >= redis_nelems(redis_typenames)) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ lua_pushstring(L, redis_typenames[typ]);
+ return 1;
+}
+
View
35 t/sanity.t
@@ -15,10 +15,10 @@ __DATA__
parser = require("redis.parser")
reply = '+OK'
res, typ = parser.parse_reply(reply)
-print("typ == " .. typ .. ' == ' .. parser.BAD_REPLY)
+print("typ == " .. typ .. ' == ' .. parser.typename(typ) .. ' == ' .. parser.BAD_REPLY)
print("res == " .. res)
--- out
-typ == 0 == 0
+typ == 0 == bad reply == 0
res == bad status reply
@@ -28,10 +28,10 @@ res == bad status reply
parser = require("redis.parser")
reply = '+OK\r\n'
res, typ = parser.parse_reply(reply)
-print("typ == " .. typ .. ' == ' .. parser.STATUS_REPLY)
+print("typ == " .. typ .. ' == ' .. parser.typename(typ) .. ' == ' .. parser.STATUS_REPLY)
print("res == " .. res)
--- out
-typ == 1 == 1
+typ == 1 == status reply == 1
res == OK
@@ -41,10 +41,10 @@ res == OK
parser = require("redis.parser")
reply = '-Bad argument\rHey\r\nblah blah blah\r\n'
res, typ = parser.parse_reply(reply)
-print("typ == " .. typ .. ' == ' .. parser.ERROR_REPLY)
+print("typ == " .. typ .. ' == ' .. parser.typename(typ) .. ' == ' .. parser.ERROR_REPLY)
print("res == " .. res)
--- out eval
-"typ == 2 == 2
+"typ == 2 == error reply == 2
res == Bad argument\rHey\n"
@@ -54,11 +54,11 @@ res == Bad argument\rHey\n"
parser = require("redis.parser")
reply = ':-32\r\n'
res, typ = parser.parse_reply(reply)
-print("typ == " .. typ .. ' == ' .. parser.INTEGER_REPLY)
+print("typ == " .. typ .. ' == ' .. parser.typename(typ) .. ' == ' .. parser.INTEGER_REPLY)
print("res == " .. res)
print("res type == " .. type(res))
--- out
-typ == 3 == 3
+typ == 3 == integer reply == 3
res == -32
res type == number
@@ -99,10 +99,10 @@ res type == string
parser = require("redis.parser")
reply = '$5\r\nhello\r\n'
res, typ = parser.parse_reply(reply)
-print("typ == " .. typ .. ' == ' .. parser.BULK_REPLY)
+print("typ == " .. typ .. ' == ' .. parser.typename(typ) .. ' == ' .. parser.BULK_REPLY)
print("res == " .. res)
--- out
-typ == 4 == 4
+typ == 4 == bulk reply == 4
res == hello
@@ -256,10 +256,10 @@ cjson = require('cjson')
parser = require("redis.parser")
reply = '*1\r\n$1\r\na\r\n'
res, typ = parser.parse_reply(reply)
-print("typ == " .. typ .. ' == ' .. parser.MULTI_BULK_REPLY)
+print("typ == " .. typ .. ' == ' .. parser.typename(typ) .. ' == ' .. parser.MULTI_BULK_REPLY)
print("res == " .. cjson.encode(res))
--- out eval
-qq{typ == 5 == 5
+qq{typ == 5 == multi-bulk reply == 5
res == ["a"]\n}
@@ -456,3 +456,14 @@ print("res == " .. cjson.encode(resp))
--- out
res == [[["subscribe","ledge:d1d0ed5f3251473795548ab392181d06","1"],5],[["message","ledge:d1d0ed5f3251473795548ab392181d06","finished"],5]]
+
+
+=== TEST 35: bad typ
+--- lua
+local parser = require "redis.parser"
+print(parser.typename(-1))
+print(parser.typename(6))
+--- out
+nil
+nil
+

0 comments on commit 9332b2f

Please sign in to comment.
Something went wrong with that request. Please try again.