aboutsummaryrefslogtreecommitdiffstats
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
parenta8b0c56f656fcd3fca033dc5f3154a4d8a2464a2 (diff)
downloadprosody-924064a30ad560e9aecc2d5e3b72f6a4a85b40ae.tar.gz
prosody-924064a30ad560e9aecc2d5e3b72f6a4a85b40ae.zip
core.moduleapi: Allow specifying an acceptable range for number options
-rw-r--r--CHANGES1
-rw-r--r--core/features.lua1
-rw-r--r--core/moduleapi.lua16
3 files changed, 16 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index 66d64edd..3f1171e4 100644
--- a/CHANGES
+++ b/CHANGES
@@ -41,6 +41,7 @@ TRUNK
### Module API
- Config interface API can require that string values be picked from a provided set
+- Acceptable interval can be specified for number options
## Changes
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