Skip to content

Commit

Permalink
Apply tidier --structs to modules of the core dirs (2600hz#2160)
Browse files Browse the repository at this point in the history
* Apply tidier --structs to modules of the core dirs

This factors out common structure creations, which typically both
shortens the text of the source and makes the code faster by avoiding
unnecessary term creations.

DO NOT merge yet -- more to come here...

* More tidier --structs transformations

Now you can merge this once it passes the Travis build

* Shut off an unused variable warning
kostis authored and fenollp committed Jun 24, 2016
1 parent 58e1274 commit d538123
Showing 32 changed files with 171 additions and 167 deletions.
18 changes: 9 additions & 9 deletions core/amqp_cron/src/amqp_cron.erl
Original file line number Diff line number Diff line change
@@ -242,8 +242,8 @@ surrendered(State, Sync, _Election) ->
handle_leader_call({cancel, Name}, From, State, Election) when
is_binary(Name); is_atom(Name) ->
case pid_for_name(Name, State#state.tasks) of
{error, Reason} ->
{reply, {error, Reason}, State};
{error, _Reason} = Error ->
{reply, Error, State};
Pid ->
handle_leader_call({cancel, Pid}, From, State, Election)
end;
@@ -261,27 +261,27 @@ handle_leader_call({cancel, Pid}, _From, State, Election) ->
end,
{reply, Reply, State1};
handle_leader_call({schedule, {Name, Schedule, Exec}}, _From, State, Election) ->
case not (Name == undefined)
case (Name =/= undefined)
andalso lists:keymember(Name, 1, State#state.tasks) of
true ->
{reply, {error, already_exists}, State};
false ->
case amqp_cron_task:start_link(Schedule, Exec) of
{ok, Pid} ->
{ok, Pid} = Ok ->
Task = {Name, Pid, Schedule, Exec},
TaskList = [Task|State#state.tasks],
State1 = State#state{tasks = TaskList},
ok = send_tasks(TaskList, Election),
{reply, {ok, Pid}, State1};
{error, Reason} ->
{reply, {error, Reason}, State}
{reply, Ok, State1};
{error, _Reason} = Error ->
{reply, Error, State}
end
end;
handle_leader_call({task_status, Name}, From, State, Election) when
is_binary(Name); is_atom(Name) ->
case pid_for_name(Name, State#state.tasks) of
{error, Reason} ->
{reply, {error, Reason}, State};
{error, _Reason} = Error ->
{reply, Error, State};
Pid ->
handle_leader_call({task_status, Pid}, From, State, Election)
end;
14 changes: 7 additions & 7 deletions core/amqp_cron/src/amqp_cron_task.erl
Original file line number Diff line number Diff line change
@@ -326,12 +326,12 @@ oneshot({oneshot, DateTime}, Exec, ParentPid) ->
gen_server:cast(ParentPid, {error, Message})
end.

run_task({sleeper, Millis}, Exec, ParentPid) ->
run_task({sleeper, Millis} = Sleeper, Exec, ParentPid) ->
gen_server:cast(ParentPid, {running, Millis}),
apply_task(Exec),
gen_server:cast(ParentPid, {waiting, Millis}),
sleep_accounting_for_max(Millis),
run_task({sleeper, Millis}, Exec, ParentPid);
run_task(Sleeper, Exec, ParentPid);
run_task(Schedule, Exec, ParentPid) ->
CurrentDateTime = calendar:universal_time(),
NextValidDateTime = next_valid_datetime(Schedule, CurrentDateTime),
@@ -372,17 +372,17 @@ time_to_wait_millis(CurrentDateTime, NextDateTime) ->

-spec next_valid_datetime(cron(), datetime()) -> datetime().

next_valid_datetime({cron, Schedule}, DateTime) ->
next_valid_datetime({cron, _Schedule} = Cron, DateTime) ->
DateTime1 = advance_seconds(DateTime, ?MINUTE_IN_SECONDS),
{{Y, Mo, D}, {H, M, _}} = DateTime1,
DateTime2 = {{Y, Mo, D}, {H, M, 0}},
next_valid_datetime(not_done, {cron, Schedule}, DateTime2).
next_valid_datetime(not_done, Cron, DateTime2).

-spec next_valid_datetime(done|not_done, cron(), datetime()) -> datetime().

next_valid_datetime(done, _, DateTime) ->
DateTime;
next_valid_datetime(not_done, {cron, Schedule}, DateTime) ->
next_valid_datetime(not_done, {cron, Schedule} = Cron, DateTime) ->
{MinuteSpec, HourSpec, DayOfMonthSpec, MonthSpec, DayOfWeekSpec} = Schedule,
{{Year, Month, Day}, {Hour, Minute, _}} = DateTime,
{Done, Time} =
@@ -427,9 +427,9 @@ next_valid_datetime(not_done, {cron, Schedule}, DateTime) ->
end
end
end,
next_valid_datetime(Done, {cron, Schedule}, Time).
next_valid_datetime(Done, Cron, Time).

-spec value_valid(cronspec(), integer(), integer(), integer()) -> true | false.
-spec value_valid(cronspec(), integer(), integer(), integer()) -> boolean().

value_valid(Spec, Min, Max, Value) when Value >= Min, Value =< Max->
case Spec of
17 changes: 9 additions & 8 deletions core/apns/src/apns_connection.erl
Original file line number Diff line number Diff line change
@@ -119,8 +119,8 @@ open_out(Connection) ->
ssl_opts(Connection),
Connection#apns_connection.timeout
) of
{ok, OutSocket} -> {ok, OutSocket};
{error, Reason} -> {error, Reason}
{ok, _OutSocket} = OK -> OK;
{error, _Reason} = Er -> Er
end.

%% @hidden
@@ -131,15 +131,16 @@ open_feedback(Connection) ->
ssl_opts(Connection),
Connection#apns_connection.timeout
) of
{ok, InSocket} -> {ok, InSocket};
{error, Reason} -> {error, Reason}
{ok, _InSocket} = OK -> OK;
{error, _Reason} = E -> E
end.

%% @hidden
-spec handle_call(X, {pid(), reference()}, state()) ->
{stop, {unknown_request, X}, {unknown_request, X}, state()}.
{stop, Unknown, Unknown, state()} when Unknown :: {'unknown_request', X}.
handle_call(Request, _From, State) ->
{stop, {unknown_request, Request}, {unknown_request, Request}, State}.
Unknown = {unknown_request, Request},
{stop, Unknown, Unknown, State}.

%% @hidden
-spec handle_cast(stop | apns:msg(), state()) ->
@@ -165,9 +166,9 @@ handle_cast(#apns_msg{device_token = DeviceToken, expiry = Expiry,
case send_payload(Socket, Id, Expiry, BinToken, Payload, Priority) of
ok ->
{noreply, State};
{error, Reason} ->
{error, _Reason} = Error ->
apns_queue:fail(Queue, Id),
{stop, {error, Reason}, State}
{stop, Error, State}
end;

handle_cast(stop, State) ->
4 changes: 2 additions & 2 deletions core/gcm/src/gcm.erl
Original file line number Diff line number Diff line change
@@ -100,9 +100,9 @@ do_push(RegIds, Message, Key, Retry) ->
lager:info("retry after: ~p", [RetryAfter]),
_ = do_backoff(RetryAfter, RegIds, Message, Key, Retry),
{error, retry};
{error, Reason} ->
{error, Reason} = Error ->
lager:info("error pushing: ~p", [Reason]),
{error, Reason}
Error
end.

%% handle_result(GCMResult, RegIds) ->
4 changes: 2 additions & 2 deletions core/gcm/src/gcm_api.erl
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ push(RegIds, Message, Key) ->
{error, {retry, RetryTime}};
{ok, {{_StatusLine, _, _}, _, _Body}} ->
{error, timeout};
{error, Reason} ->
{error, Reason};
{error, _Reason} = Error ->
Error;
_OtherError ->
{noreply, unknown}
catch
7 changes: 4 additions & 3 deletions core/kazoo/src/kz_json.erl
Original file line number Diff line number Diff line change
@@ -1051,14 +1051,15 @@ flatten([_ | _] = Elems, Acc, Keys, Depth) ->
end,
Acc, Elems);
flatten({Key, Value}, Acc, Keys, Depth) ->
KList = [Key | Keys],
case length(Keys) + 2 =:= Depth of
'false' ->
flatten(Value, Acc, [Key | Keys], Depth);
flatten(Value, Acc, KList, Depth);
'true' ->
case flatten(Value, [], [Key | Keys], Depth) of
case flatten(Value, [], KList, Depth) of
[] -> Acc;
Group ->
Pos = lists:reverse([Key | Keys]),
Pos = lists:reverse(KList),
[{Pos, Group} | Acc]
end
end;
14 changes: 5 additions & 9 deletions core/kazoo/src/kz_perf_transform.erl
Original file line number Diff line number Diff line change
@@ -45,19 +45,15 @@ kz_trace_opt(Options, Forms) ->


tree(N, Mod, Fun, Ari, Args) ->
Attr = {attr,N,[],none},
{tree,application,
{attr,N,[],none},
Attr,
{application,
{tree,module_qualifier,
{attr,N,[],none},
Attr,
{module_qualifier,{atom,N,kzs_perf},{atom,N,profile}}},
[{tree,tuple,
{attr,N,[],none},
[{atom,N,Mod},{atom,N,Fun},{integer,N,Ari}]},
{tree,list,
{attr,N,[],none},
{list, Args,
none}}]}}.
[{tree,tuple,Attr,[{atom,N,Mod},{atom,N,Fun},{integer,N,Ari}]},
{tree,list, Attr,{list,Args,none}}]}}.

yup(Mod, Fun, Ari, Form) ->
N = erl_syntax:get_pos(Form),
6 changes: 2 additions & 4 deletions core/kazoo/src/listener_federator.erl
Original file line number Diff line number Diff line change
@@ -68,10 +68,8 @@ stop(Pid) ->
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([Parent, Broker]) ->
lager:debug("federating listener ~p on broker ~s"
,[Parent, Broker]
),
init([Parent, Broker]=L) ->
lager:debug("federating listener ~p on broker ~s", L),
kz_amqp_channel:consumer_broker(Broker),
Zone = kz_util:to_binary(kz_amqp_connections:broker_zone(Broker)),
{'ok', #state{parent=Parent
4 changes: 2 additions & 2 deletions core/kazoo_amqp/src/kz_amqp_connections.erl
Original file line number Diff line number Diff line change
@@ -96,11 +96,11 @@ add(#kz_amqp_connection{broker=Broker, tags=Tags}=Connection, Zone) ->
{'ok', Pid} ->
gen_server:cast(?SERVER, {'new_connection', Pid, Broker, Zone, Tags}),
Connection;
{'error', Reason} ->
{'error', Reason} = Error ->
lager:warning("unable to start amqp connection to '~s': ~p"
,[Broker, Reason]
),
{'error', Reason}
Error
end;
add(Broker, Zone) when not is_binary(Broker) ->
add(kz_util:to_binary(Broker), Zone);
12 changes: 6 additions & 6 deletions core/kazoo_amqp/src/kz_amqp_worker.erl
Original file line number Diff line number Diff line change
@@ -572,9 +572,9 @@ handle_call({'request', ReqProp, PublishFun, VFun, Timeout}
,callid = CallId
}
};
{'error', Err} ->
{'error', Err}=Error ->
lager:debug("failed to send request: ~p", [Err]),
{'reply', {'error', Err}, reset(State)}
{'reply', Error, reset(State)}
end;
handle_call({'call_collect', ReqProp, PublishFun, UntilFun, Timeout, Acc}
,{ClientPid, _}=From
@@ -602,9 +602,9 @@ handle_call({'call_collect', ReqProp, PublishFun, UntilFun, Timeout, Acc}
,callid = CallId
}
};
{'error', Err} ->
{'error', Err}=Error ->
lager:debug("failed to send request: ~p", [Err]),
{'reply', {'error', Err}, reset(State)}
{'reply', Error, reset(State)}
end;
handle_call({'publish', ReqProp, PublishFun}, {Pid, _}=From, #state{confirms=C}=State) ->
_ = kz_util:put_callid(ReqProp),
@@ -665,8 +665,8 @@ handle_cast({'gen_listener', {'created_queue', Q}}, #state{defer={Call,From}}=St
{'reply', Reply, NewState} ->
gen_server:reply(From, Reply),
{'noreply', NewState};
{'noreply', NewState} ->
{'noreply', NewState}
{'noreply', _NewState}=NoReply ->
NoReply
end;
handle_cast({'set_negative_threshold', NegThreshold}, State) ->
lager:debug("set negative threshold to ~p", [NegThreshold]),
Loading

0 comments on commit d538123

Please sign in to comment.