Explorar o código

feat(variform): allow boolean (true|false) in expression

zmstone hai 1 ano
pai
achega
521e897141

+ 4 - 2
apps/emqx_utils/src/emqx_variform_parser.yrl

@@ -10,6 +10,7 @@ Terminals
     integer
     float
     string
+    boolean
     '(' ')'
     ',' '[' ']'.
 
@@ -18,8 +19,9 @@ Rootsymbol
 
 %% Grammar Rules
 
-%% Root expression: function call or variable
+%% Root expression: function call or variable or a boolean
 expr -> call_or_var : '$1'.
+expr -> boolean: element(3, '$1').
 
 %% Function call or variable
 call_or_var -> identifier '(' ')' : {call, element(3, '$1'), []}.
@@ -33,7 +35,7 @@ array -> '[' args ']' : {array, '$2'}.
 args -> arg : ['$1'].
 args -> args ',' arg : '$1' ++ ['$3'].
 
-%% Arguments can be expressions, arrays, numbers, or strings
+%% Arguments can be expressions, arrays, numbers, strings or booleans
 arg -> expr : '$1'.
 arg -> array : '$1'.
 arg -> integer: {integer, element(3, '$1')}.

+ 2 - 0
apps/emqx_utils/src/emqx_variform_scan.xrl

@@ -1,5 +1,6 @@
 Definitions.
 %% Define regular expressions for tokens
+BOOLEAN     = true|false
 IDENTIFIER  = [a-zA-Z][-a-zA-Z0-9_.]*
 SQ_STRING   = \'[^\']*\'
 DQ_STRING   = \"[^\"]*\"
@@ -14,6 +15,7 @@ WHITESPACE  = [\s\t\n]+
 
 Rules.
 {WHITESPACE} : skip_token.
+{BOOLEAN}    : {token, {boolean, TokenLine, list_to_atom(TokenChars)}}.
 {IDENTIFIER} : {token, {identifier, TokenLine, TokenChars}}.
 {SQ_STRING}  : {token, {string, TokenLine, unquote(TokenChars, $')}}.
 {DQ_STRING}  : {token, {string, TokenLine, unquote(TokenChars, $")}}.

+ 6 - 0
apps/emqx_utils/test/emqx_variform_tests.erl

@@ -182,6 +182,12 @@ coalesce_test_() ->
         end}
     ].
 
+boolean_literal_test_() ->
+    [
+        ?_assertEqual({ok, <<"true">>}, render("true", #{})),
+        ?_assertEqual({ok, <<"T">>}, render("iif(true,'T','F')", #{}))
+    ].
+
 compare_string_test_() ->
     [
         %% is_nil test