Przeglądaj źródła

Merge pull request #14369 from lafirest/feat/rulefunc

feat(rule_engin): add two size-related functions
lafirest 1 rok temu
rodzic
commit
9329a3e1ed

+ 1 - 1
apps/emqx_rule_engine/src/emqx_rule_engine.app.src

@@ -2,7 +2,7 @@
 {application, emqx_rule_engine, [
 {application, emqx_rule_engine, [
     {description, "EMQX Rule Engine"},
     {description, "EMQX Rule Engine"},
     % strict semver, bump manually!
     % strict semver, bump manually!
-    {vsn, "5.2.3"},
+    {vsn, "5.2.4"},
     {modules, []},
     {modules, []},
     {registered, [emqx_rule_engine_sup, emqx_rule_engine]},
     {registered, [emqx_rule_engine_sup, emqx_rule_engine]},
     {applications, [
     {applications, [

+ 16 - 2
apps/emqx_rule_engine/src/emqx_rule_funcs.erl

@@ -131,7 +131,8 @@
     is_float/1,
     is_float/1,
     is_num/1,
     is_num/1,
     is_map/1,
     is_map/1,
-    is_array/1
+    is_array/1,
+    is_empty/1
 ]).
 ]).
 
 
 %% String Funcs
 %% String Funcs
@@ -183,7 +184,8 @@
     map_put/3,
     map_put/3,
     map_keys/1,
     map_keys/1,
     map_values/1,
     map_values/1,
-    map_to_entries/1
+    map_to_entries/1,
+    map_size/1
 ]).
 ]).
 
 
 %% For backward compatibility
 %% For backward compatibility
@@ -785,6 +787,15 @@ is_map(_) -> false.
 is_array(T) when is_list(T) -> true;
 is_array(T) when is_list(T) -> true;
 is_array(_) -> false.
 is_array(_) -> false.
 
 
+is_empty([]) ->
+    true;
+is_empty(<<>>) ->
+    true;
+is_empty(List) when is_list(List) ->
+    false;
+is_empty(Map) ->
+    ?MODULE:map_size(Map) == 0.
+
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 %% String Funcs
 %% String Funcs
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
@@ -1052,6 +1063,9 @@ map_values(Map) ->
 map_to_entries(Map) ->
 map_to_entries(Map) ->
     [#{key => K, value => V} || {K, V} <- maps:to_list(map(Map))].
     [#{key => K, value => V} || {K, V} <- maps:to_list(map(Map))].
 
 
+map_size(Map) ->
+    maps:size(map(Map)).
+
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 %% Hash Funcs
 %% Hash Funcs
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------

+ 18 - 0
apps/emqx_rule_engine/test/emqx_rule_funcs_SUITE.erl

@@ -333,6 +333,16 @@ t_is_array(_) ->
      || T <- [<<>>, a]
      || T <- [<<>>, a]
     ].
     ].
 
 
+t_is_empty(_) ->
+    [
+        ?assertEqual(true, emqx_rule_funcs:is_empty(T))
+     || T <- [[], #{}, <<"{}">>]
+    ],
+    [
+        ?assertEqual(false, emqx_rule_funcs:is_empty(T))
+     || T <- [[1], #{a => b}, <<"{\"a\" : \"b\"}">>]
+    ].
+
 t_coalesce(_) ->
 t_coalesce(_) ->
     ?assertEqual(undefined, emqx_rule_funcs:coalesce([])),
     ?assertEqual(undefined, emqx_rule_funcs:coalesce([])),
     ?assertEqual(undefined, emqx_rule_funcs:coalesce([undefined, undefined, undefined])),
     ?assertEqual(undefined, emqx_rule_funcs:coalesce([undefined, undefined, undefined])),
@@ -946,6 +956,14 @@ t_map_to_entries(_) ->
         apply_func(map_to_entries, [J])
         apply_func(map_to_entries, [J])
     ).
     ).
 
 
+t_map_size(_) ->
+    ?assertEqual(0, apply_func(map_size, [#{}])),
+    ?assertEqual(1, apply_func(map_size, [#{a => b}])),
+    ?assertEqual(0, apply_func(map_size, [[]])),
+    ?assertEqual(1, apply_func(map_size, [[{a, b}]])),
+    ?assertEqual(0, apply_func(map_size, [<<"{}">>])),
+    ?assertEqual(1, apply_func(map_size, [<<"{\"a\" : \"b\"}">>])).
+
 t_bitsize(_) ->
 t_bitsize(_) ->
     ?assertEqual(8, apply_func(bitsize, [<<"a">>])),
     ?assertEqual(8, apply_func(bitsize, [<<"a">>])),
     ?assertEqual(4, apply_func(bitsize, [<<15:4>>])).
     ?assertEqual(4, apply_func(bitsize, [<<15:4>>])).

+ 4 - 0
changes/ce/feat-14369.en.md

@@ -0,0 +1,4 @@
+Introduced two size-related functions in the rule engine:
+- `is_empty` : return `true` if the map or array is empty
+- `map_size` : return the size of a map
+