diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-08-21 15:30:35 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-08-21 17:58:51 +0100 |
commit | ef130e10c0577a1f2e5baf175767a7a0a474969f (patch) | |
tree | 469a26e35305c95e4b40b7304689e865e4683784 /lualib/lua_selectors | |
parent | 875321678ba17222c25b6a5d72dc012c8a5984fd (diff) | |
download | rspamd-ef130e10c0577a1f2e5baf175767a7a0a474969f.tar.gz rspamd-ef130e10c0577a1f2e5baf175767a7a0a474969f.zip |
[Feature] Selectors: Add flatten function
Diffstat (limited to 'lualib/lua_selectors')
-rw-r--r-- | lualib/lua_selectors/init.lua | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/lualib/lua_selectors/init.lua b/lualib/lua_selectors/init.lua index 817330598..f148c4d46 100644 --- a/lualib/lua_selectors/init.lua +++ b/lualib/lua_selectors/init.lua @@ -462,9 +462,33 @@ exports.combine_selectors = function(_, selectors, delimiter) end --[[[ +-- @function lua_selectors.flatten_selectors(selectors) +-- Convert selectors to a flat table of elements +--]] +exports.flatten_selectors = function(selectors) + local res = {} + + local function fill(tbl) + for _,s in ipairs(tbl) do + if type(s) == 'string' then + rawset(res, #res + 1, s) + elseif type(s) == 'userdata' then + rawset(res, #res + 1, tostring(s)) + else + fill(s) + end + end + end + + fill(selectors) + + return res +end + +--[[[ -- @function lua_selectors.create_closure(cfg, selector_str, delimiter='') --]] -exports.create_selector_closure = function(cfg, selector_str, delimiter) +exports.create_selector_closure = function(cfg, selector_str, delimiter, flatten) local selector = exports.parse_selector(cfg, selector_str) if not selector then @@ -475,7 +499,11 @@ exports.create_selector_closure = function(cfg, selector_str, delimiter) local res = exports.process_selectors(task, selector) if res then - return exports.combine_selectors(nil, res, delimiter) + if flatten then + return exports.flatten_selectors(res) + else + return exports.combine_selectors(nil, res, delimiter) + end end return nil |