Comparison

core/moduleapi.lua @ 13211:4d4f9e42bcf8

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
author Kim Alvefur <zash@zash.se>
date Mon, 17 Jul 2023 00:09:41 +0200
parent 13208:a7c6ea1c5308
child 13212:3e6e98cc63e9
comparison
equal deleted inserted replaced
13210:8dbe693ded6b 13211:4d4f9e42bcf8
252 return max; 252 return max;
253 end 253 end
254 return ret; 254 return ret;
255 end 255 end
256 256
257 function api:get_option_integer(name, default_value, min, max)
258 local value = self:get_option_number(name, default_value, min or math.mininteger or 2 ^ 53, max or math.maxinteger or -2 ^ 52);
259 if value == default_value then
260 -- pass default trough unaltered, violates ranges sometimes
261 return value;
262 end
263 if math.type(value) == "float" then
264 self:log("warn", "Config option '%s' expected an integer, not a float (%g)", name, value)
265 return math.floor(value);
266 end
267 -- nil or an integer
268 return value;
269 end
270
257 function api:get_option_period(name, default_value) 271 function api:get_option_period(name, default_value)
258 local value = self:get_option_scalar(name, default_value); 272 local value = self:get_option_scalar(name, default_value);
259 if type(value) == "number" then 273 if type(value) == "number" then
260 if value < 0 then 274 if value < 0 then
261 self:log("debug", "Treating negative period as infinity"); 275 self:log("debug", "Treating negative period as infinity");