From 56c6cf560ae791601c4611abe72c57f04ec2e4d1 Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Wed, 16 Jun 2021 14:13:20 +0800 Subject: [PATCH] fix(dialyzer): guard test can never succeed --- .../src/emqx_plugin_libs_ssl.erl | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/apps/emqx_plugin_libs/src/emqx_plugin_libs_ssl.erl b/apps/emqx_plugin_libs/src/emqx_plugin_libs_ssl.erl index e473a8145..9a17765c6 100644 --- a/apps/emqx_plugin_libs/src/emqx_plugin_libs_ssl.erl +++ b/apps/emqx_plugin_libs/src/emqx_plugin_libs_ssl.erl @@ -53,8 +53,8 @@ save_files_return_opts(Options, SubDir, ResId) -> %% Returns ssl options for Erlang's ssl application. -spec save_files_return_opts(opts_input(), file:name_all()) -> opts(). save_files_return_opts(Options, Dir) -> - GetD = fun(Key, Default) -> maps:get(key_to_atom(Key), Options, Default) end, - Get = fun(Key) -> GetD(key_to_atom(Key), undefined) end, + GetD = fun(Key, Default) -> fuzzy_map_get(Key, Options, Default) end, + Get = fun(Key) -> GetD(Key, undefined) end, KeyFile = Get(keyfile), CertFile = Get(certfile), CAFile = GetD(cacertfile, Get(cafile)), @@ -77,7 +77,7 @@ save_files_return_opts(Options, Dir) -> -spec save_file(file_input(), atom() | string() | binary()) -> string(). save_file(Param, SubDir) -> Dir = filename:join([emqx:get_env(data_dir), SubDir]), - do_save_file( Param, Dir). + do_save_file(Param, Dir). filter([]) -> []; filter([{_, ""} | T]) -> filter(T); @@ -86,10 +86,10 @@ filter([H | T]) -> [H | filter(T)]. do_save_file(#{filename := FileName, file := Content}, Dir) when FileName =/= undefined andalso Content =/= undefined -> do_save_file(ensure_str(FileName), iolist_to_binary(Content), Dir); -do_save_file(FilePath, _) when is_binary(FilePath) -> - ensure_str(FilePath); do_save_file(FilePath, _) when is_list(FilePath) -> FilePath; +do_save_file(FilePath, _) when is_binary(FilePath) -> + ensure_str(FilePath); do_save_file(_, _) -> "". do_save_file("", _, _Dir) -> ""; %% ignore @@ -108,7 +108,11 @@ do_save_file(FileName, Content, Dir) -> ensure_str(L) when is_list(L) -> L; ensure_str(B) when is_binary(B) -> unicode:characters_to_list(B, utf8). -key_to_atom(B) when is_binary(B) -> - binary_to_atom(B, utf8); -key_to_atom(A) when is_atom(A) -> - A. +-spec fuzzy_map_get(atom() | binary(), map(), any()) -> any(). +fuzzy_map_get(Key, Options, Default) -> + case maps:find(Key, Options) of + {ok, Val} -> Val; + error when is_atom(Key) -> + fuzzy_map_get(atom_to_binary(Key, utf8), Options, Default); + error -> Default + end.