diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-05-05 15:57:47 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-05-05 15:57:47 +0100 |
commit | e80aae67f2e95519c8dd5259f57b8dc32b97487d (patch) | |
tree | fdb1069a6148d9d69bbfd123974509eb58a162cd /src/lua/lua_config.c | |
parent | e86c789d1b42d8197be9da78d5af370cdb79d17f (diff) | |
download | rspamd-e80aae67f2e95519c8dd5259f57b8dc32b97487d.tar.gz rspamd-e80aae67f2e95519c8dd5259f57b8dc32b97487d.zip |
`get_all_opt` now flattens the content of objects
So now in a configuration with multiple section with the same name, for
example:
module {
param1 = value1;
param2 = value2;
}
module {
param2 = value2_1;
param3 = value3;
}
this function will return a single lua table that looks as following:
param1 = value1,
param2 = value2_1,
param3 = value3
Diffstat (limited to 'src/lua/lua_config.c')
-rw-r--r-- | src/lua/lua_config.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/lua/lua_config.c b/src/lua/lua_config.c index ad980e57e..938c0fa62 100644 --- a/src/lua/lua_config.c +++ b/src/lua/lua_config.c @@ -28,6 +28,7 @@ #include "message.h" #include "radix.h" #include "expression.h" +#include "utlist.h" /*** * This module is used to configure rspamd and is normally available as global @@ -57,7 +58,8 @@ local opts = rspamd_config:get_key('options') -- get content of the specified ke LUA_FUNCTION_DEF (config, get_module_opt); /*** * @method rspamd_config:get_all_opt(mname) - * Returns value of all options for a module `mname`, + * Returns value of all options for a module `mname`, flattening values into a single table consisting + * of all sections with such a name. * @param {string} mname name of module * @return {table} table of all options for `mname` or `nil` if a module's configuration is not found */ @@ -407,19 +409,38 @@ lua_config_get_all_opt (lua_State * L) { struct rspamd_config *cfg = lua_check_config (L, 1); const gchar *mname; - const ucl_object_t *obj; + const ucl_object_t *obj, *cur, *cur_elt; + ucl_object_iter_t it = NULL; if (cfg) { mname = luaL_checkstring (L, 2); if (mname) { obj = ucl_obj_get_key (cfg->rcl_obj, mname); - if (obj != NULL) { - return ucl_object_push_lua (L, obj, TRUE); + /* Flatten object */ + if (obj != NULL && ucl_object_type (obj) == UCL_OBJECT) { + + lua_newtable (L); + it = ucl_object_iterate_new (obj); + + LL_FOREACH (obj, cur) { + it = ucl_object_iterate_reset (it, cur); + + while ((cur_elt = ucl_object_iterate_safe (it, true))) { + lua_pushstring (L, ucl_object_key (cur_elt)); + ucl_object_push_lua (L, cur_elt, true); + lua_settable (L, -3); + } + } + + ucl_object_iterate_free (it); + + return 1; } } } lua_pushnil (L); + return 1; } |