__bsontype = luaejdb.BSON_UNDEFINED
}
+local mtBSON_INT = {
+ __bsontype = luaejdb.BSON_INT
+}
+
+local mtBSON_DOUBLE = {
+ __bsontype = luaejdb.BSON_DOUBLE
+}
+
+local mtBSON_LONG = {
+ __bsontype = luaejdb.BSON_LONG
+}
+
+local mtBSON_OBJECT = {
+ __bsontype = luaejdb.BSON_OBJECT
+}
+
+local mtBSON_BOOL = {
+ __bsontype = luaejdb.BSON_BOOL
+}
+
+
function luaejdb.toOID(val)
assert(type(val) == "string" and #val == 24)
return setmetatable({ val }, mtBSON_OID);
end
-- Generic key=value
-function B:KeyVal(key, val)
+function B:KV(key, val)
self:F(key);
self:_setop(nil, val, nil, true)
return self
}
default:
break;
-
}
}
}
int lua_from_bson(lua_State *L) {
- const void *bsdata = luaL_checkstring(L, lua_gettop(L));
+ size_t slen = 0;
+ const void *bsdata = luaL_checklstring(L, lua_gettop(L), &slen);
+ if (slen <= 4 || !bsdata) {
+ return luaL_error(L, "Invalid bson string at argument #1");
+ }
bson_iterator it;
bson_iterator_from_buffer(&it, bsdata);
lua_push_bson_table(L, &it);
if (key) bson_append_finish_object(bs);
}
} else { //metafield __bsontype on top
- assert(key);
int bson_type = lua_tointeger(L, -1);
+ if (!key && bson_type != BSON_OBJECT && bson_type != BSON_ARRAY) {
+ lua_pop(L, 1);
+ luaL_error(L, "Invalid object structure");
+ }
lua_pop(L, 1); //-metafield __bsontype
lua_rawgeti(L, -1, 1); //get first value
switch (bson_type) {
case BSON_UNDEFINED:
bson_append_undefined(bs, key);
break;
+ case BSON_OBJECT:
+ if (key) bson_append_start_object(bs, key);
+ lua_val_to_bson(L, NULL, vpos, bs, tref);
+ if (key) bson_append_finish_object(bs);
+ break;
+ case BSON_ARRAY:
+ if (key) bson_append_start_array(bs, key);
+ lua_val_to_bson(L, NULL, vpos, bs, tref);
+ if (key) bson_append_finish_array(bs);
+ break;
+ case BSON_DOUBLE:
+ bson_append_double(bs, key, (double) lua_tonumber(L, -1));
+ break;
+ case BSON_INT:
+ bson_append_int(bs, key, (int32_t) lua_tonumber(L, -1));
+ break;
+ case BSON_LONG:
+ bson_append_long(bs, key, (int64_t) lua_tonumber(L, -1));
+ break;
+ case BSON_BOOL:
+ bson_append_bool(bs, key, lua_toboolean(L, -1));
+ break;
default:
break;
}