-type topic() :: binary().

This commit is contained in:
Feng 2016-01-12 13:58:24 +08:00
parent e3e2c3c738
commit 6e3ae6412d
1 changed files with 11 additions and 9 deletions

View File

@ -1,5 +1,5 @@
%%%----------------------------------------------------------------------------- %%%-----------------------------------------------------------------------------
%%% Copyright (c) 2012-2015 eMQTT.IO, All Rights Reserved. %%% Copyright (c) 2012-2016 eMQTT.IO, All Rights Reserved.
%%% %%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy %%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal %%% of this software and associated documentation files (the "Software"), to deal
@ -31,6 +31,8 @@
-export([join/1, feed_var/3, is_queue/1, systop/1]). -export([join/1, feed_var/3, is_queue/1, systop/1]).
-type topic() :: binary().
%-type type() :: static | dynamic. %-type type() :: static | dynamic.
-type word() :: '' | '+' | '#' | binary(). -type word() :: '' | '+' | '#' | binary().
@ -39,7 +41,7 @@
-type triple() :: {root | binary(), word(), binary()}. -type triple() :: {root | binary(), word(), binary()}.
-export_type([word/0, triple/0]). -export_type([topic/0, word/0, triple/0]).
-define(MAX_TOPIC_LEN, 4096). -define(MAX_TOPIC_LEN, 4096).
@ -47,7 +49,7 @@
%% @doc Is wildcard topic? %% @doc Is wildcard topic?
%% @end %% @end
%%%----------------------------------------------------------------------------- %%%-----------------------------------------------------------------------------
-spec wildcard(binary()) -> true | false. -spec wildcard(topic()) -> true | false.
wildcard(Topic) when is_binary(Topic) -> wildcard(Topic) when is_binary(Topic) ->
wildcard(words(Topic)); wildcard(words(Topic));
wildcard([]) -> wildcard([]) ->
@ -64,8 +66,8 @@ wildcard([_H|T]) ->
%% @end %% @end
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec match(Name, Filter) -> boolean() when -spec match(Name, Filter) -> boolean() when
Name :: binary() | words(), Name :: topic() | words(),
Filter :: binary() | 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([], []) ->
@ -91,7 +93,7 @@ match([], [_H|_T2]) ->
%% @doc Validate Topic %% @doc Validate Topic
%% @end %% @end
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec validate({name | filter, binary()}) -> boolean(). -spec validate({name | filter, topic()}) -> boolean().
validate({_, <<>>}) -> validate({_, <<>>}) ->
false; false;
validate({_, Topic}) when is_binary(Topic) and (size(Topic) > ?MAX_TOPIC_LEN) -> validate({_, Topic}) when is_binary(Topic) and (size(Topic) > ?MAX_TOPIC_LEN) ->
@ -129,7 +131,7 @@ validate3(<<_/utf8, Rest/binary>>) ->
%% @doc Topic to Triples %% @doc Topic to Triples
%% @end %% @end
%%%----------------------------------------------------------------------------- %%%-----------------------------------------------------------------------------
-spec triples(binary()) -> list(triple()). -spec triples(topic()) -> list(triple()).
triples(Topic) when is_binary(Topic) -> triples(Topic) when is_binary(Topic) ->
triples(words(Topic), root, []). triples(words(Topic), root, []).
@ -154,7 +156,7 @@ bin(B) when is_binary(B) -> B.
%% @doc Split Topic Path to Words %% @doc Split Topic Path to Words
%% @end %% @end
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec words(binary()) -> words(). -spec words(topic()) -> words().
words(Topic) when is_binary(Topic) -> words(Topic) when is_binary(Topic) ->
[word(W) || W <- binary:split(Topic, <<"/">>, [global])]. [word(W) || W <- binary:split(Topic, <<"/">>, [global])].
@ -167,7 +169,7 @@ word(Bin) -> Bin.
%% @doc Queue is a special topic name that starts with "$Q/" %% @doc Queue is a special topic name that starts with "$Q/"
%% @end %% @end
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec is_queue(binary()) -> boolean(). -spec is_queue(topic()) -> boolean().
is_queue(<<"$Q/", _Queue/binary>>) -> is_queue(<<"$Q/", _Queue/binary>>) ->
true; true;
is_queue(_) -> is_queue(_) ->