aboutsummaryrefslogtreecommitdiffstats
path: root/lualib
diff options
context:
space:
mode:
authorSimon Hauser <simon.hauser@helsinki-systems.de>2023-08-04 16:12:01 +0200
committerSimon Hauser <simon.hauser@helsinki-systems.de>2023-08-04 16:30:05 +0200
commitc04808c938e2a3deb0e9b43e3ae0af915d1d561d (patch)
tree8de52e7eb387fb9dbc00733b6b16616d92640cc3 /lualib
parent04c2886544ca562a0b9154974ecc491d14e0fba6 (diff)
downloadrspamd-c04808c938e2a3deb0e9b43e3ae0af915d1d561d.tar.gz
rspamd-c04808c938e2a3deb0e9b43e3ae0af915d1d561d.zip
fix: redis schema parsing
Diffstat (limited to 'lualib')
-rw-r--r--lualib/lua_redis.lua60
-rw-r--r--lualib/lua_util.lua13
2 files changed, 47 insertions, 26 deletions
diff --git a/lualib/lua_redis.lua b/lualib/lua_redis.lua
index 2a43a915c..47da29899 100644
--- a/lualib/lua_redis.lua
+++ b/lualib/lua_redis.lua
@@ -39,25 +39,39 @@ local common_schema = ts.shape {
sentinel_password = ts.string:is_optional(),
}
-local config_schema = -- Allow separate read/write servers to allow usage in the `extra_fields`
-ts.shape({
+local read_schema = lutil.table_merge({
read_servers = ts.string + ts.array_of(ts.string),
-}, { extra_fields = common_schema }) +
- ts.shape({
- write_servers = ts.string + ts.array_of(ts.string),
- }, { extra_fields = common_schema }) +
- ts.shape({
- read_servers = ts.string + ts.array_of(ts.string),
- write_servers = ts.string + ts.array_of(ts.string),
- }, { extra_fields = common_schema }) +
- ts.shape({
- servers = ts.string + ts.array_of(ts.string),
- }, { extra_fields = common_schema }) +
- ts.shape({
- server = ts.string + ts.array_of(ts.string),
- }, { extra_fields = common_schema })
-
-exports.config_schema = config_schema
+}, common_schema)
+
+local write_schema = lutil.table_merge({
+ write_servers = ts.string + ts.array_of(ts.string),
+}, common_schema)
+
+local rw_schema = lutil.table_merge({
+ read_servers = ts.string + ts.array_of(ts.string),
+ write_servers = ts.string + ts.array_of(ts.string),
+}, common_schema)
+
+local servers_schema = lutil.table_merge({
+ servers = ts.string + ts.array_of(ts.string),
+}, common_schema)
+
+local server_schema = lutil.table_merge({
+ server = ts.string + ts.array_of(ts.string),
+}, common_schema)
+
+local generate_schema = function(external)
+ return ts.one_of {
+ ts.shape(external),
+ ts.shape(lutil.table_merge(read_schema, external)),
+ ts.shape(lutil.table_merge(write_schema, external)),
+ ts.shape(lutil.table_merge(rw_schema, external)),
+ ts.shape(lutil.table_merge(servers_schema, external)),
+ ts.shape(lutil.table_merge(server_schema, external)),
+ }
+end
+
+exports.generate_schema = generate_schema
local function redis_query_sentinel(ev_base, params, initialised)
local function flatten_redis_table(tbl)
@@ -1122,20 +1136,14 @@ local function script_set_loaded(script)
end
local function prepare_redis_call(script)
- local function merge_tables(t1, t2)
- for k, v in pairs(t2) do
- t1[k] = v
- end
- end
-
local servers = {}
local options = {}
if script.redis_params.read_servers then
- merge_tables(servers, script.redis_params.read_servers:all_upstreams())
+ lutil.table_merge(servers, script.redis_params.read_servers:all_upstreams())
end
if script.redis_params.write_servers then
- merge_tables(servers, script.redis_params.write_servers:all_upstreams())
+ lutil.table_merge(servers, script.redis_params.write_servers:all_upstreams())
end
-- Call load script on each server, set loaded flag
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index c2bdf40a9..289e2ed3a 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -619,6 +619,19 @@ end
exports.table_cmp = table_cmp
--[[[
+-- @function lua_util.table_merge(t1, t2)
+-- Merge two tables
+--]]
+local function table_merge(t1, t2)
+ for k, v in pairs(t2) do
+ t1[k] = v
+ end
+ return t1
+end
+
+exports.table_merge = table_merge
+
+--[[[
-- @function lua_util.table_cmp(task, name, value, stop_chars)
-- Performs header folding
--]]