@@ -133,14 +133,16 @@ _noop_kind_handler = _new_kind_handler(
133133 transform = lambda v : v ,
134134)
135135
136- def _to_starlark (values , kind_handlers = {}):
136+ def _to_starlark (values , kind_handlers = {}, preserve_ordering = False ):
137137 """Converts the provied values into Starlark using the information in the \
138138 kind handlers.
139139
140140 Args:
141141 values: A `list` of values that are processed and added to the output.
142142 kind_handlers: A `dict` of king handler `struct` values
143143 (`bzl_selects.new_kind_handler`).
144+ preserve_ordering: A `bool` for keeping the processing as lists instead of
145+ sets that deduplicate entries.
144146
145147 Returns:
146148 A `struct` as returned by `starlark_codegen.new_expr`.
@@ -152,40 +154,45 @@ def _to_starlark(values, kind_handlers = {}):
152154 # dict whose keys are the conditions and the value is the value for the
153155 # condition.
154156 selects_by_kind = {}
155- no_condition_results = sets .make ()
157+ no_condition_results = []
158+
159+ def process_list (input ):
160+ if preserve_ordering :
161+ return input
162+ return sets .to_list (sets .make (input ))
156163
157164 for v in values :
158165 v_type = type (v )
159166 if v_type != "struct" :
160167 if v_type == "list" :
161- no_condition_results = sets . union ( no_condition_results , sets . make ( v ) )
168+ no_condition_results . extend ( v )
162169 else :
163- sets . insert ( no_condition_results , v )
170+ no_condition_results . append ( v )
164171 continue
165172
166173 # We are assuming that the select will always result in a list.
167174 # Hence, we wrap the transformed value in a list.
168175 kind_handler = kind_handlers .get (v .kind , _noop_kind_handler )
169- tvs_set = sets . make ( lists .flatten (kind_handler .transform (v .value ) ))
176+ tvs_set = lists .flatten (kind_handler .transform (v .value ))
170177 if v .condition != None :
171178 # Collect all of the values associted with a condition.
172179 select_dict = selects_by_kind .get (v .kind , {})
173- condition_values = select_dict .get (v .condition , sets .make ())
174- condition_values = sets . union ( condition_values , tvs_set )
180+ condition_values = sets . to_list ( select_dict .get (v .condition , sets .make () ))
181+ condition_values = condition_values . extend ( tvs_set )
175182 select_dict [v .condition ] = condition_values
176183 selects_by_kind [v .kind ] = select_dict
177184 else :
178- no_condition_results = sets . union ( no_condition_results , tvs_set )
185+ no_condition_results . extend ( tvs_set )
179186
180187 expr_members = []
181- if sets . length (no_condition_results ) > 0 :
182- expr_members .append (sets . to_list (no_condition_results ))
188+ if len (no_condition_results ) > 0 :
189+ expr_members .append (process_list (no_condition_results ))
183190 for (kind , select_dict ) in selects_by_kind .items ():
184191 if len (expr_members ) > 0 :
185192 expr_members .append (scg .new_op ("+" ))
186193 sorted_keys = sorted (select_dict .keys ())
187194 new_select_dict = {
188- k : sets . to_list (select_dict [k ])
195+ k : process_list (select_dict [k ])
189196 for k in sorted_keys
190197 }
191198 kind_handler = kind_handlers .get (kind , _noop_kind_handler )
0 commit comments