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.
|
end.
|
||||||
|
|
||||||
lookup(Prop, Data) when is_binary(Prop) ->
|
lookup(Prop, Data) when is_binary(Prop) ->
|
||||||
case maps:get(Prop, Data, undefined) of
|
case do_one_lookup(Prop, Data) of
|
||||||
undefined ->
|
{error, undefined} ->
|
||||||
try
|
try binary_to_existing_atom(Prop, utf8) of
|
||||||
{ok, maps:get(binary_to_existing_atom(Prop, utf8), Data)}
|
AtomKey ->
|
||||||
|
do_one_lookup(AtomKey, Data)
|
||||||
catch
|
catch
|
||||||
error:{badkey, _} ->
|
|
||||||
{error, undefined};
|
|
||||||
error:badarg ->
|
error:badarg ->
|
||||||
{error, undefined}
|
{error, undefined}
|
||||||
end;
|
end;
|
||||||
Value ->
|
{ok, Value} ->
|
||||||
{ok, Value}
|
{ok, Value}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
do_one_lookup(Key, Data) ->
|
||||||
|
try
|
||||||
|
{ok, maps:get(Key, Data)}
|
||||||
|
catch
|
||||||
|
error:{badkey, _} ->
|
||||||
|
{error, undefined};
|
||||||
|
error:{badmap, _} ->
|
||||||
|
{error, undefined}
|
||||||
|
end.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
{application, emqx_utils, [
|
{application, emqx_utils, [
|
||||||
{description, "Miscellaneous utilities for EMQX apps"},
|
{description, "Miscellaneous utilities for EMQX apps"},
|
||||||
% strict semver, bump manually!
|
% strict semver, bump manually!
|
||||||
{vsn, "5.0.7"},
|
{vsn, "5.0.8"},
|
||||||
{modules, [
|
{modules, [
|
||||||
emqx_utils,
|
emqx_utils,
|
||||||
emqx_utils_api,
|
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}">>,
|
<<"a:1,a:1-1,b:1,b:2,c:1.0,d:oo,d1:hi}">>,
|
||||||
emqx_placeholder:proc_tmpl(Tks, Selected)
|
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