Skip to content

Commit

Permalink
Convert from table to array correctly - useful for $or queries.
Browse files Browse the repository at this point in the history
git-svn-id: http://luamongo.googlecode.com/svn/trunk@44 17e306f6-9439-11de-a13a-71b910e68cc8
  • Loading branch information
nrich@ii.net committed Aug 7, 2010
1 parent e42f34b commit 7546de3
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions utils.cpp
Expand Up @@ -118,12 +118,31 @@ static void lua_append_bson(lua_State *L, const char *key, int stackpos, BSONObj
// handle as a regular table, iterating keys // handle as a regular table, iterating keys
BSONObjBuilder b; BSONObjBuilder b;
lua_pushnil(L); lua_pushnil(L);

int isarray = 1;

while (lua_next(L, stackpos-1) != 0) { while (lua_next(L, stackpos-1) != 0) {
const char *k = lua_tostring(L, -2); if (lua_type(L, -2) == LUA_TNUMBER) {
lua_append_bson(L, k, -1, &b); int index = lua_tointeger(L, -2);
lua_pop(L, 1);
stringstream ss;
ss << index;

lua_append_bson(L, ss.str().c_str(), -1, &b);
lua_pop(L, 1);
} else {
const char *k = lua_tostring(L, -2);
lua_append_bson(L, k, -1, &b);
lua_pop(L, 1);
isarray = 0;
}
} }
builder->append(key, b.obj());
if (isarray) {
builder->appendArray(key, b.obj());
} else {
builder->append(key, b.obj());
}
} }
else else
{ {
Expand Down

0 comments on commit 7546de3

Please sign in to comment.