From c4abd68e92a258750177f25dca38525fe3133063 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 16 Jan 2021 20:40:14 +0100 Subject: moduleapi: Add enum config option method For when a setting has a few fixed values it can take --- .luacheckrc | 1 + CHANGES | 4 ++++ core/features.lua | 3 +++ core/moduleapi.lua | 9 +++++++++ 4 files changed, 17 insertions(+) diff --git a/.luacheckrc b/.luacheckrc index 9e0d1fdf..473015d8 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -73,6 +73,7 @@ files["plugins/"] = { "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", diff --git a/CHANGES b/CHANGES index b3eefc1d..66d64edd 100644 --- a/CHANGES +++ b/CHANGES @@ -38,6 +38,10 @@ TRUNK - 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 diff --git a/core/features.lua b/core/features.lua index c96f0c81..2948a1a7 100644 --- a/core/features.lua +++ b/core/features.lua @@ -15,5 +15,8 @@ return { -- prosody:guest, prosody:registered, prosody:member "split-user-roles"; + + -- new moduleapi methods + "getopt-enum"; }; }; diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 3ee8647f..68818b8d 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -310,6 +310,15 @@ function api:get_option_path(name, default, parent) 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 }); -- cgit v1.2.3