25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +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 %% API
-export([parse/3, validate/2, fail/1, format_error/2, replace_macros/1]). -export([parse/3, validate/2, fail/1, format_error/2, replace_macros/1]).
-export([group_dups/1]).
%% Simple types %% Simple types
-export([pos_int/0, pos_int/1, non_neg_int/0, non_neg_int/1]). -export([pos_int/0, pos_int/1, non_neg_int/0, non_neg_int/1]).
-export([int/0, int/2, number/1, octal/0]). -export([int/0, int/2, number/1, octal/0]).
@ -227,6 +228,18 @@ format_join([H|_] = L) when is_atom(H) ->
format_join(L) -> format_join(L) ->
str:join(lists:sort(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 %%% Validators from yconf
%%%=================================================================== %%%===================================================================

View File

@ -550,19 +550,15 @@ validate(Y1) ->
-spec pre_validate(term()) -> {ok, [{atom(), term()}]} | error_return(). -spec pre_validate(term()) -> {ok, [{atom(), term()}]} | error_return().
pre_validate(Y1) -> pre_validate(Y1) ->
case econf:validate( econf:validate(
econf:options( econf:and_then(
#{hosts => ejabberd_options:opt_type(hosts), econf:options(
loglevel => ejabberd_options:opt_type(loglevel), #{hosts => ejabberd_options:opt_type(hosts),
version => ejabberd_options:opt_type(version), loglevel => ejabberd_options:opt_type(loglevel),
'_' => econf:any()}, version => ejabberd_options:opt_type(version),
[{required, [hosts]}]), '_' => econf:any()},
Y1) of [{required, [hosts]}]),
{ok, Y2} -> fun econf:group_dups/1), Y1).
{ok, group_duplicated_options(Y2)};
Err ->
Err
end.
-spec load_file(binary()) -> ok | error_return(). -spec load_file(binary()) -> ok | error_return().
load_file(File) -> load_file(File) ->
@ -768,15 +764,3 @@ set_node_start(UnixTime) ->
-spec set_loglevel(logger:level()) -> ok. -spec set_loglevel(logger:level()) -> ok.
set_loglevel(Level) -> set_loglevel(Level) ->
ejabberd_logger:set(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) -> opt_type(api_permissions) ->
ejabberd_access_permissions:validator(); ejabberd_access_permissions:validator();
opt_type(append_host_config) -> opt_type(append_host_config) ->
econf:map( opt_type(host_config);
econf:and_then(
econf:domain(),
econf:enum(ejabberd_config:get_option(hosts))),
validator(),
[unique]);
opt_type(auth_cache_life_time) -> opt_type(auth_cache_life_time) ->
econf:timeout(second, infinity); econf:timeout(second, infinity);
opt_type(auth_cache_missed) -> opt_type(auth_cache_missed) ->
@ -150,12 +145,14 @@ opt_type(fqdn) ->
opt_type(hide_sensitive_log_data) -> opt_type(hide_sensitive_log_data) ->
econf:bool(); econf:bool();
opt_type(host_config) -> opt_type(host_config) ->
econf:map( econf:and_then(
econf:and_then( econf:and_then(
econf:domain(), econf:map(econf:domain(), econf:list(econf:any())),
econf:enum(ejabberd_config:get_option(hosts))), fun econf:group_dups/1),
validator(), econf:map(
[unique]); econf:enum(ejabberd_config:get_option(hosts)),
validator(),
[unique]));
opt_type(hosts) -> opt_type(hosts) ->
econf:non_empty(econf:list(econf:domain(), [unique])); econf:non_empty(econf:list(econf:domain(), [unique]));
opt_type(include_config_file) -> opt_type(include_config_file) ->
@ -737,9 +734,11 @@ globals() ->
validator() -> validator() ->
Disallowed = ejabberd_config:globals(), Disallowed = ejabberd_config:globals(),
{Validators, Required} = ejabberd_config:validators(Disallowed), {Validators, Required} = ejabberd_config:validators(Disallowed),
econf:options( econf:and_then(
Validators, fun econf:group_dups/1,
[{disallowed, Required ++ Disallowed}, unique]). econf:options(
Validators,
[{disallowed, Required ++ Disallowed}, unique])).
-spec fqdn(global | binary()) -> [binary()]. -spec fqdn(global | binary()) -> [binary()].
fqdn(global) -> fqdn(global) ->