# HG changeset patch # User Kim Alvefur # Date 1689545381 -7200 # Node ID 4d4f9e42bcf8758c1d480a599eaa85625a77c1de # Parent 8dbe693ded6b898cb63c570ad08ae52aa9ebccb7 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 diff -r 8dbe693ded6b -r 4d4f9e42bcf8 .luacheckrc --- a/.luacheckrc Sun Jul 16 21:21:37 2023 +0200 +++ b/.luacheckrc Mon Jul 17 00:09:41 2023 +0200 @@ -75,6 +75,7 @@ "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 -r 8dbe693ded6b -r 4d4f9e42bcf8 CHANGES --- a/CHANGES Sun Jul 16 21:21:37 2023 +0200 +++ b/CHANGES Mon Jul 17 00:09:41 2023 +0200 @@ -43,6 +43,7 @@ - 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 -r 8dbe693ded6b -r 4d4f9e42bcf8 core/features.lua --- a/core/features.lua Sun Jul 16 21:21:37 2023 +0200 +++ b/core/features.lua Mon Jul 17 00:09:41 2023 +0200 @@ -20,5 +20,6 @@ "getopt-enum"; "getopt-interval"; "getopt-period"; + "getopt-integer"; }; }; diff -r 8dbe693ded6b -r 4d4f9e42bcf8 core/moduleapi.lua --- a/core/moduleapi.lua Sun Jul 16 21:21:37 2023 +0200 +++ b/core/moduleapi.lua Mon Jul 17 00:09:41 2023 +0200 @@ -254,6 +254,20 @@ 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