Skip to content

Commit

Permalink
bson功能扩展
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed May 11, 2024
1 parent 44db378 commit d768b29
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
16 changes: 8 additions & 8 deletions extend/lbson/src/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ namespace lbson {
return sprintf(str, "%zd", i);
}

void write_int64(lua_State* L) {
void pack_int64(lua_State* L) {
lua_getfield(L, -1, "value");
m_buffer.write<uint64_t>(lua_tointeger(L, -1));
lua_pop(L, 1);
}

void write_string(lua_State* L) {
void pack_string(lua_State* L) {
size_t data_len;
lua_getfield(L, -1, "value");
const char* data = lua_tolstring(L, -1, &data_len);
m_buffer.push_data((uint8_t*)data, data_len);
lua_pop(L, 1);
}

void write_binary(lua_State* L) {
void pack_binary(lua_State* L) {
lua_guard g(L);
size_t bin_len;
lua_getfield(L, -1, "binrary");
Expand All @@ -206,7 +206,7 @@ namespace lbson {
m_buffer.push_data((uint8_t*)bin, bin_len);
}

void write_regex(lua_State* L) {
void pack_regex(lua_State* L) {
lua_guard g(L);
size_t regex_len;
lua_getfield(L, -1, "pattern");
Expand Down Expand Up @@ -326,21 +326,21 @@ namespace lbson {
case bson_type::BSON_NULL:
break;
case bson_type::BSON_BINARY:
write_binary(L);
pack_binary(L);
break;
case bson_type::BSON_DATE:
case bson_type::BSON_INT64:
case bson_type::BSON_TIMESTAMP:
write_int64(L);
pack_int64(L);
break;
case bson_type::BSON_ARRAY:
case bson_type::BSON_JSCODE:
case bson_type::BSON_DOCUMENT:
case bson_type::BSON_OBJECTID:
write_string(L);
pack_string(L);
break;
case bson_type::BSON_REGEX:
write_regex(L);
pack_regex(L);
break;
default:
luaL_error(L, "Invalid value type : %d", (int)type);
Expand Down
24 changes: 24 additions & 0 deletions extend/lbson/src/lbson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ namespace lbson {
llbson.set_function("pairs", pairs);
llbson.set_function("regex", regex);
llbson.set_function("date", date);
llbson.new_enum("BSON_TYPE",
"BSON_EOO", bson_type::BSON_EOO,
"BSON_REAL", bson_type::BSON_REAL,
"BSON_STRING", bson_type::BSON_STRING,
"BSON_DOCUMENT", bson_type::BSON_DOCUMENT,
"BSON_ARRAY", bson_type::BSON_ARRAY,
"BSON_BINARY", bson_type::BSON_BINARY,
"BSON_UNDEFINED", bson_type::BSON_UNDEFINED,
"BSON_OBJECTID", bson_type::BSON_OBJECTID,
"BSON_BOOLEAN", bson_type::BSON_BOOLEAN,
"BSON_DATE", bson_type::BSON_DATE,
"BSON_NULL", bson_type::BSON_NULL,
"BSON_REGEX", bson_type::BSON_REGEX,
"BSON_DBPOINTER", bson_type::BSON_DBPOINTER,
"BSON_JSCODE", bson_type::BSON_JSCODE,
"BSON_SYMBOL", bson_type::BSON_SYMBOL,
"BSON_CODEWS", bson_type::BSON_CODEWS,
"BSON_INT32", bson_type::BSON_INT32,
"BSON_TIMESTAMP", bson_type::BSON_TIMESTAMP,
"BSON_INT64", bson_type::BSON_INT64,
"BSON_INT128", bson_type::BSON_INT128,
"BSON_MINKEY", bson_type::BSON_MINKEY,
"BSON_MAXKEY", bson_type::BSON_MAXKEY
);
return llbson;
}
}
Expand Down

0 comments on commit d768b29

Please sign in to comment.