feat(emqx): add `emqx_topic:match_any/2` utility

This commit is contained in:
Andrew Mayorov 2023-06-08 08:56:24 +03:00
parent d6c1ee183f
commit 7d0abb6146
No known key found for this signature in database
GPG Key ID: 2837C62ACFBFED5D
4 changed files with 9 additions and 15 deletions

View File

@ -21,6 +21,7 @@
%% APIs
-export([
match/2,
match_any/2,
validate/1,
validate/2,
levels/1,
@ -86,6 +87,12 @@ match([_H1 | _], []) ->
match([], [_H | _T2]) ->
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
-spec validate(topic() | {name | filter, topic()}) -> true.
validate(Topic) when is_binary(Topic) ->

View File

@ -49,10 +49,6 @@
tcp_connectivity/3
]).
-export([
can_topic_match_oneof/2
]).
-compile({no_auto_import, [float/1]}).
-type uri_string() :: iodata().
@ -288,11 +284,3 @@ number_to_list(Int) when is_integer(Int) ->
integer_to_list(Int);
number_to_list(Float) when is_float(Float) ->
float_to_list(Float, [{decimals, 10}, compact]).
can_topic_match_oneof(Topic, Filters) ->
lists:any(
fun(Fltr) ->
emqx_topic:match(Topic, Fltr)
end,
Filters
).

View File

@ -206,7 +206,7 @@ get_rules_for_topic(Topic) ->
[
Rule
|| 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()].

View File

@ -14,7 +14,6 @@
-module(emqx_rule_sqltester).
-include("rule_engine.hrl").
-include_lib("emqx/include/logger.hrl").
-export([
@ -31,7 +30,7 @@ test(#{sql := Sql, context := Context}) ->
case lists:all(fun is_publish_topic/1, EventTopics) of
true ->
%% 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);
false -> {error, nomatch}
end;