aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2023-07-17 00:37:44 +0200
committerKim Alvefur <zash@zash.se>2023-07-17 00:37:44 +0200
commit55768509a3f15b0476289f7a338d02e7233d1926 (patch)
tree3a55c72bdc012ab656111b748a649ab04938f222 /core
parent210f60808657a25913b0ac559ab8a91c93aeb218 (diff)
downloadprosody-55768509a3f15b0476289f7a338d02e7233d1926.tar.gz
prosody-55768509a3f15b0476289f7a338d02e7233d1926.zip
core.moduleapi: Add min/max range support to :get_option_period
To match :get_option_number etc, specifying the allowed interval. Default is essentially (0, inf].
Diffstat (limited to 'core')
-rw-r--r--core/moduleapi.lua38
1 files changed, 27 insertions, 11 deletions
diff --git a/core/moduleapi.lua b/core/moduleapi.lua
index 7c9b357f..9a4100f7 100644
--- a/core/moduleapi.lua
+++ b/core/moduleapi.lua
@@ -268,27 +268,43 @@ function api:get_option_integer(name, default_value, min, max)
return value;
end
-function api:get_option_period(name, default_value)
+function api:get_option_period(name, default_value, min, max)
local value = self:get_option_scalar(name, default_value);
- if type(value) == "number" then
- if value < 0 then
- self:log("debug", "Treating negative period as infinity");
- return math.huge;
- end
- -- assume seconds
- return value;
- elseif value == "never" or value == false then
+
+ local ret;
+ if value == "never" or value == false then
-- usually for disabling some periodic thing
return math.huge;
+ elseif type(value) == "number" then
+ -- assume seconds
+ ret = value;
elseif type(value) == "string" then
- local ret = human_io.parse_duration(value);
+ ret = human_io.parse_duration(value);
if value ~= nil and ret == nil then
self:log("error", "Config option '%s' not understood, expecting a period (e.g. \"2 days\")", name);
end
- return ret;
elseif value ~= nil then
self:log("error", "Config option '%s' expects a number or a period description string (e.g. \"3 hours\"), not %s", name, type(value));
+ return nil;
+ else
+ return nil;
+ end
+
+ if ret < 0 then
+ self:log("debug", "Treating negative period as infinity");
+ return math.huge;
end
+
+ if min and ret < min then
+ self:log("warn", "Config option '%s' out of bounds %g < %g", name, ret, min);
+ return min;
+ end
+ if max and ret > max then
+ self:log("warn", "Config option '%s' out of bounds %g > %g", name, ret, max);
+ return max;
+ end
+
+ return ret;
end
function api:get_option_boolean(name, ...)