Просмотр исходного кода

fix: add subbits/4 and subits/5 rule_engine functions

The documentation for the family of subbits functions says that the
fifth and sixth parameters are optional (since they only make sense when
the forth parameter is 'integer'). However, before this commit
`subbits/4` and `subbits/5` did not exist.

Fixes:
https://emqx.atlassian.net/browse/EMQX-11942
https://github.com/emqx/emqx/issues/12587
Kjell Winblad 1 год назад
Родитель
Сommit
365d054e01

+ 12 - 0
apps/emqx_rule_engine/src/emqx_rule_funcs.erl

@@ -96,6 +96,8 @@
     bytesize/1,
     bytesize/1,
     subbits/2,
     subbits/2,
     subbits/3,
     subbits/3,
+    subbits/4,
+    subbits/5,
     subbits/6
     subbits/6
 ]).
 ]).
 
 
@@ -556,6 +558,16 @@ subbits(Bits, Len) when is_integer(Len), is_bitstring(Bits) ->
 subbits(Bits, Start, Len) when is_integer(Start), is_integer(Len), is_bitstring(Bits) ->
 subbits(Bits, Start, Len) when is_integer(Start), is_integer(Len), is_bitstring(Bits) ->
     get_subbits(Bits, Start, Len, <<"integer">>, <<"unsigned">>, <<"big">>).
     get_subbits(Bits, Start, Len, <<"integer">>, <<"unsigned">>, <<"big">>).
 
 
+subbits(Bits, Start, Len, Type) when
+    is_integer(Start), is_integer(Len), is_bitstring(Bits)
+->
+    get_subbits(Bits, Start, Len, Type, <<"unsigned">>, <<"big">>).
+
+subbits(Bits, Start, Len, Type, Signedness) when
+    is_integer(Start), is_integer(Len), is_bitstring(Bits)
+->
+    get_subbits(Bits, Start, Len, Type, Signedness, <<"big">>).
+
 subbits(Bits, Start, Len, Type, Signedness, Endianness) when
 subbits(Bits, Start, Len, Type, Signedness, Endianness) when
     is_integer(Start), is_integer(Len), is_bitstring(Bits)
     is_integer(Start), is_integer(Len), is_bitstring(Bits)
 ->
 ->

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

@@ -911,6 +911,17 @@ t_subbits2_float(_) ->
     ct:pal(";;;;~p", [R2]),
     ct:pal(";;;;~p", [R2]),
     ?assert((RL2 >= 0 andalso RL2 < 0.0001) orelse (RL2 =< 0 andalso RL2 > -0.0001)).
     ?assert((RL2 >= 0 andalso RL2 < 0.0001) orelse (RL2 =< 0 andalso RL2 > -0.0001)).
 
 
+t_subbits_4_args(_) ->
+    R = apply_func(subbits, [<<5.3:64/float>>, 1, 64, <<"float">>]),
+    RL = (5.3 - R),
+    ?assert((RL >= 0 andalso RL < 0.0001) orelse (RL =< 0 andalso RL > -0.0001)).
+
+t_subbits_5_args(_) ->
+    ?assertEqual(
+        456,
+        apply_func(subbits, [<<456:32/integer>>, 1, 32, <<"integer">>, <<"unsigned">>])
+    ).
+
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------
 %% Test cases for Hash funcs
 %% Test cases for Hash funcs
 %%------------------------------------------------------------------------------
 %%------------------------------------------------------------------------------