feat(variform): add a builtin function to get env vars

This commit is contained in:
firest 2024-07-23 15:06:06 +08:00
parent a2bed1efb8
commit 79020b2436
3 changed files with 43 additions and 0 deletions

View File

@ -252,6 +252,9 @@
timezone_to_offset_seconds/1 timezone_to_offset_seconds/1
]). ]).
%% System functions
-export([getenv/1]).
%% See extra_functions_module/0 and set_extra_functions_module/1 in the %% See extra_functions_module/0 and set_extra_functions_module/1 in the
%% emqx_rule_engine module %% emqx_rule_engine module
-callback handle_rule_function(atom(), list()) -> any() | {error, no_match_for_function}. -callback handle_rule_function(atom(), list()) -> any() | {error, no_match_for_function}.
@ -1262,3 +1265,9 @@ convert_timestamp(MillisecondsTimestamp) ->
uuid_str(UUID, DisplayOpt) -> uuid_str(UUID, DisplayOpt) ->
uuid:uuid_to_string(UUID, DisplayOpt). uuid:uuid_to_string(UUID, DisplayOpt).
%%------------------------------------------------------------------------------
%% System Funcs
%%------------------------------------------------------------------------------
getenv(Env) ->
emqx_variform_bif:getenv(Env).

View File

@ -79,6 +79,12 @@
%% Number compare functions %% Number compare functions
-export([num_comp/2, num_eq/2, num_lt/2, num_lte/2, num_gt/2, num_gte/2]). -export([num_comp/2, num_eq/2, num_lt/2, num_lte/2, num_gt/2, num_gte/2]).
%% System
-export([getenv/1]).
-define(CACHE(Key), {?MODULE, Key}).
-define(ENV_CACHE(Env), ?CACHE({env, Env})).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% String Funcs %% String Funcs
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -569,3 +575,24 @@ num_lte(A, B) ->
num_gte(A, B) -> num_gte(A, B) ->
R = num_comp(A, B), R = num_comp(A, B),
R =:= gt orelse R =:= eq. R =:= gt orelse R =:= eq.
%%------------------------------------------------------------------------------
%% System
%%------------------------------------------------------------------------------
getenv(Bin) when is_binary(Bin) ->
EnvKey = ?ENV_CACHE(Bin),
case persistent_term:get(EnvKey, undefined) of
undefined ->
Name = erlang:binary_to_list(Bin),
Result =
case os:getenv(Name) of
false ->
<<>>;
Value ->
erlang:list_to_binary(Value)
end,
persistent_term:put(EnvKey, Result),
Result;
Result ->
Result
end.

View File

@ -72,3 +72,10 @@ base64_encode_decode_test() ->
RandBytes = crypto:strong_rand_bytes(100), RandBytes = crypto:strong_rand_bytes(100),
Encoded = emqx_variform_bif:base64_encode(RandBytes), Encoded = emqx_variform_bif:base64_encode(RandBytes),
?assertEqual(RandBytes, emqx_variform_bif:base64_decode(Encoded)). ?assertEqual(RandBytes, emqx_variform_bif:base64_decode(Encoded)).
system_test() ->
EnvName = erlang:atom_to_list(?MODULE),
EnvVal = erlang:atom_to_list(?FUNCTION_NAME),
EnvNameBin = erlang:list_to_binary(EnvName),
os:putenv(EnvName, EnvVal),
?assertEqual(erlang:list_to_binary(EnvVal), emqx_variform_bif:getenv(EnvNameBin)).