From e33a823ac8d8242fb606b3887c2eab2c3c99c768 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 15 Aug 2024 17:37:46 +0100 Subject: [PATCH] [Minor] Slight cleanup --- lualib/lua_cfg_transform.lua | 32 +++++++++++++++++++++++--------- test/lua/unit/ucl.lua | 6 +++--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lualib/lua_cfg_transform.lua b/lualib/lua_cfg_transform.lua index 5b1ab2e1c..b928cd88a 100644 --- a/lualib/lua_cfg_transform.lua +++ b/lualib/lua_cfg_transform.lua @@ -99,10 +99,12 @@ local function convert_metric(cfg, metric) end if metric:at('actions') then - cfg.actions = lua_util.override_defaults(cfg:at('actions'):unwrap(), metric:at('actions'):unwrap()) + local existing_actions = cfg:at('actions') and cfg:at('actions'):unwrap() or {} + cfg.actions = lua_util.override_defaults(existing_actions, metric:at('actions'):unwrap()) logger.infox("overriding actions from the legacy metric settings") end if metric:at('unknown_weight') then + logger.infox("overriding unknown weight from the legacy metric settings") cfg:at('actions').unknown_weight = metric:at('unknown_weight'):unwrap() end @@ -143,9 +145,19 @@ return function(cfg) local ret = false if cfg:at('metric') then - for _, v in cfg:at('metric'):pairs() do - if v:type() == 'object' then - convert_metric(cfg, v) + local metric = cfg:at('metric') + + -- There are two things that we can have (old `metric_pairs` logic) + -- 1. A metric is a single metric definition like: metric { name = "default", ... } + -- 2. A metric is a list of metrics like: metric { "default": ... } + if metric:at('actions') or metric:at('name') then + convert_metric(cfg, metric) + else + for _, v in cfg:at('metric'):pairs() do + if v:type() == 'object' then + logger.infox('converting metric element %s', v) + convert_metric(cfg, v) + end end end ret = true @@ -169,8 +181,8 @@ return function(cfg) 'reject', 'discard' } local actions = cfg:at('actions') - if actions and (not actions:at('no action') and not actions:at('no_action') and - not actions:at('accept')) then + if not actions:at('no action') and not actions:at('no_action') and + not actions:at('accept') then for _, d in ipairs(actions_defs) do if actions:at(d) then @@ -263,9 +275,11 @@ return function(cfg) -- If neural network is enabled we MUST have `check_all_filters` flag if cfg:at('neural') then - if not cfg:at('options'):at('check_all_filters') then - logger.infox(rspamd_config, 'enable `options.check_all_filters` for neural network') - cfg:at('options')['check_all_filters'] = true + if cfg:at('options') then + if not cfg:at('options'):at('check_all_filters') then + logger.infox(rspamd_config, 'enable `options.check_all_filters` for neural network') + cfg:at('options')['check_all_filters'] = true + end end end diff --git a/test/lua/unit/ucl.lua b/test/lua/unit/ucl.lua index 991afc979..9bbf7225c 100644 --- a/test/lua/unit/ucl.lua +++ b/test/lua/unit/ucl.lua @@ -37,10 +37,10 @@ context("UCL manipulation", function() ireply[1] = 1 ireply[1] = 1 ireply[1] = 1 - ireply[#ireply + 1] = 100500 - local iexpected = { 1, 1, 1, 1, 1, "e1", "e2", 100500 } + ireply[ireply:len() + 1] = 100500 + local iexpected = { 1, "e2", 100500 } for k, v in ireply:ipairs() do - assert_equal(iexpected[k], v:unwrap()) + assert_equal(v:unwrap(), iexpected[k]) end reply.tbl = ireply -- 2.39.5