Skip to content

Commit

Permalink
bson dictionary's key can't be number
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed Jul 4, 2017
1 parent 86883c7 commit d6eb49e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
9 changes: 3 additions & 6 deletions lualib-src/lua-bson.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth)
append_key(bs, L, BSON_BOOLEAN, key, sz);
write_byte(bs, lua_toboolean(L,-1));
break;
case LUA_TNIL:
luaL_error(L, "Bson array has a hole (nil), Use bson.null instead");
default:
luaL_error(L, "Invalid value type : %s", lua_typename(L,vt));
}
Expand Down Expand Up @@ -452,12 +454,7 @@ pack_dict_data(lua_State *L, struct bson *b, int depth, int kt) {
size_t sz;
switch(kt) {
case LUA_TNUMBER:
// copy key, don't change key type
lua_pushvalue(L,-2);
lua_insert(L,-2);
key = lua_tolstring(L,-2,&sz);
append_one(b, L, key, sz, depth);
lua_pop(L,2);
luaL_error(L, "Bson dictionary's key can't be number");
break;
case LUA_TSTRING:
key = lua_tolstring(L,-2,&sz);
Expand Down
12 changes: 6 additions & 6 deletions test/testbson.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ end

local obj_a = {
__data = {
[1] = 2,
[3] = 4,
[5] = 6,
["1"] = 2,
["3"] = 4,
["5"] = 6,
}
}

Expand All @@ -38,9 +38,9 @@ setmetatable(

local obj_b = {
__data = {
[7] = 8,
[9] = 10,
[11] = obj_a,
["7"] = 8,
["9"] = 10,
["11"] = obj_a,
}
}

Expand Down

0 comments on commit d6eb49e

Please sign in to comment.