Skip to content

Commit

Permalink
lua: support nil_str to represent nil in Lua.
Browse files Browse the repository at this point in the history
Nil value is to delete key/value from associative array in Lua.
It means a key/value is removed from record if its value is null.
nil_str is to replace null by nil_str string to prevent it.

Signed-off-by: Takahiro Yamashita <[email protected]>
  • Loading branch information
nokute78 committed Aug 6, 2023
1 parent 5686334 commit 410fbac
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
5 changes: 3 additions & 2 deletions include/fluent-bit/flb_lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ struct flb_lua_l2c_type {

struct flb_lua_l2c_config {
int l2c_types_num; /* number of l2c_types */
flb_sds_t l2c_nil_str; /* string to represent nil value */
struct mk_list l2c_types; /* data types (lua -> C) */
};

int flb_lua_arraylength(lua_State *l);
void flb_lua_pushtimetable(lua_State *l, struct flb_time *tm);
int flb_lua_is_valid_func(lua_State *l, flb_sds_t func);
int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader);
void flb_lua_pushmsgpack(lua_State *l, msgpack_object *o);
int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader, flb_sds_t nil_str);
void flb_lua_pushmsgpack(lua_State *l, msgpack_object *o, flb_sds_t nil_str);
void flb_lua_tomsgpack(lua_State *l,
msgpack_packer *pck,
int index,
Expand Down
60 changes: 45 additions & 15 deletions src/flb_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int flb_lua_is_valid_func(lua_State *lua, flb_sds_t func)
return ret;
}

int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader)
int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader, flb_sds_t nil_str)
{
int ret = 0;
mpack_tag_t tag;
Expand All @@ -63,7 +63,17 @@ int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader)
tag = mpack_read_tag(reader);
switch (mpack_tag_type(&tag)) {
case mpack_type_nil:
lua_pushnil(l);
if (nil_str) {
/*
The nil value is a special value to delete key/value from associative array.
It means that pairs that contain nil value will disappear.
For that reason, we push "nil" string instead of the nil value.
*/
lua_pushlstring(l, nil_str, flb_sds_len(nil_str));
}
else {
lua_pushnil(l);
}
break;
case mpack_type_bool:
lua_pushboolean(l, mpack_tag_bool_value(&tag));
Expand Down Expand Up @@ -91,7 +101,7 @@ int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader)
length = mpack_tag_array_count(&tag);
lua_createtable(l, length, 0);
for (i = 0; i < length; i++) {
ret = flb_lua_pushmpack(l, reader);
ret = flb_lua_pushmpack(l, reader, nil_str);
if (ret) {
return ret;
}
Expand All @@ -102,11 +112,11 @@ int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader)
length = mpack_tag_map_count(&tag);
lua_createtable(l, length, 0);
for (i = 0; i < length; i++) {
ret = flb_lua_pushmpack(l, reader);
ret = flb_lua_pushmpack(l, reader, nil_str);
if (ret) {
return ret;
}
ret = flb_lua_pushmpack(l, reader);
ret = flb_lua_pushmpack(l, reader, nil_str);
if (ret) {
return ret;
}
Expand All @@ -119,7 +129,7 @@ int flb_lua_pushmpack(lua_State *l, mpack_reader_t *reader)
return 0;
}

void flb_lua_pushmsgpack(lua_State *l, msgpack_object *o)
void flb_lua_pushmsgpack(lua_State *l, msgpack_object *o, flb_sds_t nil_str)
{
int i;
int size;
Expand All @@ -128,7 +138,17 @@ void flb_lua_pushmsgpack(lua_State *l, msgpack_object *o)

switch(o->type) {
case MSGPACK_OBJECT_NIL:
lua_pushnil(l);
if (nil_str) {
/*
The nil value is a special value to delete key/value from associative array.
It means that pairs that contain nil value will disappear.
For that reason, we push "nil" string instead of the nil value.
*/
lua_pushlstring(l, nil_str, flb_sds_len(nil_str));
}
else {
lua_pushnil(l);
}
break;

case MSGPACK_OBJECT_BOOLEAN:
Expand Down Expand Up @@ -166,7 +186,7 @@ void flb_lua_pushmsgpack(lua_State *l, msgpack_object *o)
if (size != 0) {
msgpack_object *p = o->via.array.ptr;
for (i = 0; i < size; i++) {
flb_lua_pushmsgpack(l, p+i);
flb_lua_pushmsgpack(l, p+i, nil_str);
lua_rawseti (l, -2, i+1);
}
}
Expand All @@ -178,8 +198,8 @@ void flb_lua_pushmsgpack(lua_State *l, msgpack_object *o)
if (size != 0) {
msgpack_object_kv *p = o->via.map.ptr;
for (i = 0; i < size; i++) {
flb_lua_pushmsgpack(l, &(p+i)->key);
flb_lua_pushmsgpack(l, &(p+i)->val);
flb_lua_pushmsgpack(l, &(p+i)->key, nil_str);
flb_lua_pushmsgpack(l, &(p+i)->val, nil_str);
lua_settable(l, -3);
}
}
Expand Down Expand Up @@ -419,8 +439,13 @@ void flb_lua_tompack(lua_State *l,
size_t len;

str = lua_tolstring(l, -1 + index, &len);

mpack_write_str(writer, str, len);
if (l2cc->l2c_nil_str && strlen(str) == flb_sds_len(l2cc->l2c_nil_str) &&
!strncmp(str, l2cc->l2c_nil_str, flb_sds_len(l2cc->l2c_nil_str))) {
mpack_write_nil(writer);
}
else {
mpack_write_str(writer, str, len);
}
}
break;
case LUA_TNUMBER:
Expand Down Expand Up @@ -509,9 +534,14 @@ void flb_lua_tomsgpack(lua_State *l,
size_t len;

str = lua_tolstring(l, -1 + index, &len);

msgpack_pack_str(pck, len);
msgpack_pack_str_body(pck, str, len);
if (l2cc->l2c_nil_str && strlen(str) == flb_sds_len(l2cc->l2c_nil_str) &&
!strncmp(str, l2cc->l2c_nil_str, flb_sds_len(l2cc->l2c_nil_str))) {
msgpack_pack_nil(pck);
}
else {
msgpack_pack_str(pck, len);
msgpack_pack_str_body(pck, str, len);
}
}
break;
case LUA_TNUMBER:
Expand Down
6 changes: 4 additions & 2 deletions tests/internal/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static void test_pushmsgpack()

msgpack_unpacked_init(&msg);
msgpack_unpack_next(&msg, sbuf.data, sbuf.size, NULL);
flb_lua_pushmsgpack(l, &msg.data);
flb_lua_pushmsgpack(l, &msg.data, NULL);
check_equals(l, "{ [1] = { [key] = value } [2] = msgpack-str [3] = 4 }");

msgpack_unpacked_destroy(&msg);
Expand All @@ -137,7 +137,7 @@ static void test_pushmpack()
msgpack_pack_int(&pck, 4);

mpack_reader_init_data(&reader, sbuf.data, sbuf.size);
flb_lua_pushmpack(l, &reader);
flb_lua_pushmpack(l, &reader, NULL);
check_equals(l, "{ [1] = { [key] = value } [2] = msgpack-str [3] = 4 }");

msgpack_sbuffer_destroy(&sbuf);
Expand All @@ -158,6 +158,7 @@ static void test_tomsgpack()
msgpack_packer_init(&pck, &sbuf, msgpack_sbuffer_write);
mk_list_init(&l2cc.l2c_types);
l2cc.l2c_types_num = 0;
l2cc.l2c_nil_str = NULL;

lua_getglobal(l, "obj");
flb_lua_tomsgpack(l, &pck, 0, &l2cc);
Expand Down Expand Up @@ -188,6 +189,7 @@ static void test_tompack()
mpack_writer_init(&writer, buf, sizeof(buf));
mk_list_init(&l2cc.l2c_types);
l2cc.l2c_types_num = 0;
l2cc.l2c_nil_str = NULL;

lua_getglobal(l, "obj");
flb_lua_tompack(l, &writer, 0, &l2cc);
Expand Down

0 comments on commit 410fbac

Please sign in to comment.