1+ import json
12from typing import Dict
23
34from google .protobuf .struct_pb2 import Struct
@@ -27,6 +28,45 @@ def apply_result_transformer(result_dict, lambda_function: Lambda.Function) -> D
2728 return transformer_result
2829
2930
31+ def flatten_dict (prefix : str , d : dict ) -> dict :
32+ items = {}
33+ for k , v in d .items ():
34+ new_key = f"{ prefix } .{ k } "
35+ if isinstance (v , dict ):
36+ items .update (flatten_dict (new_key , v ))
37+ elif isinstance (v , str ):
38+ items [new_key ] = v .strip (" " )
39+ else :
40+ items [new_key ] = v
41+ return items
42+
43+
44+ def get_flat_global_variables (global_variable_set : dict ):
45+ flat_globals = {}
46+ for gk , gv in global_variable_set .items ():
47+ parsed = None
48+ # If gv is already a dict, use it directly.
49+ if isinstance (gv , dict ):
50+ parsed = gv
51+ else :
52+ try :
53+ parsed = json .loads (gv )
54+ except Exception :
55+ parsed = None
56+ if parsed is not None and isinstance (parsed , dict ):
57+ # Flatten the dict with gk as the prefix.
58+ flat_from_obj = flatten_dict (gk , parsed )
59+ # Merge: only add if the key doesn't already exist.
60+ for fk , fv in flat_from_obj .items ():
61+ if fk not in flat_globals :
62+ flat_globals [fk ] = fv
63+ else :
64+ # Only add the direct key if it's not a dict.
65+ flat_globals [gk ] = gv
66+
67+ return flat_globals
68+
69+
3070def resolve_global_variables (form_fields : [FormField ], global_variable_set : Struct ,
3171 source_type_task_def : Dict ) -> (Dict , Dict ):
3272 all_string_fields = [ff .key_name .value for ff in form_fields if ff .data_type == LiteralType .STRING ]
@@ -36,20 +76,23 @@ def resolve_global_variables(form_fields: [FormField], global_variable_set: Stru
3676 if ff .is_composite :
3777 all_composite_fields [ff .key_name .value ] = ff .composite_fields
3878
79+ global_variable_set_dict = proto_to_dict (global_variable_set ) if global_variable_set else {}
80+ global_variable_set = get_flat_global_variables (global_variable_set_dict )
81+
3982 task_local_variable_map = {}
4083 for gk , gv in global_variable_set .items ():
4184 for tk , tv in source_type_task_def .items ():
4285 if tk in all_string_fields :
4386 if gv is None :
4487 raise Exception (f"Global variable { gk } is None" )
45- source_type_task_def [tk ] = tv .replace (gk , gv )
88+ source_type_task_def [tk ] = tv .replace (gk , str ( gv ) )
4689 if gk in tv :
4790 task_local_variable_map [gk ] = gv
4891 elif tk in all_string_array_fields :
4992 for item in source_type_task_def [tk ]:
5093 if gv is None :
5194 raise Exception (f"Global variable { gk } is None" )
52- source_type_task_def [tk ] = item .replace (gk , gv )
95+ source_type_task_def [tk ] = item .replace (gk , str ( gv ) )
5396 if gk in tv :
5497 task_local_variable_map [gk ] = gv
5598 elif tk in all_composite_fields :
@@ -59,7 +102,7 @@ def resolve_global_variables(form_fields: [FormField], global_variable_set: Stru
59102 if cf .data_type == LiteralType .STRING :
60103 if gv is None :
61104 raise Exception (f"Global variable { gk } is None" )
62- item [cf .key_name .value ] = item [cf .key_name .value ].replace (gk , gv )
105+ item [cf .key_name .value ] = item [cf .key_name .value ].replace (gk , str ( gv ) )
63106 if gk in tv :
64107 task_local_variable_map [gk ] = gv
65108 return source_type_task_def , task_local_variable_map
0 commit comments