Skip to content

Commit 71993e0

Browse files
nokute78edsiper
authored andcommitted
filter_lua: support new option "type_array_key" (#3343)
If set, the value of the key will be handled as array. It is useful to handle empty array. Signed-off-by: Takahiro Yamashita <[email protected]>
1 parent bd123ad commit 71993e0

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

plugins/filter_lua/lua.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ static int lua_arraylength(lua_State *l)
142142
}
143143

144144
static void lua_tomsgpack(struct lua_filter *lf, msgpack_packer *pck, int index);
145+
static void lua_toarray(struct lua_filter *lf, msgpack_packer *pck, int index)
146+
{
147+
int len;
148+
int i;
149+
lua_State *l = lf->lua->state;
150+
151+
lua_pushnumber(l, (lua_Number)lua_objlen(l, -1)); // lua_len
152+
len = (int)lua_tointeger(l, -1);
153+
lua_pop(l, 1);
154+
155+
msgpack_pack_array(pck, len);
156+
for (i = 1; i <= len; i++) {
157+
lua_rawgeti(l, -1, i);
158+
lua_tomsgpack(lf, pck, 0);
159+
lua_pop(l, 1);
160+
}
161+
}
145162
static void try_to_convert_data_type(struct lua_filter *lf,
146163
msgpack_packer *pck,
147164
int index)
@@ -154,19 +171,33 @@ static void try_to_convert_data_type(struct lua_filter *lf,
154171
struct mk_list *head = NULL;
155172
struct l2c_type *l2c = NULL;
156173

174+
// convert to int
157175
if ((lua_type(l, -2) == LUA_TSTRING)
158176
&& lua_type(l, -1) == LUA_TNUMBER){
159177
tmp = lua_tolstring(l, -2, &len);
160178

161179
mk_list_foreach_safe(head, tmp_list, &lf->l2c_types) {
162180
l2c = mk_list_entry(head, struct l2c_type, _head);
163-
if (!strncmp(l2c->key, tmp, len)) {
181+
if (!strncmp(l2c->key, tmp, len) && l2c->type == L2C_TYPE_INT) {
164182
lua_tomsgpack(lf, pck, -1);
165183
msgpack_pack_int64(pck, (int64_t)lua_tonumber(l, -1));
166184
return;
167185
}
168186
}
169187
}
188+
else if ((lua_type(l, -2) == LUA_TSTRING)
189+
&& lua_type(l, -1) == LUA_TTABLE){
190+
tmp = lua_tolstring(l, -2, &len);
191+
192+
mk_list_foreach_safe(head, tmp_list, &lf->l2c_types) {
193+
l2c = mk_list_entry(head, struct l2c_type, _head);
194+
if (!strncmp(l2c->key, tmp, len) && l2c->type == L2C_TYPE_ARRAY) {
195+
lua_tomsgpack(lf, pck, -1);
196+
lua_toarray(lf, pck, 0);
197+
return;
198+
}
199+
}
200+
}
170201

171202
/* not matched */
172203
lua_tomsgpack(lf, pck, -1);
@@ -585,6 +616,12 @@ static struct flb_config_map config_map[] = {
585616
"If these keys are matched, the fields are converted to integer. "
586617
"If more than one key, delimit by space."
587618
},
619+
{
620+
FLB_CONFIG_MAP_STR, "type_array_key", NULL,
621+
0, FLB_FALSE, 0,
622+
"If these keys are matched, the fields are converted to array. "
623+
"If more than one key, delimit by space."
624+
},
588625
{
589626
FLB_CONFIG_MAP_BOOL, "protected_mode", "true",
590627
0, FLB_TRUE, offsetof(struct lua_filter, protected_mode),

plugins/filter_lua/lua_config.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,26 @@ struct lua_filter *lua_config_create(struct flb_filter_instance *ins,
131131

132132
tmp_key = flb_strndup(sentry->value, sentry->len);
133133
l2c->key = flb_sds_create(tmp_key);
134+
l2c->type = L2C_TYPE_INT;
135+
flb_free(tmp_key);
136+
137+
mk_list_add(&l2c->_head, &lf->l2c_types);
138+
lf->l2c_types_num++;
139+
}
140+
flb_utils_split_free(split);
141+
}
142+
143+
tmp = flb_filter_get_property("type_array_key", ins);
144+
if (tmp) {
145+
split = flb_utils_split(tmp, ' ', L2C_TYPES_NUM_MAX);
146+
mk_list_foreach_safe(head, tmp_list, split) {
147+
l2c = flb_malloc(sizeof(struct l2c_type));
148+
149+
sentry = mk_list_entry(head, struct flb_split_entry, _head);
150+
151+
tmp_key = flb_strndup(sentry->value, sentry->len);
152+
l2c->key = flb_sds_create(tmp_key);
153+
l2c->type = L2C_TYPE_ARRAY;
134154
flb_free(tmp_key);
135155

136156
mk_list_add(&l2c->_head, &lf->l2c_types);

plugins/filter_lua/lua_config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,14 @@
2929
#define LUA_BUFFER_CHUNK 1024 * 8 /* 8K should be enough to get started */
3030
#define L2C_TYPES_NUM_MAX 16
3131

32+
enum l2c_type_enum {
33+
L2C_TYPE_INT,
34+
L2C_TYPE_ARRAY
35+
};
36+
3237
struct l2c_type {
3338
flb_sds_t key;
39+
int type;
3440
struct mk_list _head;
3541
};
3642

0 commit comments

Comments
 (0)