Skip to content

Commit

Permalink
move kz_util:uniq calls to props:unqiue (2600hz#6134)
Browse files Browse the repository at this point in the history
DRY change to a better implementation.
  • Loading branch information
jamesaimonetti authored and harenson committed Nov 1, 2019
1 parent c41cea9 commit 9181c86
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 34 deletions.
20 changes: 0 additions & 20 deletions core/kazoo/src/kz_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@

-export([application_version/1]).

-export([uniq/1]).

-export([kz_log_md_clear/0, kz_log_md_put/2]).

-ifdef(TEST).
Expand Down Expand Up @@ -776,21 +774,3 @@ application_version(Application) ->
{'ok', Vsn} -> kz_term:to_binary(Vsn);
'undefined' -> <<"unknown">>
end.

%%------------------------------------------------------------------------------
%% @doc Like `lists:usort/1' but preserves original ordering.
%%
%% Time: `O(nlog(n))'
%% @end
%%------------------------------------------------------------------------------
-spec uniq(kz_term:proplist()) -> kz_term:proplist().
uniq(KVs) when is_list(KVs) -> uniq(KVs, sets:new(), []).

uniq([], _, L) -> lists:reverse(L);
uniq([{K,_}=KV|Rest], S, L) ->
case sets:is_element(K, S) of
'true' -> uniq(Rest, S, L);
'false' ->
NewS = sets:add_element(K, S),
uniq(Rest, NewS, [KV|L])
end.
11 changes: 0 additions & 11 deletions core/kazoo/test/kz_util_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,3 @@ runs_in_test_() ->
,?_assertEqual(timeout, kz_util:runs_in(1.0, fun timer:sleep/1, [10]))
,?_assertEqual({ok,ok}, kz_util:runs_in(10.0, fun timer:sleep/1, [1]))
].


uniq_test_() ->
[?_assertEqual([], kz_util:uniq([]))
,?_assertEqual([{module_name, <<"my_module">>}]
,kz_util:uniq([{module_name, <<"my_module">>}
,{module_name, <<"blaaa">>}
,{module_name, false}
])
)
].
2 changes: 1 addition & 1 deletion core/kazoo_number_manager/src/knm_number_options.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mdn_options() ->
-spec to_phone_number_setters(options()) -> knm_phone_number:set_functions().
to_phone_number_setters(Options) ->
[{setter_fun(Option), Value}
|| {Option, Value} <- kz_util:uniq(Options),
|| {Option, Value} <- props:unique(Options),
is_atom(Option),
Option =/= 'crossbar'
].
Expand Down
12 changes: 10 additions & 2 deletions core/kazoo_stdlib/test/props_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ filter_undefined_test_() ->
,?_assertEqual([{<<"pouet">>, null}], props:filter_undefined([{<<"pouet">>, null}]))
].

unique_test() ->
unique_test_() ->
L = [{'a', 'b'}, {'a', 'b'}, {'a', 'c'}, {'b','c'}, {'b','d'}],
?assertEqual([{'a', 'b'}, {'b', 'c'}], props:unique(L)).
[?_assertEqual([{'a', 'b'}, {'b', 'c'}], props:unique(L))
,?_assertEqual([], props:unique([]))
,?_assertEqual([{module_name, <<"my_module">>}]
,props:unique([{module_name, <<"my_module">>}
,{module_name, <<"blaaa">>}
,{module_name, false}
])
)
].

delete_test_() ->
L = [{'a', 1}, {'b', 2}, 'c', {'d', 3}],
Expand Down
6 changes: 6 additions & 0 deletions scripts/kz_diaspora.bash
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,10 @@ kapps_util_amqp() {
replace "kapps_util" "amqp_pool_request" "kz_amqp_worker" "call"
}

kz_util_props() {
replace 'kz_util' 'uniq' 'props' 'unique'
}

echo "ensuring kz_term is used"
kz_util_to_term
echo "ensuring kz_binary is used"
Expand Down Expand Up @@ -553,5 +557,7 @@ echo "updating amqp_util to kz_amqp_util"
amqp_util_to_kz_amqp_util
echo "updating kapps_util amqp to kz_amqp_worker"
kapps_util_amqp
echo "updating uniq usage to props module"
kz_util_props

popd >/dev/null

0 comments on commit 9181c86

Please sign in to comment.