fix(auto_subscribe): make a warning if the topic is empty when auto subscribe

This commit is contained in:
firest 2022-03-21 17:47:04 +08:00
parent fe95e6ffbb
commit bad04e3571
1 changed files with 21 additions and 7 deletions

View File

@ -16,6 +16,7 @@
-module(emqx_auto_subscribe_placeholder).
-include_lib("emqx/include/emqx_placeholder.hrl").
-include_lib("emqx/include/logger.hrl").
-export([generate/1]).
@ -31,11 +32,23 @@ generate(T0 = #{topic := Topic}) ->
-spec(to_topic_table(list(), map(), map()) -> list()).
to_topic_table(PHs, ClientInfo, ConnInfo) ->
[begin
Topic0 = to_topic(PlaceHolder, ClientInfo, ConnInfo, []),
Fold = fun(#{qos := Qos, rh := RH, rap := RAP, nl := NL, placeholder := PlaceHolder}, Acc) ->
case to_topic(PlaceHolder, ClientInfo, ConnInfo, []) of
{error, _} ->
Acc;
<<>> ->
?SLOG(warning, #{msg => "Topic can't be empty",
clientinfo => ClientInfo,
conninfo => ConnInfo,
placeholder => PlaceHolder
}),
Acc;
Topic0 ->
{Topic, Opts} = emqx_topic:parse(Topic0),
{Topic, Opts#{qos => Qos, rh => RH, rap => RAP, nl => NL}}
end || #{qos := Qos, rh := RH, rap := RAP, nl := NL, placeholder := PlaceHolder} <- PHs].
[{Topic, Opts#{qos => Qos, rh => RH, rap => RAP, nl => NL}} | Acc]
end
end,
lists:foldl(Fold, [], PHs).
%%--------------------------------------------------------------------
%% internal
@ -63,8 +76,9 @@ to_topic([Binary | PTs], C, Co, Res) when is_binary(Binary) ->
to_topic(PTs, C, Co, [Binary | Res]);
to_topic([clientid | PTs], C = #{clientid := ClientID}, Co, Res) ->
to_topic(PTs, C, Co, [ClientID | Res]);
to_topic([username | PTs], C = #{username := undefined}, Co, Res) ->
to_topic(PTs, C, Co, [?PH_USERNAME | Res]);
to_topic([username | _], #{username := undefined}, _, _) ->
?SLOG(error, #{msg => "Username undefined when auto subscribe"}),
{error, username_undefined};
to_topic([username | PTs], C = #{username := Username}, Co, Res) ->
to_topic(PTs, C, Co, [Username | Res]);
to_topic([host | PTs], C, Co = #{peername := {Host, _}}, Res) ->