refactor: use option to skip rendering as string

This commit is contained in:
Thales Macedo Garitezi 2024-06-10 10:49:39 -03:00
parent d34558954d
commit e54cf2f218
3 changed files with 19 additions and 13 deletions

View File

@ -160,7 +160,14 @@ eval_operation(Operation, Transformation, Context) ->
-spec eval_variform([binary(), ...], _, eval_context()) -> -spec eval_variform([binary(), ...], _, eval_context()) ->
{ok, rendered_value()} | {error, term()}. {ok, rendered_value()} | {error, term()}.
eval_variform(K, V, Context) -> eval_variform(K, V, Context) ->
case emqx_variform:render(V, Context) of Opts =
case K of
[<<"payload">> | _] ->
#{eval_as_string => false};
_ ->
#{}
end,
case emqx_variform:render(V, Context, Opts) of
{error, Reason} -> {error, Reason} ->
{error, Reason}; {error, Reason};
{ok, Rendered} -> {ok, Rendered} ->

View File

@ -28,7 +28,7 @@ json_encode(X) ->
json_decode(JSON) -> json_decode(JSON) ->
case emqx_utils_json:safe_decode(JSON, [return_maps]) of case emqx_utils_json:safe_decode(JSON, [return_maps]) of
{ok, X} -> {ok, X} ->
emqx_variform:skip_stringification(X); X;
{error, Reason} -> {error, Reason} ->
throw(#{reason => json_decode_failure, detail => Reason}) throw(#{reason => json_decode_failure, detail => Reason})
end. end.

View File

@ -31,7 +31,6 @@
-export([render/2, render/3]). -export([render/2, render/3]).
-export([compile/1, decompile/1]). -export([compile/1, decompile/1]).
-export([skip_stringification/1]).
-export_type([compiled/0]). -export_type([compiled/0]).
@ -44,7 +43,6 @@
). ).
-define(IS_EMPTY(X), (X =:= <<>> orelse X =:= "" orelse X =:= undefined)). -define(IS_EMPTY(X), (X =:= <<>> orelse X =:= "" orelse X =:= undefined)).
-define(SKIP_STRINGIFICATION, {?MODULE, '__skip_stringification__'}).
%% @doc Render a variform expression with bindings. %% @doc Render a variform expression with bindings.
%% A variform expression is a template string which supports variable substitution %% A variform expression is a template string which supports variable substitution
@ -71,7 +69,7 @@ render(Expression, Bindings) ->
render(Expression, Bindings, #{}). render(Expression, Bindings, #{}).
render(#{form := Form}, Bindings, Opts) -> render(#{form := Form}, Bindings, Opts) ->
eval_as_string(Form, Bindings, Opts); eval_render(Form, Bindings, Opts);
render(Expression, Bindings, Opts) -> render(Expression, Bindings, Opts) ->
case compile(Expression) of case compile(Expression) of
{ok, Compiled} -> {ok, Compiled} ->
@ -80,9 +78,16 @@ render(Expression, Bindings, Opts) ->
{error, Reason} {error, Reason}
end. end.
eval_as_string(Expr, Bindings, _Opts) -> eval_render(Expr, Bindings, Opts) ->
EvalAsStr = maps:get(eval_as_string, Opts, true),
try try
{ok, return_str(eval(Expr, Bindings, #{}))} Result = eval(Expr, Bindings, #{}),
case EvalAsStr of
true ->
{ok, return_str(Result)};
false ->
{ok, Result}
end
catch catch
throw:Reason -> throw:Reason ->
{error, Reason}; {error, Reason};
@ -95,9 +100,6 @@ return_str(Str) when is_binary(Str) -> Str;
return_str(Num) when is_integer(Num) -> integer_to_binary(Num); return_str(Num) when is_integer(Num) -> integer_to_binary(Num);
return_str(Num) when is_float(Num) -> float_to_binary(Num, [{decimals, 10}, compact]); return_str(Num) when is_float(Num) -> float_to_binary(Num, [{decimals, 10}, compact]);
return_str(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8); return_str(Atom) when is_atom(Atom) -> atom_to_binary(Atom, utf8);
%% For usage by other modules (e.g.: message transformation)
return_str({?SKIP_STRINGIFICATION, X}) ->
X;
return_str(Other) -> return_str(Other) ->
throw(#{ throw(#{
reason => bad_return, reason => bad_return,
@ -105,9 +107,6 @@ return_str(Other) ->
got => Other got => Other
}). }).
skip_stringification(X) ->
{?SKIP_STRINGIFICATION, X}.
%% @doc Compile varifom expression. %% @doc Compile varifom expression.
-spec compile(string() | binary() | compiled()) -> {ok, compiled()} | {error, any()}. -spec compile(string() | binary() | compiled()) -> {ok, compiled()} | {error, any()}.
compile(#{form := _} = Compiled) -> compile(#{form := _} = Compiled) ->