commit 2b582c2: [Feature] Allow to specify `selector_alias` in the maps definition

Vsevolod Stakhov vsevolod at rspamd.com
Mon Dec 12 21:42:03 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-12-12 21:33:58 +0000
URL: https://github.com/rspamd/rspamd/commit/2b582c23dcb975ea5c8cedf278e4cf280901c2f0 (HEAD -> master)

[Feature] Allow to specify `selector_alias` in the maps definition

---
 lualib/lua_maps.lua           | 23 +++++++++++++++++++++++
 lualib/lua_selectors/init.lua |  8 ++++++++
 2 files changed, 31 insertions(+)

diff --git a/lualib/lua_maps.lua b/lualib/lua_maps.lua
index c7c5a983d..a436a60f0 100644
--- a/lualib/lua_maps.lua
+++ b/lualib/lua_maps.lua
@@ -247,6 +247,13 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
     return nil
   end
 
+  local function maybe_register_selector()
+    if opt.selector_alias then
+      local lua_selectors = require "lua_selectors"
+      lua_selectors.add_map(opt.selector_alias, ret)
+    end
+  end
+
   if type(opt) == 'string' then
     opt,mtype = maybe_adjust_type(opt, mtype)
     local cache_key = map_hash_key(opt, mtype)
@@ -303,6 +310,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         else
@@ -316,6 +325,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         end
@@ -331,6 +342,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         else
@@ -346,6 +359,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         end
@@ -361,6 +376,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = map
             setmetatable(ret, ret_mt)
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           end
         else
@@ -399,6 +416,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             end
 
             maps_cache[cache_key] = ret
+            maybe_register_selector()
+
             return ret
           else
             -- Empty map, huh?
@@ -418,6 +437,7 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
             ret.__data = opt
             ret.__external = true
             setmetatable(ret, ret_mt)
+            maybe_register_selector()
 
             return ret
           else
@@ -439,6 +459,8 @@ local function rspamd_map_add_from_ucl(opt, mtype, description, callback)
           ret.__data = map
           setmetatable(ret, ret_mt)
           maps_cache[cache_key] = ret
+          maybe_register_selector()
+
           return ret
         end
       end
@@ -539,6 +561,7 @@ end
 local direct_map_schema = ts.shape{ -- complex object
   name = ts.string:is_optional(),
   description = ts.string:is_optional(),
+  selector_alias = ts.string:is_optional(), -- an optional alias for the selectos framework
   timeout = ts.number,
   data = ts.array_of(ts.string):is_optional(),
   -- Tableshape has no options support for something like key1 or key2?
diff --git a/lualib/lua_selectors/init.lua b/lualib/lua_selectors/init.lua
index f85b9a487..39cf654f3 100644
--- a/lualib/lua_selectors/init.lua
+++ b/lualib/lua_selectors/init.lua
@@ -616,4 +616,12 @@ exports.list_transforms = function()
   return display_selectors(transform_function)
 end
 
+exports.add_map = function(name, map)
+  if not exports.maps[name] then
+    exports.maps[name] = map
+  else
+    logger.errx(rspamd_config, "duplicate map redefinition for the selectors: %s", name)
+  end
+end
+
 return exports


More information about the Commits mailing list