fix: handle badmap inside `emqx_placeholder:proc_tmpl`
This commit is contained in:
parent
bb55b04d46
commit
080cb73da1
|
@ -277,20 +277,29 @@ lookup_var([Prop | Rest], Data0) ->
|
|||
end.
|
||||
|
||||
lookup(Prop, Data) when is_binary(Prop) ->
|
||||
case maps:get(Prop, Data, undefined) of
|
||||
undefined ->
|
||||
try
|
||||
{ok, maps:get(binary_to_existing_atom(Prop, utf8), Data)}
|
||||
case do_one_lookup(Prop, Data) of
|
||||
{error, undefined} ->
|
||||
try binary_to_existing_atom(Prop, utf8) of
|
||||
AtomKey ->
|
||||
do_one_lookup(AtomKey, Data)
|
||||
catch
|
||||
error:{badkey, _} ->
|
||||
{error, undefined};
|
||||
error:badarg ->
|
||||
{error, undefined}
|
||||
end;
|
||||
Value ->
|
||||
{ok, Value} ->
|
||||
{ok, Value}
|
||||
end.
|
||||
|
||||
do_one_lookup(Key, Data) ->
|
||||
try
|
||||
{ok, maps:get(Key, Data)}
|
||||
catch
|
||||
error:{badkey, _} ->
|
||||
{error, undefined};
|
||||
error:{badmap, _} ->
|
||||
{error, undefined}
|
||||
end.
|
||||
|
||||
%%------------------------------------------------------------------------------
|
||||
%% Internal functions
|
||||
%%------------------------------------------------------------------------------
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
{application, emqx_utils, [
|
||||
{description, "Miscellaneous utilities for EMQX apps"},
|
||||
% strict semver, bump manually!
|
||||
{vsn, "5.0.7"},
|
||||
{vsn, "5.0.8"},
|
||||
{modules, [
|
||||
emqx_utils,
|
||||
emqx_utils_api,
|
||||
|
|
|
@ -256,3 +256,25 @@ t_proc_tmpl_arbitrary_var_name_double_quote(_) ->
|
|||
<<"a:1,a:1-1,b:1,b:2,c:1.0,d:oo,d1:hi}">>,
|
||||
emqx_placeholder:proc_tmpl(Tks, Selected)
|
||||
).
|
||||
|
||||
t_proc_tmpl_badmap(_Config) ->
|
||||
ThisTks = emqx_placeholder:preproc_tmpl(<<"${.}">>),
|
||||
Tks = emqx_placeholder:preproc_tmpl(<<"${.a.b.c}">>),
|
||||
BadMap = <<"not-a-map">>,
|
||||
?assertEqual(
|
||||
<<"not-a-map">>,
|
||||
emqx_placeholder:proc_tmpl(ThisTks, BadMap)
|
||||
),
|
||||
?assertEqual(
|
||||
<<"undefined">>,
|
||||
emqx_placeholder:proc_tmpl(Tks, #{<<"a">> => #{<<"b">> => BadMap}})
|
||||
),
|
||||
?assertEqual(
|
||||
<<"undefined">>,
|
||||
emqx_placeholder:proc_tmpl(Tks, #{<<"a">> => BadMap})
|
||||
),
|
||||
?assertEqual(
|
||||
<<"undefined">>,
|
||||
emqx_placeholder:proc_tmpl(Tks, BadMap)
|
||||
),
|
||||
ok.
|
||||
|
|
Loading…
Reference in New Issue