File tree Expand file tree Collapse file tree 2 files changed +48
-2
lines changed
nemoguardrails/colang/v2_x/runtime Expand file tree Collapse file tree 2 files changed +48
-2
lines changed Original file line number Diff line number Diff line change @@ -168,7 +168,22 @@ def eval_expression(expr: str, context: dict) -> Any:
168
168
functions = functions ,
169
169
names = expr_locals ,
170
170
)
171
- return s .eval (updated_expr )
171
+
172
+ result = s .eval (updated_expr )
173
+
174
+ # Assign back changed values to dictionary variables
175
+ for var_name , val in expr_locals .items ():
176
+ if isinstance (val , AttributeDict ):
177
+ var_name = var_name [4 :]
178
+ global_var_name = f"_global_{ var_name } "
179
+ if global_var_name in context :
180
+ context [global_var_name ].clear ()
181
+ context [global_var_name ].update (val )
182
+ else :
183
+ context [var_name ].clear ()
184
+ context [var_name ].update (val )
185
+
186
+ return result
172
187
except Exception as e :
173
188
raise ColangValueError (f"Error evaluating '{ expr } '" ) from e
174
189
Original file line number Diff line number Diff line change @@ -918,5 +918,36 @@ def test_out_flow_variables():
918
918
)
919
919
920
920
921
+ def test_expression_evaluation ():
922
+ """Test the different ways of expression evaluations."""
923
+
924
+ content = """
925
+ flow main
926
+ $dict = {"val": 2 + 3}
927
+ start bot say number ($dict["val"])
928
+ ($dict.update({"val":10}))
929
+ bot say number ($dict["val"] + 1)
930
+
931
+ flow bot say number $number
932
+ await UtteranceBotAction(script="{$number}")
933
+ """
934
+
935
+ config = _init_state (content )
936
+ state = run_to_completion (config , start_main_flow_event )
937
+ assert is_data_in_events (
938
+ state .outgoing_events ,
939
+ [
940
+ {
941
+ "type" : "StartUtteranceBotAction" ,
942
+ "script" : "5" ,
943
+ },
944
+ {
945
+ "type" : "StartUtteranceBotAction" ,
946
+ "script" : "11" ,
947
+ },
948
+ ],
949
+ )
950
+
951
+
921
952
if __name__ == "__main__" :
922
- test_when_or_core_mechanics ()
953
+ test_expression_evaluation ()
You can’t perform that action at this time.
0 commit comments