aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2023-07-17 00:09:41 +0200
committerKim Alvefur <zash@zash.se>2023-07-17 00:09:41 +0200
commit210f60808657a25913b0ac559ab8a91c93aeb218 (patch)
treee798dae284df7346bf06da02cd6dae89bcb997f7
parentc222b08005a9a7e8e45bef8b074fd3ab12cd8a5f (diff)
downloadprosody-210f60808657a25913b0ac559ab8a91c93aeb218.tar.gz
prosody-210f60808657a25913b0ac559ab8a91c93aeb218.zip
moduleapi: Add :get_option_integer()
Many options in Prosody that are treated as numbers don't make sense as floats, e.g. sizes and limits measured in bytes. Simplified implementation based on an earlier attempt dating back to 2020
-rw-r--r--.luacheckrc1
-rw-r--r--CHANGES1
-rw-r--r--core/features.lua1
-rw-r--r--core/moduleapi.lua14
4 files changed, 17 insertions, 0 deletions
diff --git a/.luacheckrc b/.luacheckrc
index 9d89fbe6..7aaade08 100644
--- a/.luacheckrc
+++ b/.luacheckrc
@@ -75,6 +75,7 @@ files["plugins/"] = {
"module.get_option_boolean",
"module.get_option_enum",
"module.get_option_inherited_set",
+ "module.get_option_integer",
"module.get_option_number",
"module.get_option_path",
"module.get_option_period",
diff --git a/CHANGES b/CHANGES
index 60b11127..68900215 100644
--- a/CHANGES
+++ b/CHANGES
@@ -43,6 +43,7 @@ TRUNK
- Config interface API can require that string values be picked from a provided set
- Acceptable interval can be specified for number options
- Method for parsing time periods / intervals from config
+- Method for retrieving integer settings from config
## Changes
diff --git a/core/features.lua b/core/features.lua
index 49c34111..db1bc986 100644
--- a/core/features.lua
+++ b/core/features.lua
@@ -20,5 +20,6 @@ return {
"getopt-enum";
"getopt-interval";
"getopt-period";
+ "getopt-integer";
};
};
diff --git a/core/moduleapi.lua b/core/moduleapi.lua
index 53e74a84..7c9b357f 100644
--- a/core/moduleapi.lua
+++ b/core/moduleapi.lua
@@ -254,6 +254,20 @@ function api:get_option_number(name, default_value, min, max)
return ret;
end
+function api:get_option_integer(name, default_value, min, max)
+ local value = self:get_option_number(name, default_value, min or math.mininteger or 2 ^ 53, max or math.maxinteger or -2 ^ 52);
+ if value == default_value then
+ -- pass default trough unaltered, violates ranges sometimes
+ return value;
+ end
+ if math.type(value) == "float" then
+ self:log("warn", "Config option '%s' expected an integer, not a float (%g)", name, value)
+ return math.floor(value);
+ end
+ -- nil or an integer
+ return value;
+end
+
function api:get_option_period(name, default_value)
local value = self:get_option_scalar(name, default_value);
if type(value) == "number" then