Permalink
Browse files

Convert from table to array correctly - useful for $or queries.

git-svn-id: http://luamongo.googlecode.com/svn/trunk@44 17e306f6-9439-11de-a13a-71b910e68cc8
  • Loading branch information...
1 parent e42f34b commit 7546de3a7ef1b09bac8834aed884800f2e87fc53 nrich@ii.net committed Aug 7, 2010
Showing with 23 additions and 4 deletions.
  1. +23 −4 utils.cpp
View
@@ -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
BSONObjBuilder b;
lua_pushnil(L);
+
+ int isarray = 1;
+
while (lua_next(L, stackpos-1) != 0) {
- const char *k = lua_tostring(L, -2);
- lua_append_bson(L, k, -1, &b);
- lua_pop(L, 1);
+ if (lua_type(L, -2) == LUA_TNUMBER) {
+ int index = lua_tointeger(L, -2);
+
+ 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
{

0 comments on commit 7546de3

Please sign in to comment.