From c7f5145e9e4f15e83d282ab12c8d9b9a22959d64 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Mon, 11 May 2015 18:20:17 +0800 Subject: [PATCH] update sproto , add new api sproto:default --- lualib-src/sproto/lsproto.c | 50 +++++++++++++++++++++++++++++++++++++ lualib/sproto.lua | 15 +++++++++++ 2 files changed, 65 insertions(+) diff --git a/lualib-src/sproto/lsproto.c b/lualib-src/sproto/lsproto.c index c33118d4..99448487 100644 --- a/lualib-src/sproto/lsproto.c +++ b/lualib-src/sproto/lsproto.c @@ -573,6 +573,55 @@ lloadproto(lua_State *L) { return 1; } +static int +encode_default(const struct sproto_arg *args) { + lua_State *L = args->ud; + lua_pushstring(L, args->tagname); + if (args->index > 0) { + lua_newtable(L); + } else { + switch(args->type) { + case SPROTO_TINTEGER: + lua_pushinteger(L, 0); + break; + case SPROTO_TBOOLEAN: + lua_pushboolean(L, 0); + break; + case SPROTO_TSTRING: + lua_pushliteral(L, ""); + break; + case SPROTO_TSTRUCT: + lua_createtable(L, 0, 1); + lua_pushstring(L, sproto_name(args->subtype)); + lua_setfield(L, -2, "__type"); + break; + } + } + lua_rawset(L, -3); + return 0; +} + +/* + lightuserdata sproto_type + return default table + */ +static int +ldefault(lua_State *L) { + int ret; + // 32 is enough for dummy buffer, because ldefault encode nothing but the header. + char dummy[32]; + struct sproto_type * st = lua_touserdata(L, 1); + if (st == NULL) { + return luaL_argerror(L, 1, "Need a sproto_type object"); + } + lua_newtable(L); + ret = sproto_encode(st, dummy, sizeof(dummy), encode_default, L); + if (ret<0) { + return luaL_error(L, "dummy buffer (%d) is too small", (int)sizeof(dummy)); + } + return 1; +} + int luaopen_sproto_core(lua_State *L) { #ifdef luaL_checkversion @@ -587,6 +636,7 @@ luaopen_sproto_core(lua_State *L) { { "protocol", lprotocol }, { "loadproto", lloadproto }, { "saveproto", lsaveproto }, + { "default", ldefault }, { NULL, NULL }, }; luaL_newlib(L,l); diff --git a/lualib/sproto.lua b/lualib/sproto.lua index a9466f9c..271e692d 100644 --- a/lualib/sproto.lua +++ b/lualib/sproto.lua @@ -122,6 +122,21 @@ end sproto.pack = core.pack sproto.unpack = core.unpack +function sproto:default(typename, type) + if type == nil then + return core.default(querytype(self, typename)) + else + local p = queryproto(self, typename) + if type == "REQUEST" then + return core.default(p.request) + elseif type == "RESPONSE" then + return core.default(p.response) + else + error "Invalid type" + end + end +end + local header_tmp = {} local function gen_response(self, response, session)