25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-12-24 17:29:28 +01:00

Also group duplicated list-like options inside host_config/append_host_config

This commit is contained in:
Evgeny Khramtsov 2019-11-07 12:14:08 +03:00
parent b7b76cc38b
commit 3826a9ed58
3 changed files with 35 additions and 39 deletions

View File

@ -24,6 +24,7 @@
%% API
-export([parse/3, validate/2, fail/1, format_error/2, replace_macros/1]).
-export([group_dups/1]).
%% Simple types
-export([pos_int/0, pos_int/1, non_neg_int/0, non_neg_int/1]).
-export([int/0, int/2, number/1, octal/0]).
@ -227,6 +228,18 @@ format_join([H|_] = L) when is_atom(H) ->
format_join(L) ->
str:join(lists:sort(L), <<", ">>).
%% All duplicated options having list-values are grouped
%% into a single option with all list-values being concatenated
-spec group_dups(list(T)) -> list(T).
group_dups(Y1) ->
{Y2, D} = lists:mapfoldl(
fun({Option, Values}, Acc) when is_list(Values) ->
{[], dict:append_list(Option, Values, Acc)};
(Other, Acc) ->
{[Other], Acc}
end, dict:new(), Y1),
lists:append(Y2) ++ dict:to_list(D).
%%%===================================================================
%%% Validators from yconf
%%%===================================================================

View File

@ -550,19 +550,15 @@ validate(Y1) ->
-spec pre_validate(term()) -> {ok, [{atom(), term()}]} | error_return().
pre_validate(Y1) ->
case econf:validate(
econf:options(
#{hosts => ejabberd_options:opt_type(hosts),
loglevel => ejabberd_options:opt_type(loglevel),
version => ejabberd_options:opt_type(version),
'_' => econf:any()},
[{required, [hosts]}]),
Y1) of
{ok, Y2} ->
{ok, group_duplicated_options(Y2)};
Err ->
Err
end.
econf:validate(
econf:and_then(
econf:options(
#{hosts => ejabberd_options:opt_type(hosts),
loglevel => ejabberd_options:opt_type(loglevel),
version => ejabberd_options:opt_type(version),
'_' => econf:any()},
[{required, [hosts]}]),
fun econf:group_dups/1), Y1).
-spec load_file(binary()) -> ok | error_return().
load_file(File) ->
@ -768,15 +764,3 @@ set_node_start(UnixTime) ->
-spec set_loglevel(logger:level()) -> ok.
set_loglevel(Level) ->
ejabberd_logger:set(Level).
%% All duplicated options having list-values are grouped
%% into a single option with all list-values being concatenated
-spec group_duplicated_options([{atom(), term()}]) -> [{atom(), term()}].
group_duplicated_options(Y1) ->
{Y2, D} = lists:mapfoldl(
fun({Option, Values}, Acc) when is_list(Values) ->
{[], dict:append_list(Option, Values, Acc)};
({Option, Value}, Acc) ->
{{Option, Value}, Acc}
end, dict:new(), Y1),
lists:flatten(Y2) ++ dict:to_list(D).

View File

@ -53,12 +53,7 @@ opt_type(anonymous_protocol) ->
opt_type(api_permissions) ->
ejabberd_access_permissions:validator();
opt_type(append_host_config) ->
econf:map(
econf:and_then(
econf:domain(),
econf:enum(ejabberd_config:get_option(hosts))),
validator(),
[unique]);
opt_type(host_config);
opt_type(auth_cache_life_time) ->
econf:timeout(second, infinity);
opt_type(auth_cache_missed) ->
@ -150,12 +145,14 @@ opt_type(fqdn) ->
opt_type(hide_sensitive_log_data) ->
econf:bool();
opt_type(host_config) ->
econf:map(
econf:and_then(
econf:and_then(
econf:domain(),
econf:enum(ejabberd_config:get_option(hosts))),
validator(),
[unique]);
econf:map(econf:domain(), econf:list(econf:any())),
fun econf:group_dups/1),
econf:map(
econf:enum(ejabberd_config:get_option(hosts)),
validator(),
[unique]));
opt_type(hosts) ->
econf:non_empty(econf:list(econf:domain(), [unique]));
opt_type(include_config_file) ->
@ -737,9 +734,11 @@ globals() ->
validator() ->
Disallowed = ejabberd_config:globals(),
{Validators, Required} = ejabberd_config:validators(Disallowed),
econf:options(
Validators,
[{disallowed, Required ++ Disallowed}, unique]).
econf:and_then(
fun econf:group_dups/1,
econf:options(
Validators,
[{disallowed, Required ++ Disallowed}, unique])).
-spec fqdn(global | binary()) -> [binary()].
fqdn(global) ->