aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-10-05 15:36:38 +0200
committerKim Alvefur <zash@zash.se>2021-10-05 15:36:38 +0200
commit924064a30ad560e9aecc2d5e3b72f6a4a85b40ae (patch)
tree4290a3b105c06977ed6de3fc730e345deb819757 /core
parenta8b0c56f656fcd3fca033dc5f3154a4d8a2464a2 (diff)
downloadprosody-924064a30ad560e9aecc2d5e3b72f6a4a85b40ae.tar.gz
prosody-924064a30ad560e9aecc2d5e3b72f6a4a85b40ae.zip
core.moduleapi: Allow specifying an acceptable range for number options
Diffstat (limited to 'core')
-rw-r--r--core/features.lua1
-rw-r--r--core/moduleapi.lua16
2 files changed, 15 insertions, 2 deletions
diff --git a/core/features.lua b/core/features.lua
index 2948a1a7..41d8ce70 100644
--- a/core/features.lua
+++ b/core/features.lua
@@ -18,5 +18,6 @@ return {
-- new moduleapi methods
"getopt-enum";
+ "getopt-interval";
};
};
diff --git a/core/moduleapi.lua b/core/moduleapi.lua
index 68818b8d..87a25ae7 100644
--- a/core/moduleapi.lua
+++ b/core/moduleapi.lua
@@ -232,12 +232,24 @@ function api:get_option_string(name, default_value)
return tostring(value);
end
-function api:get_option_number(name, ...)
- local value = self:get_option_scalar(name, ...);
+function api:get_option_number(name, default_value, min, max)
+ local value = self:get_option_scalar(name, default_value);
local ret = tonumber(value);
if value ~= nil and ret == nil then
self:log("error", "Config option '%s' not understood, expecting a number", name);
end
+ if ret == default_value then
+ -- skip interval checks for default or nil
+ return ret;
+ 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