From 64776aebde1fe48c1038fba3b61f457590ab4408 Mon Sep 17 00:00:00 2001 From: terry-xiaoyu <506895667@qq.com> Date: Fri, 31 May 2019 18:16:53 +0800 Subject: [PATCH] Fix nested_put and nested_get --- src/emqx_rule_runtime.erl | 4 ++-- src/emqx_rule_utils.erl | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/emqx_rule_runtime.erl b/src/emqx_rule_runtime.erl index b7d4a37b2..a8d4e7b5e 100644 --- a/src/emqx_rule_runtime.erl +++ b/src/emqx_rule_runtime.erl @@ -154,7 +154,7 @@ select_and_transform(['*'|More], Input, Output) -> select_and_transform(More, Input, maps:merge(Output, Input)); select_and_transform([{as, Field, Alias}|More], Input, Output) -> Val = eval(Field, Input), - select_and_transform(More, Input, nested_put(Alias, Val, Output)); + select_and_transform(More, Input, nested_put(emqx_rule_utils:atom_key(Alias), Val, Output)); select_and_transform([Field|More], Input, Output) -> Val = eval(Field, Input), Alias = alias(Field, Val), @@ -214,7 +214,7 @@ take_action(#action_instance{id = Id}, Selected, Envs) -> Apply(Selected, Envs). eval({var, Var}, Input) -> %% nested - nested_get(Var, Input); + nested_get(emqx_rule_utils:atom_key(Var), Input); eval({const, Val}, _Input) -> Val; eval({payload, Attr}, Input) when is_binary(Attr) -> diff --git a/src/emqx_rule_utils.erl b/src/emqx_rule_utils.erl index 9ea31ca08..c34ca4fab 100644 --- a/src/emqx_rule_utils.erl +++ b/src/emqx_rule_utils.erl @@ -71,13 +71,16 @@ preproc_sql(Sql, ReplaceWith) -> {match, PlaceHolders} -> {repalce_with(Sql, ReplaceWith), fun(Data) -> - [maps:get(atom_key(Key), Data, undefined) - || Key <- [var(hd(PH)) || PH <- PlaceHolders]] + get_phld(Data, PlaceHolders) end}; nomatch -> {Sql, fun(_) -> [] end} end. +get_phld(Data, PlaceHolders) -> + [maps:get(atom_key(Key), Data, undefined) + || Key <- [var(hd(PH)) || PH <- PlaceHolders]]. + repalce_with(Tmpl, '?') -> re:replace(Tmpl, ?EX_PLACE_HOLDER, "?", [{return, binary}, global]); repalce_with(Tmpl, '$n') -> @@ -94,9 +97,9 @@ repalce_with(Tmpl, '$n') -> atom_key(Key) when is_atom(Key) -> Key; -atom_key(Key) when is_list(Key) -> - try list_to_existing_atom(Key) - catch error:badarg -> error({invalid_key, Key}) +atom_key(Keys = [Key | SubKeys]) when is_binary(Key) -> %% nested keys + try [binary_to_existing_atom(Key, utf8) | SubKeys] + catch error:badarg -> error({invalid_key, Keys}) end; atom_key(Key) when is_binary(Key) -> try binary_to_existing_atom(Key, utf8)