Software / code / prosody
Comparison
core/moduleapi.lua @ 13212:3e6e98cc63e9
core.moduleapi: Add min/max range support to :get_option_period
To match :get_option_number etc, specifying the allowed interval.
Default is essentially (0, inf].
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 17 Jul 2023 00:37:44 +0200 |
| parent | 13211:4d4f9e42bcf8 |
| child | 13215:b1c2e70de470 |
comparison
equal
deleted
inserted
replaced
| 13211:4d4f9e42bcf8 | 13212:3e6e98cc63e9 |
|---|---|
| 266 end | 266 end |
| 267 -- nil or an integer | 267 -- nil or an integer |
| 268 return value; | 268 return value; |
| 269 end | 269 end |
| 270 | 270 |
| 271 function api:get_option_period(name, default_value) | 271 function api:get_option_period(name, default_value, min, max) |
| 272 local value = self:get_option_scalar(name, default_value); | 272 local value = self:get_option_scalar(name, default_value); |
| 273 if type(value) == "number" then | 273 |
| 274 if value < 0 then | 274 local ret; |
| 275 self:log("debug", "Treating negative period as infinity"); | 275 if value == "never" or value == false then |
| 276 return math.huge; | |
| 277 end | |
| 278 -- assume seconds | |
| 279 return value; | |
| 280 elseif value == "never" or value == false then | |
| 281 -- usually for disabling some periodic thing | 276 -- usually for disabling some periodic thing |
| 282 return math.huge; | 277 return math.huge; |
| 278 elseif type(value) == "number" then | |
| 279 -- assume seconds | |
| 280 ret = value; | |
| 283 elseif type(value) == "string" then | 281 elseif type(value) == "string" then |
| 284 local ret = human_io.parse_duration(value); | 282 ret = human_io.parse_duration(value); |
| 285 if value ~= nil and ret == nil then | 283 if value ~= nil and ret == nil then |
| 286 self:log("error", "Config option '%s' not understood, expecting a period (e.g. \"2 days\")", name); | 284 self:log("error", "Config option '%s' not understood, expecting a period (e.g. \"2 days\")", name); |
| 287 end | 285 end |
| 288 return ret; | |
| 289 elseif value ~= nil then | 286 elseif value ~= nil then |
| 290 self:log("error", "Config option '%s' expects a number or a period description string (e.g. \"3 hours\"), not %s", name, type(value)); | 287 self:log("error", "Config option '%s' expects a number or a period description string (e.g. \"3 hours\"), not %s", name, type(value)); |
| 291 end | 288 return nil; |
| 289 else | |
| 290 return nil; | |
| 291 end | |
| 292 | |
| 293 if ret < 0 then | |
| 294 self:log("debug", "Treating negative period as infinity"); | |
| 295 return math.huge; | |
| 296 end | |
| 297 | |
| 298 if min and ret < min then | |
| 299 self:log("warn", "Config option '%s' out of bounds %g < %g", name, ret, min); | |
| 300 return min; | |
| 301 end | |
| 302 if max and ret > max then | |
| 303 self:log("warn", "Config option '%s' out of bounds %g > %g", name, ret, max); | |
| 304 return max; | |
| 305 end | |
| 306 | |
| 307 return ret; | |
| 292 end | 308 end |
| 293 | 309 |
| 294 function api:get_option_boolean(name, ...) | 310 function api:get_option_boolean(name, ...) |
| 295 local value = self:get_option_scalar(name, ...); | 311 local value = self:get_option_scalar(name, ...); |
| 296 if value == nil then | 312 if value == nil then |