diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-04-05 12:44:59 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-04-05 12:44:59 +0100 |
commit | f422d322ec1b36547f4c8f95f13cb631ca387ed4 (patch) | |
tree | de399ed4a3c7a8871e537b325f844ee2334ff72e /lualib | |
parent | 6d75049c12a18b1fb07cadcd4ca605debe203977 (diff) | |
download | rspamd-f422d322ec1b36547f4c8f95f13cb631ca387ed4.tar.gz rspamd-f422d322ec1b36547f4c8f95f13cb631ca387ed4.zip |
[Minor] Add generic routine to override default settings
Diffstat (limited to 'lualib')
-rw-r--r-- | lualib/lua_util.lua | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index d97f58175..5af09d316 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -22,7 +22,7 @@ limitations under the License. local exports = {} local lpeg = require 'lpeg' local rspamd_util = require "rspamd_util" - +local fun = require "fun" local split_grammar = {} local function rspamd_str_split(s, sep) @@ -438,4 +438,41 @@ exports.fold_header = function(task, name, value, stop_chars) return rspamd_util.fold_header(name, value, how, stop_chars) end +--[[[ +-- @function lua_util.override_defaults(defaults, override) +-- Overrides values from defaults with override +--]] +local function override_defaults(def, override) + -- Corner cases + if not override or type(override) ~= 'table' then + return def + end + if not def or type(def) ~= 'table' then + return override + end + + local res = {} + fun.each(function(k, v) + if type(v) == 'table' then + if def[k] and type(def[k]) == 'table' then + -- Recursively override elements + res[k] = override_defaults(def[k], v) + else + res[k] = v + end + else + res[k] = v + end + end, override) + fun.each(function(k, v) + if not res[k] then + res[k] = v + end + end, def) + + return res +end + +exports.override_defaults = override_defaults + return exports |