Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unfold global options without the temporary functions #3406

Merged
merged 4 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/config/mongoose_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

-type key() :: atom() | host_type_key() | host_type_or_global_key().
-type s2s_domain_key() :: {atom(), jid:lserver()}.
-type host_type_key() :: {atom() | s2s_domain_key(), mongooseim:host_type()}.
-type host_type_or_global_key() :: {shaper | access | acl, atom(), mongooseim:host_type() | global}.
-type host_type_key() :: {atom() | s2s_domain_key(), mongooseim:host_type_or_global()}.
Premwoik marked this conversation as resolved.
Show resolved Hide resolved
-type host_type_or_global_key() :: {shaper | access | acl, atom(), mongooseim:host_type_or_global()}.

-type value() :: atom()
| binary()
Expand Down
36 changes: 19 additions & 17 deletions src/config/mongoose_config_parser_toml.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
extract_errors/1]).
-endif.

-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").
-include("ejabberd_config.hrl").

%% Used to create per-host config when the list of hosts is not known yet
-define(HOST_F(Expr), [fun(Host) -> Expr end]).

%% Input: TOML parsed by tomerl
-type toml_key() :: binary().
-type toml_value() :: tomerl:value().
Expand All @@ -28,7 +24,6 @@
-type top_level_config() :: #local_config{}.
-type config_error() :: #{class := error, what := atom(), text := string(), any() => any()}.
-type config() :: top_level_config() | config_error().
-type config_list() :: [config() | fun((jid:server()) -> [config()])]. % see HOST_F

-type list_processor() :: fun((path(), [config_part()]) -> config_part())
| fun(([config_part()]) -> config_part()).
Expand Down Expand Up @@ -61,21 +56,31 @@ process(Content) ->
Config = parse(Content),
Hosts = get_value(Config, hosts),
HostTypes = get_value(Config, host_types),
{FOpts, Opts} = lists:partition(fun(Opt) -> is_function(Opt, 1) end, Config),
HostsOpts = lists:flatmap(fun(F) -> lists:flatmap(F, Hosts) end, FOpts),
HostTypesOpts = lists:flatmap(fun(F) -> lists:flatmap(F, HostTypes) end, FOpts),
AllOpts = Opts ++ HostsOpts ++ HostTypesOpts,
case extract_errors(AllOpts) of
Opts = unfold_globals(Config, Hosts ++ HostTypes),
case extract_errors(Opts) of
[] ->
build_state(Hosts, HostTypes, AllOpts);
build_state(Hosts, HostTypes, Opts);
Errors ->
error(config_error(Errors))
end.

%% @doc Repeat global options for each host type for simpler lookup
%% Put them at the end so host_config can override them
%% Options with tags (shaper, acl, access) are left as globals as they can be set on both levels
-spec unfold_globals([config()], [mongooseim:host_type()]) -> [config()].
unfold_globals(Config, AllHostTypes) ->
{GlobalOpts, Opts} = lists:partition(fun is_global_to_unfold/1, Config),
Opts ++ [Opt#local_config{key = {Key, HostType}} ||
Opt = #local_config{key = {Key, global}} <- GlobalOpts,
HostType <- AllHostTypes].

is_global_to_unfold(#local_config{key = {_Key, global}}) -> true;
is_global_to_unfold(_) -> false.

config_error(Errors) ->
{config_error, "Could not read the TOML configuration file", Errors}.

-spec parse(toml_section()) -> config_list().
-spec parse(toml_section()) -> [config()].
parse(Content) ->
handle([], Content, mongoose_config_spec:root()).

Expand Down Expand Up @@ -207,10 +212,7 @@ format([Key|_] = Path, V, host_local_config) ->
format([Key|_] = Path, V, local_config) ->
format(Path, V, {local_config, b2a(Key)});
format(Path, V, {host_local_config, Key}) ->
case get_host(Path) of
global -> ?HOST_F([#local_config{key = {Key, Host}, value = V}]);
Host -> [#local_config{key = {Key, Host}, value = V}]
end;
[#local_config{key = {Key, get_host(Path)}, value = V}];
format(Path, V, {local_config, Key}) ->
global = get_host(Path),
[#local_config{key = Key, value = V}];
Expand Down Expand Up @@ -281,7 +283,7 @@ item_key(_, _) -> item.

%% Processing of the parsed options

-spec get_value(config_list(), mongoose_config:key()) -> [mongoose_config:value()].
-spec get_value([config()], mongoose_config:key()) -> [mongoose_config:value()].
get_value(Config, Key) ->
FilterFn = fun(#local_config{key = K}) when K =:= Key -> true;
(_) -> false
Expand Down
Loading