25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Use named functions instead of carrying self as argument

This commit is contained in:
Paweł Chmielowski 2018-01-29 10:02:20 +01:00
parent c47366ba97
commit 719dfe12f6

View File

@ -29,7 +29,7 @@ Vars = case file:consult(filename:join([filename:dirname(SCRIPT),"vars.config"])
{ldflags, LDFlags} = lists:keyfind(ldflags, 1, Vars), {ldflags, LDFlags} = lists:keyfind(ldflags, 1, Vars),
{system_deps, SystemDeps} = lists:keyfind(system_deps, 1, Vars), {system_deps, SystemDeps} = lists:keyfind(system_deps, 1, Vars),
GetCfg0 = fun(F, Cfg, [Key | Tail], Default) -> GetCfg = fun GetCfg(Cfg, [Key | Tail], Default) ->
Val = case lists:keyfind(Key, 1, Cfg) of Val = case lists:keyfind(Key, 1, Cfg) of
{Key, V1} -> V1; {Key, V1} -> V1;
false -> Default false -> Default
@ -38,10 +38,10 @@ GetCfg0 = fun(F, Cfg, [Key | Tail], Default) ->
[] -> [] ->
Val; Val;
_ -> _ ->
F(F, Val, Tail, Default) GetCfg(Val, Tail, Default)
end end
end, end,
ModCfg0 = fun(F, Cfg, [Key | Tail], Op, Default) -> ModCfg = fun ModCfg(Cfg, [Key | Tail], Op, Default) ->
{OldVal, PartCfg} = case lists:keytake(Key, 1, Cfg) of {OldVal, PartCfg} = case lists:keytake(Key, 1, Cfg) of
{value, {_, V1}, V2} -> {V1, V2}; {value, {_, V1}, V2} -> {V1, V2};
false -> {if Tail == [] -> Default; true -> [] end, Cfg} false -> {if Tail == [] -> Default; true -> [] end, Cfg}
@ -50,19 +50,19 @@ ModCfg0 = fun(F, Cfg, [Key | Tail], Op, Default) ->
[] -> [] ->
[{Key, Op(OldVal)} | PartCfg]; [{Key, Op(OldVal)} | PartCfg];
_ -> _ ->
[{Key, F(F, OldVal, Tail, Op, Default)} | PartCfg] [{Key, ModCfg(OldVal, Tail, Op, Default)} | PartCfg]
end end
end, end,
FilterConfig = fun(F, Cfg, [{Path, true, ModFun, Default} | Tail]) -> FilterConfig = fun FilterConfig(Cfg, [{Path, true, ModFun, Default} | Tail]) ->
F(F, ModCfg0(ModCfg0, Cfg, Path, ModFun, Default), Tail); FilterConfig(ModCfg(Cfg, Path, ModFun, Default), Tail);
(F, Cfg, [{Path, SourcePath, true, ModFun, Default, SourceDefault} | Tail]) -> FilterConfig(Cfg, [{Path, SourcePath, true, ModFun, Default, SourceDefault} | Tail]) ->
SourceVal = GetCfg0(GetCfg0, Cfg, SourcePath, SourceDefault), SourceVal = GetCfg(Cfg, SourcePath, SourceDefault),
ModFun2 = fun(V) -> ModFun(V, SourceVal) end, ModFun2 = fun(V) -> ModFun(V, SourceVal) end,
F(F, ModCfg0(ModCfg0, Cfg, Path, ModFun2, Default), Tail); FilterConfig(ModCfg(Cfg, Path, ModFun2, Default), Tail);
(F, Cfg, [_ | Tail]) -> FilterConfig(Cfg, [_ | Tail]) ->
F(F, Cfg, Tail); FilterConfig(Cfg, Tail);
(_, Cfg, []) -> FilterConfig(Cfg, []) ->
Cfg Cfg
end, end,
@ -78,15 +78,15 @@ IsRebar3 = case application:get_key(rebar, vsn) of
SysVer = erlang:system_info(otp_release), SysVer = erlang:system_info(otp_release),
ProcessSingleVar = fun(F, Var, Tail) -> ProcessSingleVar = fun(F, Var, Tail) ->
case F(F, [Var], []) of case F([Var], []) of
[] -> Tail; [] -> Tail;
[Val] -> [Val | Tail] [Val] -> [Val | Tail]
end end
end, end,
ProcessVars = fun(_F, [], Acc) -> ProcessVars = fun F([], Acc) ->
lists:reverse(Acc); lists:reverse(Acc);
(F, [{Type, Ver, Value} | Tail], Acc) when F([{Type, Ver, Value} | Tail], Acc) when
Type == if_version_above orelse Type == if_version_above orelse
Type == if_version_below -> Type == if_version_below ->
SysVer = erlang:system_info(otp_release), SysVer = erlang:system_info(otp_release),
@ -96,11 +96,11 @@ ProcessVars = fun(_F, [], Acc) ->
SysVer < Ver SysVer < Ver
end, end,
if Include -> if Include ->
F(F, Tail, ProcessSingleVar(F, Value, Acc)); F(Tail, ProcessSingleVar(F, Value, Acc));
true -> true ->
F(F, Tail, Acc) F(Tail, Acc)
end; end;
(F, [{Type, Ver, Value, ElseValue} | Tail], Acc) when F([{Type, Ver, Value, ElseValue} | Tail], Acc) when
Type == if_version_above orelse Type == if_version_above orelse
Type == if_version_below -> Type == if_version_below ->
Include = if Type == if_version_above -> Include = if Type == if_version_above ->
@ -109,53 +109,53 @@ ProcessVars = fun(_F, [], Acc) ->
SysVer < Ver SysVer < Ver
end, end,
if Include -> if Include ->
F(F, Tail, ProcessSingleVar(F, Value, Acc)); F(Tail, ProcessSingleVar(F, Value, Acc));
true -> true ->
F(F, Tail, ProcessSingleVar(F, ElseValue, Acc)) F(Tail, ProcessSingleVar(F, ElseValue, Acc))
end; end;
(F, [{Type, Var, Value} | Tail], Acc) when F([{Type, Var, Value} | Tail], Acc) when
Type == if_var_true orelse Type == if_var_true orelse
Type == if_var_false -> Type == if_var_false ->
Flag = Type == if_var_true, Flag = Type == if_var_true,
case proplists:get_bool(Var, Vars) of case proplists:get_bool(Var, Vars) of
V when V == Flag -> V when V == Flag ->
F(F, Tail, ProcessSingleVar(F, Value, Acc)); F(Tail, ProcessSingleVar(F, Value, Acc));
_ -> _ ->
F(F, Tail, Acc) F(Tail, Acc)
end; end;
(F, [{Type, Value} | Tail], Acc) when F([{Type, Value} | Tail], Acc) when
Type == if_rebar3 orelse Type == if_rebar3 orelse
Type == if_not_rebar3 -> Type == if_not_rebar3 ->
Flag = Type == if_rebar3, Flag = Type == if_rebar3,
case IsRebar3 == Flag of case IsRebar3 == Flag of
true -> true ->
F(F, Tail, ProcessSingleVar(F, Value, Acc)); F(Tail, ProcessSingleVar(F, Value, Acc));
_ -> _ ->
F(F, Tail, Acc) F(Tail, Acc)
end; end;
(F, [{Type, Var, Match, Value} | Tail], Acc) when F([{Type, Var, Match, Value} | Tail], Acc) when
Type == if_var_match orelse Type == if_var_match orelse
Type == if_var_no_match -> Type == if_var_no_match ->
case proplists:get_value(Var, Vars) of case proplists:get_value(Var, Vars) of
V when V == Match -> V when V == Match ->
F(F, Tail, ProcessSingleVar(F, Value, Acc)); F(Tail, ProcessSingleVar(F, Value, Acc));
_ -> _ ->
F(F, Tail, Acc) F(Tail, Acc)
end; end;
(F, [{if_have_fun, MFA, Value} | Tail], Acc) -> F([{if_have_fun, MFA, Value} | Tail], Acc) ->
{Mod, Fun, Arity} = MFA, {Mod, Fun, Arity} = MFA,
code:ensure_loaded(Mod), code:ensure_loaded(Mod),
case erlang:function_exported(Mod, Fun, Arity) of case erlang:function_exported(Mod, Fun, Arity) of
true -> true ->
F(F, Tail, ProcessSingleVar(F, Value, Acc)); F(Tail, ProcessSingleVar(F, Value, Acc));
false -> false ->
F(F, Tail, Acc) F(Tail, Acc)
end; end;
(F, [Other1 | Tail1], Acc) -> F([Other1 | Tail1], Acc) ->
F(F, Tail1, [F(F, Other1, []) | Acc]); F(Tail1, [F(Other1, []) | Acc]);
(F, Val, Acc) when is_tuple(Val) -> F(Val, Acc) when is_tuple(Val) ->
list_to_tuple(F(F, tuple_to_list(Val), Acc)); list_to_tuple(F(tuple_to_list(Val), Acc));
(_F, Other2, _Acc) -> F(Other2, _Acc) ->
Other2 Other2
end, end,
@ -210,18 +210,18 @@ DepAlts = fun("esip") -> ["esip", "p1_sip"];
(Val) -> [Val] (Val) -> [Val]
end, end,
LibDirInt = fun([Dep|Rest], Suffix, F) -> LibDirInt = fun F([Dep|Rest], Suffix) ->
case code:lib_dir(Dep) of case code:lib_dir(Dep) of
{error, _} -> {error, _} ->
F(Rest, Suffix, F); F(Rest, Suffix);
V -> V ++ Suffix V -> V ++ Suffix
end; end;
([], _, _) -> F([], _) ->
error error
end, end,
LibDir = fun(Name, Suffix) -> LibDir = fun(Name, Suffix) ->
LibDirInt(DepAlts(Name), Suffix, LibDirInt) LibDirInt(DepAlts(Name), Suffix)
end, end,
GlobalDepsFilter = GlobalDepsFilter =
@ -372,7 +372,7 @@ Rules = [
], ],
Config = [{plugin_dir, filename:join([filename:dirname(SCRIPT),"plugins"])}]++ Config = [{plugin_dir, filename:join([filename:dirname(SCRIPT),"plugins"])}]++
FilterConfig(FilterConfig, ProcessVars(ProcessVars, CONFIG, []), Rules), FilterConfig(ProcessVars(CONFIG, []), Rules),
%io:format("ejabberd configuration:~n ~p~n", [Config]), %io:format("ejabberd configuration:~n ~p~n", [Config]),