feat(emqx): add `emqx_topic:match_any/2` utility
This commit is contained in:
parent
d6c1ee183f
commit
7d0abb6146
|
@ -21,6 +21,7 @@
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([
|
-export([
|
||||||
match/2,
|
match/2,
|
||||||
|
match_any/2,
|
||||||
validate/1,
|
validate/1,
|
||||||
validate/2,
|
validate/2,
|
||||||
levels/1,
|
levels/1,
|
||||||
|
@ -86,6 +87,12 @@ match([_H1 | _], []) ->
|
||||||
match([], [_H | _T2]) ->
|
match([], [_H | _T2]) ->
|
||||||
false.
|
false.
|
||||||
|
|
||||||
|
-spec match_any(Name, [Filter]) -> boolean() when
|
||||||
|
Name :: topic() | words(),
|
||||||
|
Filter :: topic() | words().
|
||||||
|
match_any(Topic, Filters) ->
|
||||||
|
lists:any(fun(Filter) -> match(Topic, Filter) end, Filters).
|
||||||
|
|
||||||
%% @doc Validate topic name or filter
|
%% @doc Validate topic name or filter
|
||||||
-spec validate(topic() | {name | filter, topic()}) -> true.
|
-spec validate(topic() | {name | filter, topic()}) -> true.
|
||||||
validate(Topic) when is_binary(Topic) ->
|
validate(Topic) when is_binary(Topic) ->
|
||||||
|
|
|
@ -49,10 +49,6 @@
|
||||||
tcp_connectivity/3
|
tcp_connectivity/3
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([
|
|
||||||
can_topic_match_oneof/2
|
|
||||||
]).
|
|
||||||
|
|
||||||
-compile({no_auto_import, [float/1]}).
|
-compile({no_auto_import, [float/1]}).
|
||||||
|
|
||||||
-type uri_string() :: iodata().
|
-type uri_string() :: iodata().
|
||||||
|
@ -288,11 +284,3 @@ number_to_list(Int) when is_integer(Int) ->
|
||||||
integer_to_list(Int);
|
integer_to_list(Int);
|
||||||
number_to_list(Float) when is_float(Float) ->
|
number_to_list(Float) when is_float(Float) ->
|
||||||
float_to_list(Float, [{decimals, 10}, compact]).
|
float_to_list(Float, [{decimals, 10}, compact]).
|
||||||
|
|
||||||
can_topic_match_oneof(Topic, Filters) ->
|
|
||||||
lists:any(
|
|
||||||
fun(Fltr) ->
|
|
||||||
emqx_topic:match(Topic, Fltr)
|
|
||||||
end,
|
|
||||||
Filters
|
|
||||||
).
|
|
||||||
|
|
|
@ -206,7 +206,7 @@ get_rules_for_topic(Topic) ->
|
||||||
[
|
[
|
||||||
Rule
|
Rule
|
||||||
|| Rule = #{from := From} <- get_rules(),
|
|| Rule = #{from := From} <- get_rules(),
|
||||||
emqx_plugin_libs_rule:can_topic_match_oneof(Topic, From)
|
emqx_topic:match_any(Topic, From)
|
||||||
].
|
].
|
||||||
|
|
||||||
-spec get_rules_with_same_event(Topic :: binary()) -> [rule()].
|
-spec get_rules_with_same_event(Topic :: binary()) -> [rule()].
|
||||||
|
|
|
@ -14,7 +14,6 @@
|
||||||
|
|
||||||
-module(emqx_rule_sqltester).
|
-module(emqx_rule_sqltester).
|
||||||
|
|
||||||
-include("rule_engine.hrl").
|
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
-export([
|
-export([
|
||||||
|
@ -31,7 +30,7 @@ test(#{sql := Sql, context := Context}) ->
|
||||||
case lists:all(fun is_publish_topic/1, EventTopics) of
|
case lists:all(fun is_publish_topic/1, EventTopics) of
|
||||||
true ->
|
true ->
|
||||||
%% test if the topic matches the topic filters in the rule
|
%% test if the topic matches the topic filters in the rule
|
||||||
case emqx_plugin_libs_rule:can_topic_match_oneof(InTopic, EventTopics) of
|
case emqx_topic:match_any(InTopic, EventTopics) of
|
||||||
true -> test_rule(Sql, Select, Context, EventTopics);
|
true -> test_rule(Sql, Select, Context, EventTopics);
|
||||||
false -> {error, nomatch}
|
false -> {error, nomatch}
|
||||||
end;
|
end;
|
||||||
|
|
Loading…
Reference in New Issue