@@ -133,14 +133,16 @@ _noop_kind_handler = _new_kind_handler(
133
133
transform = lambda v : v ,
134
134
)
135
135
136
- def _to_starlark (values , kind_handlers = {}):
136
+ def _to_starlark (values , kind_handlers = {}, preserve_ordering = False ):
137
137
"""Converts the provied values into Starlark using the information in the \
138
138
kind handlers.
139
139
140
140
Args:
141
141
values: A `list` of values that are processed and added to the output.
142
142
kind_handlers: A `dict` of king handler `struct` values
143
143
(`bzl_selects.new_kind_handler`).
144
+ preserve_ordering: A `bool` for keeping the processing as lists instead of
145
+ sets that deduplicate entries.
144
146
145
147
Returns:
146
148
A `struct` as returned by `starlark_codegen.new_expr`.
@@ -152,40 +154,45 @@ def _to_starlark(values, kind_handlers = {}):
152
154
# dict whose keys are the conditions and the value is the value for the
153
155
# condition.
154
156
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 ))
156
163
157
164
for v in values :
158
165
v_type = type (v )
159
166
if v_type != "struct" :
160
167
if v_type == "list" :
161
- no_condition_results = sets . union ( no_condition_results , sets . make ( v ) )
168
+ no_condition_results . extend ( v )
162
169
else :
163
- sets . insert ( no_condition_results , v )
170
+ no_condition_results . append ( v )
164
171
continue
165
172
166
173
# We are assuming that the select will always result in a list.
167
174
# Hence, we wrap the transformed value in a list.
168
175
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 ))
170
177
if v .condition != None :
171
178
# Collect all of the values associted with a condition.
172
179
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 )
175
182
select_dict [v .condition ] = condition_values
176
183
selects_by_kind [v .kind ] = select_dict
177
184
else :
178
- no_condition_results = sets . union ( no_condition_results , tvs_set )
185
+ no_condition_results . extend ( tvs_set )
179
186
180
187
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 ))
183
190
for (kind , select_dict ) in selects_by_kind .items ():
184
191
if len (expr_members ) > 0 :
185
192
expr_members .append (scg .new_op ("+" ))
186
193
sorted_keys = sorted (select_dict .keys ())
187
194
new_select_dict = {
188
- k : sets . to_list (select_dict [k ])
195
+ k : process_list (select_dict [k ])
189
196
for k in sorted_keys
190
197
}
191
198
kind_handler = kind_handlers .get (kind , _noop_kind_handler )
0 commit comments