]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Selectors: Add flatten function
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 21 Aug 2019 14:30:35 +0000 (15:30 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 21 Aug 2019 16:58:51 +0000 (17:58 +0100)
lualib/lua_selectors/init.lua

index 8173305981dbaaeb33bc2e61152407cfaed1b6ed..f148c4d4661e5cba2cc1e21d3f260eb82465f2a7 100644 (file)
@@ -461,10 +461,34 @@ exports.combine_selectors = function(_, selectors, delimiter)
   end
 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