moduleapi: Add enum config option method
authorKim Alvefur <zash@zash.se>
Sat, 16 Jan 2021 20:40:14 +0100
changeset 13205 65fb0d7a2312
parent 13204 150e3bbec1bd
child 13206 173038306750
moduleapi: Add enum config option method For when a setting has a few fixed values it can take
.luacheckrc
CHANGES
core/features.lua
core/moduleapi.lua
--- a/.luacheckrc	Sun Jul 16 20:29:06 2023 +0200
+++ b/.luacheckrc	Sat Jan 16 20:40:14 2021 +0100
@@ -73,6 +73,7 @@
 		"module.get_option",
 		"module.get_option_array",
 		"module.get_option_boolean",
+		"module.get_option_enum",
 		"module.get_option_inherited_set",
 		"module.get_option_number",
 		"module.get_option_path",
--- a/CHANGES	Sun Jul 16 20:29:06 2023 +0200
+++ b/CHANGES	Sat Jan 16 20:40:14 2021 +0100
@@ -38,6 +38,10 @@
 - Performance improvements in internal archive stores
 - Ability to use SQLite3 storage using LuaSQLite3 instead of LuaDBI
 
+### Module API
+
+- Config interface API can require that string values be picked from a provided set
+
 ## Changes
 
 - Support sub-second precision timestamps
--- a/core/features.lua	Sun Jul 16 20:29:06 2023 +0200
+++ b/core/features.lua	Sat Jan 16 20:40:14 2021 +0100
@@ -15,5 +15,8 @@
 
 		-- prosody:guest, prosody:registered, prosody:member
 		"split-user-roles";
+
+		-- new moduleapi methods
+		"getopt-enum";
 	};
 };
--- a/core/moduleapi.lua	Sun Jul 16 20:29:06 2023 +0200
+++ b/core/moduleapi.lua	Sat Jan 16 20:40:14 2021 +0100
@@ -310,6 +310,15 @@
 	return resolve_relative_path(parent, value);
 end
 
+function api:get_option_enum(name, default, ...)
+	local value = self:get_option_scalar(name, default);
+	if value == nil then return nil; end
+	local options = set.new{default, ...};
+	if not options:contains(value) then
+		self:log("error", "Config option '%s' not in set of allowed values (one of: %s)", name, options);
+	end
+	return value;
+end
 
 function api:context(host)
 	return setmetatable({ host = host or "*", global = "*" == host }, { __index = self, __newindex = self });