25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Merge mod_opt_type from db sub-modules to main module mod_opt_type

This commit is contained in:
Paweł Chmielowski 2016-11-04 12:57:57 +01:00
parent 149cc9654f
commit 62db030942
2 changed files with 61 additions and 11 deletions

View File

@ -38,7 +38,7 @@
transform_options/1, collect_options/1, default_db/2,
convert_to_yaml/1, convert_to_yaml/2, v_db/2,
env_binary_to_list/2, opt_type/1, may_hide_data/1,
is_elixir_enabled/0]).
is_elixir_enabled/0, v_dbs/1, v_dbs_mods/1]).
-export([start/2]).
@ -893,6 +893,19 @@ v_db(Mod, Type) ->
[] -> erlang:error(badarg)
end.
-spec v_dbs(module()) -> [atom()].
v_dbs(Mod) ->
lists:flatten(ets:match(module_db, {Mod, '$1'})).
-spec v_dbs_mods(module()) -> [module()].
v_dbs_mods(Mod) ->
lists:map(fun([M]) ->
binary_to_atom(<<(atom_to_binary(Mod, utf8))/binary, "_",
(atom_to_binary(M, utf8))/binary>>, utf8)
end, ets:match(module_db, {Mod, '$1'})).
-spec default_db(binary(), module()) -> atom().
default_db(Host, Module) ->

View File

@ -308,10 +308,47 @@ get_opt_host(Host, Opts, Default) ->
Val = get_opt(host, Opts, fun iolist_to_binary/1, Default),
ejabberd_regexp:greplace(Val, <<"@HOST@">>, Host).
get_module_mod_opt_type_fun(Module) ->
DBSubMods = ejabberd_config:v_dbs_mods(Module),
fun(Opt) ->
Res = lists:foldl(fun(Mod, {Funs, ArgsList, _} = Acc) ->
case catch Mod:mod_opt_type(Opt) of
Fun when is_function(Fun) ->
{[Fun | Funs], ArgsList, true};
L when is_list(L) ->
{Funs, L ++ ArgsList, true};
_ ->
Acc
end
end, {[], [], false}, [Module | DBSubMods]),
case Res of
{[], [], false} ->
throw({'EXIT', {undef, mod_opt_type}});
{[], Args, _} -> Args;
{Funs, _, _} ->
fun(Val) ->
lists:any(fun(F) ->
try F(Val) of
_ ->
true
catch {replace_with, _NewVal} = E ->
throw(E);
{invalid_syntax, _Error} = E2 ->
throw(E2);
_:_ ->
false
end
end, Funs)
end
end
end.
validate_opts(Module, Opts) ->
ModOptFun = get_module_mod_opt_type_fun(Module),
lists:filtermap(
fun({Opt, Val}) ->
case catch Module:mod_opt_type(Opt) of
case catch ModOptFun(Opt) of
VFun when is_function(VFun) ->
try VFun(Val) of
_ ->