commit f7a044a: [Feature] Lua_maps: Allow static maps for key-value pairs
Vsevolod Stakhov
vsevolod at highsecure.ru
Fri Jul 19 12:56:06 UTC 2019
Author: Vsevolod Stakhov
Date: 2019-07-19 13:41:12 +0100
URL: https://github.com/rspamd/rspamd/commit/f7a044a1cb297b891af84e6cefae32217e97c300
[Feature] Lua_maps: Allow static maps for key-value pairs
---
lualib/lua_maps.lua | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)
diff --git a/lualib/lua_maps.lua b/lualib/lua_maps.lua
index 6b1a37590..6b478ded8 100644
--- a/lualib/lua_maps.lua
+++ b/lualib/lua_maps.lua
@@ -206,14 +206,30 @@ local function rspamd_map_add_from_ucl(opt, mtype, description)
else
local data = {}
local nelts = 0
+ -- Plain array of keys, count merely numeric elts
for _,elt in ipairs(opt) do
if type(elt) == 'string' then
- data[elt] = true
+ -- Numeric table
+ if mtype == 'hash' then
+ -- Treat as KV pair
+ local lua_util = require "lua_util"
+ local pieces = lua_util.str_split(elt, ' ')
+ if #pieces > 1 then
+ local key = table.remove(pieces, 1)
+ data[key] = table.concat(pieces, ' ')
+ else
+ data[elt] = true
+ end
+ else
+ data[elt] = true
+ end
+
nelts = nelts + 1
end
end
if nelts > 0 then
+ -- Plain Lua table that is used as a map
ret.__data = data
ret.get_key = function(t, k)
if k ~= '__data' then
@@ -222,11 +238,17 @@ local function rspamd_map_add_from_ucl(opt, mtype, description)
return nil
end
+
return ret
+ else
+ -- Empty map, huh?
+ rspamd_logger.errx(rspamd_config, 'invalid map element: %s',
+ opt)
end
end
end
else
+ -- We have some non-trivial object so let C code to deal with it somehow...
local map = rspamd_config:add_map{
type = mtype,
description = description,
@@ -237,7 +259,7 @@ local function rspamd_map_add_from_ucl(opt, mtype, description)
setmetatable(ret, ret_mt)
return ret
end
- end
+ end -- opt[1]
end
return nil
More information about the Commits
mailing list