core.moduleapi: Improve handling of different types in :get_option_period
authorKim Alvefur <zash@zash.se>
Sun, 16 Jul 2023 20:59:27 +0200
changeset 13209 0ccd82b965d5
parent 13208 c9ef35fab0b1
child 13210 7435a9341bb3
core.moduleapi: Improve handling of different types in :get_option_period Pass positive numbers trough unharmed, parse strings as periods, discard anything else.
core/moduleapi.lua
--- a/core/moduleapi.lua	Sun Jul 16 19:49:12 2023 +0200
+++ b/core/moduleapi.lua	Sun Jul 16 20:59:27 2023 +0200
@@ -256,16 +256,16 @@
 
 function api:get_option_period(name, default_value)
 	local value = self:get_option_scalar(name, default_value);
-	local num = tonumber(value);
-	if num then
+	if type(value) == "number" then
 		-- assume seconds
-		return num;
+		return value;
+	elseif type(value) == "string" then
+		local ret = human_io.parse_duration(value);
+		if value ~= nil and ret == nil then
+			self:log("error", "Config option '%s' not understood, expecting a period (e.g. \"2 days\")", name);
+		end
+		return ret;
 	end
-	local ret = human_io.parse_duration(value);
-	if value ~= nil and ret == nil then
-		self:log("error", "Config option '%s' not understood, expecting a period", name);
-	end
-	return ret;
 end
 
 function api:get_option_boolean(name, ...)