Skip to content

Commit

Permalink
add new api sproto:default
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed May 11, 2015
1 parent 6d11b55 commit 00f6513
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
50 changes: 50 additions & 0 deletions lsproto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -587,6 +636,7 @@ luaopen_sproto_core(lua_State *L) {
{ "protocol", lprotocol },
{ "loadproto", lloadproto },
{ "saveproto", lsaveproto },
{ "default", ldefault },
{ NULL, NULL },
};
luaL_newlib(L,l);
Expand Down
4 changes: 4 additions & 0 deletions sproto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ end
sproto.pack = core.pack
sproto.unpack = core.unpack

function sproto:default(typename)
return core.default(core.querytype(self.__cobj, typename))
end

local header_tmp = {}

local function gen_response(self, response, session)
Expand Down
18 changes: 11 additions & 7 deletions test.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local parser = require "sprotoparser"
local sproto = require "sproto"
local core = require "sproto.core"
local print_r = require "print_r"

local sp = parser.parse [[
local sp = sproto.parse [[
.Person {
name 0 : string
id 1 : integer
Expand All @@ -22,9 +22,13 @@ local sp = parser.parse [[
}
]]

sp = core.newproto(sp)
core.dumpproto(sp)
local st = core.querytype(sp, "AddressBook")
-- core.dumpproto only for debug use
core.dumpproto(sp.__cobj)

local def = sp:default "Person"
print("default table for Person")
print_r(def)
print("--------------")

local ab = {
person = {
Expand Down Expand Up @@ -57,6 +61,6 @@ local ab = {

collectgarbage "stop"

local code = core.encode(st, ab)
local addr = core.decode(st, code)
local code = sp:encode("AddressBook", ab)
local addr = sp:decode("AddressBook", code)
print_r(addr)

0 comments on commit 00f6513

Please sign in to comment.