Upgrade parse/2 function

This commit is contained in:
Feng Lee 2017-02-16 11:17:57 +08:00
parent 0019fb2b49
commit 796d5df1c0
1 changed files with 31 additions and 26 deletions

View File

@ -1,5 +1,5 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Copyright (c) 2012-2017 Feng Lee <feng@emqtt.io>. %% Copyright (c) 2013-2017 EMQ Enterprise, Inc. (http://emqtt.io)
%% %%
%% Licensed under the Apache License, Version 2.0 (the "License"); %% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License. %% you may not use this file except in compliance with the License.
@ -16,8 +16,12 @@
-module(emqttd_topic). -module(emqttd_topic).
-author("Feng Lee <feng@emqtt.io>").
-include("emqttd_protocol.hrl"). -include("emqttd_protocol.hrl").
-include("emqttd_internal.hrl").
-import(lists, [reverse/1]). -import(lists, [reverse/1]).
-export([match/2, validate/1, triples/1, words/1, wildcard/1]). -export([match/2, validate/1, triples/1, words/1, wildcard/1]).
@ -55,8 +59,8 @@ wildcard([_H|T]) ->
%% @doc Match Topic name with filter %% @doc Match Topic name with filter
-spec(match(Name, Filter) -> boolean() when -spec(match(Name, Filter) -> boolean() when
Name :: topic() | words(), Name :: topic() | words(),
Filter :: topic() | words()). Filter :: topic() | words()).
match(Name, Filter) when is_binary(Name) and is_binary(Filter) -> match(Name, Filter) when is_binary(Name) and is_binary(Filter) ->
match(words(Name), words(Filter)); match(words(Name), words(Filter));
match([], []) -> match([], []) ->
@ -101,10 +105,7 @@ validate2([''|Words]) ->
validate2(['+'|Words]) -> validate2(['+'|Words]) ->
validate2(Words); validate2(Words);
validate2([W|Words]) -> validate2([W|Words]) ->
case validate3(W) of case validate3(W) of true -> validate2(Words); false -> false end.
true -> validate2(Words);
false -> false
end.
validate3(<<>>) -> validate3(<<>>) ->
true; true;
@ -180,24 +181,28 @@ join(Words) ->
parse(Topic) when is_binary(Topic) -> parse(Topic) when is_binary(Topic) ->
parse(Topic, []). parse(Topic, []).
parse(Topic = <<"$local/", Topic1/binary>>, Options) -> parse(<<"$local/", Topic1/binary>>, Options) ->
case lists:member(local, Options) of if_not_contain(local, Options, fun() ->
true -> error({invalid_topic, Topic}); parse(Topic1, [local | Options])
false -> parse(Topic1, [local | Options]) end);
end;
parse(Topic = <<"$queue/", Topic1/binary>>, Options) -> parse(<<"$queue/", Topic1/binary>>, Options) ->
case lists:keyfind(share, 1, Options) of if_not_contain(share, Options,fun() ->
{share, _} -> error({invalid_topic, Topic}); parse(Topic1, [{share, '$queue'} | Options])
false -> parse(Topic1, [{share, '$queue'} | Options]) end);
end;
parse(Topic = <<"$share/", Topic1/binary>>, Options) -> parse(<<"$share/", Topic1/binary>>, Options) ->
case lists:keyfind(share, 1, Options) of if_not_contain(share, Options, fun() ->
{share, _} -> error({invalid_topic, Topic}); [Share, Topic2] = binary:split(Topic1, <<"/">>),
false -> [Share, Topic2] = binary:split(Topic1, <<"/">>), {Topic2, [{share, Share} | Options]}
{Topic2, [{share, Share} | Options]} end);
end;
parse(Topic, Options) -> {Topic, Options}. parse(Topic, Options) ->
{Topic, Options}.
if_not_contain(local, Options, Fun) ->
?IF(lists:member(local, Options), error(invalid_topic), Fun());
if_not_contain(share, Options, Fun) ->
?IF(lists:keyfind(share, 1, Options), error(invalid_topic), Fun()).