summaryrefslogtreecommitdiff
path: root/server/src/luaquerymapper.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/luaquerymapper.cc')
-rw-r--r--server/src/luaquerymapper.cc67
1 files changed, 32 insertions, 35 deletions
diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc
index 380960c..f593c28 100644
--- a/server/src/luaquerymapper.cc
+++ b/server/src/luaquerymapper.cc
@@ -29,35 +29,24 @@
#include <sstream>
#include <string>
-static bool hasField(lua_State *L, int i, std::string name)
-{
- lua_getfield(L, i, name.c_str());
- bool ret = lua_isnil(L, lua_gettop(L));
- lua_pop(L, 1);
- return ret;
-}
+#include "luautil.h"
static void loadResult(lua_State *L, QueryResult &res,
- std::vector<std::string> group = std::vector<std::string>())
+ std::vector<std::string> group = std::vector<std::string>())
{
- int grp = 0;
+ std::vector<std::string> groups;
std::vector<std::string>::iterator gi = group.begin();
while(gi != group.end()) {
const char *name = gi->c_str();
- printf("[%d: %s]", grp, name); fflush(stdout);
- if(grp == 0) grp = LUA_GLOBALSINDEX;
- if(hasField(L, grp, name)) {
- lua_newtable(L);
- lua_setfield(L, grp, name);
- lua_getfield(L, grp, name);
+ if(!Pracro::testField(L, groups, name)) {
+ Pracro::createField(L, groups, name);
}
- grp = lua_gettop(L);
+ groups.push_back(name);
gi++;
}
- printf("\n"); fflush(stdout);
-
+
std::string s;
std::stringstream timestamp; timestamp << res.timestamp;
@@ -66,33 +55,30 @@ static void loadResult(lua_State *L, QueryResult &res,
const char *name = v->first.c_str();
- if(grp == 0) grp = LUA_GLOBALSINDEX;
- if(hasField(L, grp, name)) {
- lua_newtable(L);
- lua_setfield(L, grp, name);
+ if(!Pracro::testField(L, group, name)) {
+ Pracro::createField(L, group, name);
}
- int val;
+ int top = lua_gettop(L);
+ Pracro::getField(L, group, name);
- lua_getfield(L, grp, name);
- val = lua_gettop(L);
lua_pushstring(L, v->second.c_str());
- lua_setfield(L, val, "value");
+ lua_setfield(L, -2, "value");
- lua_getfield(L, grp, name);
- val = lua_gettop(L);
lua_pushstring(L, timestamp.str().c_str());
- lua_setfield(L, val, "timestamp");
+ lua_setfield(L, -2, "timestamp");
- lua_getfield(L, grp, name);
- val = lua_gettop(L);
lua_pushstring(L, res.source.c_str());
- lua_setfield(L, val, "source");
+ lua_setfield(L, -2, "source");
+
+ while(lua_gettop(L) > top) lua_pop(L, 1);
v++;
}
- while(lua_gettop(L) > 1) lua_pop(L, 1);
+ while(lua_gettop(L)) lua_pop(L, 1);
+
+ printf("< %d\n", lua_gettop(L)); fflush(stdout);
std::map< std::string, QueryResult >::iterator g = res.groups.begin();
while(g != res.groups.end()) {
@@ -232,7 +218,7 @@ std::string LUAQueryMapper::automap(const std::string &name)
}
#ifdef TEST_LUAQUERYMAPPER
-//deps: exception.cc log.cc debug.cc
+//deps: exception.cc log.cc debug.cc luautil.cc
//cflags: -I.. ${LUA_CFLAGS}
//libs:${LUA_LIBS}
#include <test.h>
@@ -250,15 +236,26 @@ time_t now = time(NULL);
res.groups["test"].groups["subtest"].source = "test app 2";
res.groups["test"].groups["subtest"].timestamp = now + 1;
res.groups["test"].groups["subtest"].values["somevalue"] = "hello world!";
+ res.groups["test"].groups["subtest"].groups["subsubtest"].source = "test app 3";
+ res.groups["test"].groups["subtest"].groups["subsubtest"].timestamp = now + 2;
+ res.groups["test"].groups["subtest"].groups["subsubtest"].values["somevalue"] = "hello world!!";
LUAQueryMapper mapper;
mapper.addQueryResult(res);
// Test simple value forwarding with nesting
- std::string luamap = "return test.subtest.somevalue.value, test.subtest.somevalue.timestamp, test.subtest.somevalue.source";
+ std::string luamap = "return test.subtest.subsubtest.somevalue.value, test.subtest.subsubtest.somevalue.timestamp, test.subtest.subsubtest.somevalue.source";
Value value = mapper.map(luamap);
+ TEST_EQUAL_STR(value.value, "hello world!!", "Test value");
+ TEST_EQUAL_INT(value.timestamp, now + 2, "Test timestamp");
+ TEST_EQUAL_STR(value.source, "test app 3", "Test source");
+
+ luamap = "return test.subtest.somevalue.value, test.subtest.somevalue.timestamp, test.subtest.somevalue.source";
+
+ value = mapper.map(luamap);
+
TEST_EQUAL_STR(value.value, "hello world!", "Test value");
TEST_EQUAL_INT(value.timestamp, now + 1, "Test timestamp");
TEST_EQUAL_STR(value.source, "test app 2", "Test source");