Skip to content

Commit 87392a7

Browse files
committed
feedback, remove stringly types
1 parent cf199e2 commit 87392a7

File tree

1 file changed

+73
-37
lines changed

1 file changed

+73
-37
lines changed

jsonnet/jsonnet.bzl

Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def _setup_deps(deps, tla_code_libraries = {}, ext_code_libraries = {}, transiti
5959
deps: List of deps labels from ctx.attr.deps.
6060
tla_code_libraries: Dict of labels to names from ctx.attr.tla_code_files.
6161
ext_code_libraries: List of deps labels from ctx.attr.ext_code_files.
62+
transitive_extvars: Dict of extvar to values build from _make_extvar_dict
6263
6364
Returns:
6465
Returns a struct containing the following fields:
@@ -69,6 +70,9 @@ def _setup_deps(deps, tla_code_libraries = {}, ext_code_libraries = {}, transiti
6970
short_imports: Depset of Strings containing import flags set by
7071
transitive dependency targets, when invoking Jsonnet as part
7172
of a test where dependencies are stored in runfiles.
73+
transitive_extvars: Dict of extvar to values that has merged the
74+
input value with all extvars of its depdencies.
75+
7276
"""
7377
transitive_sources = []
7478
imports = []
@@ -115,42 +119,72 @@ def _make_extvar_dict(
115119
116120
Returns:
117121
Dictionary with keys are variable names, and values a dict containing
118-
type: The kind of extvar it is from and maps to a ctx.attr, e.g. string, code, string_file, etc.
122+
type: The type of extvar it will be in jsonnet: string or code
119123
value: The string, code, or File depending on type
120124
sources: List of labels that define the extvar
121125
"""
122126
extvars = dict()
123127
label = str(label)
124-
for key, code in ext_code.items():
125-
_make_extvar_dict_update(extvars, "code", key, code, label)
126-
for key in ext_code_envs:
127-
_make_extvar_dict_update(extvars, "code_env", key, None, label)
128-
for file, key in ext_code_files.items():
129-
_make_extvar_dict_update(extvars, "code_file", key, file, label)
130-
for file, key in ext_code_libraries.items():
131-
_make_extvar_dict_update(extvars, "code_library", key, file, label)
132-
for key in ext_str_envs:
133-
_make_extvar_dict_update(extvars, "string_env", key, None, label)
134-
for val, key in ext_str_files.items():
135-
_make_extvar_dict_update(extvars, "string_file", key, val, label)
136-
for key, val in ext_strs.items():
128+
129+
# extvar_lists is a list of tuple (extvar: str, value: None | str | File | JsonnetInfo, extvar_type: str)
130+
# The `None` value are used by environment
131+
# Collect all the Code extvars
132+
# ext_code, dict[extvar, str_value]
133+
extvar_code_lists = zip(ext_code.keys(), ext_code.values())
134+
135+
# ext_code_envs, list[extvar]
136+
extvar_code_lists.extend(zip(ext_code_envs, [None] * len(ext_code_envs)))
137+
138+
# ext_code_files, dict[label, extvar]
139+
extvar_code_lists.extend(zip(ext_code_files.values(), ext_code_files.keys()))
140+
141+
# ext_code_libraries, dict[label, extvar]
142+
extvar_code_lists.extend(zip(ext_code_libraries.values(), ext_code_libraries.keys()))
143+
144+
for key, val in extvar_code_lists:
145+
_make_extvar_dict_update(extvars, "code", key, val, label)
146+
147+
# Collect all of the String extvars
148+
# ext_str_envs, list[extvar]
149+
extvar_str_lists = zip(ext_str_envs, [None] * len(ext_str_envs))
150+
151+
# ext_str_files, dict[label, extvar]
152+
extvar_str_lists.extend(zip(ext_str_files.values(), ext_str_files.keys()))
153+
154+
# ext_strs, dict[extvar, str]
155+
extvar_str_lists.extend(zip(ext_strs.keys(), ext_strs.values()))
156+
157+
for key, val in extvar_str_lists:
137158
_make_extvar_dict_update(extvars, "string", key, val, label)
159+
138160
return extvars
139161

140-
def _make_extvar_dict_update(extvars, extvar_type, key, val, label):
141-
if key in extvars:
162+
def _make_extvar_dict_update(extvars, extvar_type, extvar_name, value, label):
163+
"""Adds an entry to a given extrvars dict and validates its uniqueness
164+
165+
Args:
166+
extvars: Dict of extvars to be added to
167+
extvar_type: String of either "string" or "code"
168+
extvar_name: String of the extvar variable name
169+
value: Either a None, string, File or Target
170+
label: String of the package this extvar is defined in
171+
172+
Returns:
173+
None, modifies the given extvars input in-place
174+
"""
175+
if extvar_name in extvars:
142176
fail("duplicate extvar '{}' of type {} and {}"
143-
.format(key, extvar_type, extvars[key]["type"]))
177+
.format(extvar_name, extvar_type, extvars[extvar_name]["type"]))
144178

145-
if type(val) == "string" or type(val) == "File" or val == None:
179+
if type(value) == "string" or type(value) == "File" or value == None:
146180
pass
147-
elif type(val) == "Target":
148-
val = val[DefaultInfo].files.to_list()[0]
181+
elif type(value) == "Target":
182+
value = value[DefaultInfo].files.to_list()[0]
149183
else:
150-
fail("unknown type of value {} for {} in {}".format(type(val), key, label))
184+
fail("unknown type of value {} for {} in {}".format(type(value), extvar_name, label))
151185

152-
extvars.update([[key, {
153-
"value": val,
186+
extvars.update([[extvar_name, {
187+
"value": value,
154188
"type": extvar_type,
155189
"sources": [label],
156190
}]])
@@ -167,22 +201,24 @@ def _extvar_to_arguments(transitive_extvars, short_path = False):
167201
"""
168202
args = []
169203
for key, val in transitive_extvars.items():
170-
if val["type"] == "string":
171-
args.append("--ext-str %s=%s" % (_quote(key), _quote(val["value"])))
172-
elif val["type"] == "string_env":
173-
args.append("--ext-str %s" % _quote(key))
174-
elif val["type"] == "string_file":
175-
file = val["value"]
176-
args.append("--ext-str-file %s=%s" % (_quote(key), _quote(file.short_path if short_path else file.path)))
177-
elif val["type"] == "code":
178-
args.append("--ext-code %s=%s" % (_quote(key), _quote(val["value"])))
179-
elif val["type"] == "code_env":
180-
args.append("--ext-code %s" % _quote(key))
181-
elif val["type"] == "code_library" or val["type"] == "code_file":
204+
# The --ext-str-* and --ext-code-* flag families are interchangable,
205+
# so the `type` is used to determine which to use.
206+
flag_type = "str" if val["type"] == "string" else val["type"]
207+
208+
# Each different type of value is formatted in the flags differently
209+
if val["value"] == None:
210+
# Environment flags
211+
args.append("--ext-%s %s" % (flag_type, _quote(key)))
212+
elif type(val["value"]) == "string":
213+
# String flags
214+
args.append("--ext-%s %s=%s" % (flag_type, _quote(key), _quote(val["value"])))
215+
elif type(val["value"]) == "File":
216+
# Files and library flags
182217
file = val["value"]
183-
args.append("--ext-code-file %s=%s" % (_quote(key), _quote(file.short_path if short_path else file.path)))
218+
file_path = file.short_path if short_path else file.path
219+
args.append("--ext-%s-file %s=%s" % (flag_type, _quote(key), _quote(file_path)))
184220
else:
185-
fail("The {} key has an unknown extvar type {}: {}".format(key, val["type"], val["sources"]))
221+
fail("The {} key has an unknown extvar type {}: {}".format(key, type(val["value"]), val["sources"]))
186222

187223
return args
188224

0 commit comments

Comments
 (0)