Software / code / prosody
Diff
core/moduleapi.lua @ 13203:aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 05 Oct 2021 15:36:38 +0200 |
| parent | 13201:65fb0d7a2312 |
| child | 13204:c9ef35fab0b1 |
line wrap: on
line diff
--- a/core/moduleapi.lua Sat Jan 16 21:04:58 2021 +0100 +++ b/core/moduleapi.lua Tue Oct 05 15:36:38 2021 +0200 @@ -232,12 +232,24 @@ 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