Skip to content

Commit cf199e2

Browse files
committed
ai feedback and stamping fix
1 parent 41312cd commit cf199e2

File tree

1 file changed

+38
-72
lines changed

1 file changed

+38
-72
lines changed

jsonnet/jsonnet.bzl

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -122,73 +122,39 @@ def _make_extvar_dict(
122122
extvars = dict()
123123
label = str(label)
124124
for key, code in ext_code.items():
125-
if key in extvars:
126-
fail("duplicate extVar '{}': {}".format(key, extvars.keys()))
127-
extvars[key] = {
128-
"value": code,
129-
"type": "code",
130-
"sources": [label],
131-
}
125+
_make_extvar_dict_update(extvars, "code", key, code, label)
132126
for key in ext_code_envs:
133-
if key in extvars:
134-
fail("duplicate extVar '{}': {}".format(key, extvars.keys()))
135-
extvars[key] = {
136-
"value": "",
137-
"type": "code_env",
138-
"sources": [label],
139-
}
127+
_make_extvar_dict_update(extvars, "code_env", key, None, label)
140128
for file, key in ext_code_files.items():
141-
if key in extvars:
142-
fail("duplicate extVar '{}': {}".format(key, extvars.keys()))
143-
144-
files = [file]
145-
if type(file) != "File":
146-
files = file[DefaultInfo].files.to_list()
147-
148-
extvars[key] = {
149-
"value": files,
150-
"type": "code_file",
151-
"sources": [label],
152-
}
129+
_make_extvar_dict_update(extvars, "code_file", key, file, label)
153130
for file, key in ext_code_libraries.items():
154-
if key in extvars:
155-
fail("duplicate extVar '{}': {}".format(key, extvars.keys()))
156-
extvars[key] = {
157-
"value": file[DefaultInfo].files.to_list(),
158-
"type": "code_library",
159-
"sources": [label],
160-
}
131+
_make_extvar_dict_update(extvars, "code_library", key, file, label)
161132
for key in ext_str_envs:
162-
if key in extvars:
163-
fail("duplicate extVar '{}': {}".format(key, extvars.keys()))
164-
extvars[key] = {
165-
"value": "",
166-
"type": "string_env",
167-
"sources": [label],
168-
}
169-
for file, key in ext_str_files.items():
170-
if key in extvars:
171-
fail("duplicate extVar '{}': {}".format(key, extvars.keys()))
172-
173-
files = [file]
174-
if type(file) != "File":
175-
files = file[DefaultInfo].files.to_list()
176-
177-
extvars[key] = {
178-
"value": files,
179-
"type": "string_file",
180-
"sources": [label],
181-
}
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)
182136
for key, val in ext_strs.items():
183-
if key in extvars:
184-
fail("duplicate extVar '{}': {}".format(key, extvars.keys()))
185-
extvars[key] = {
186-
"value": val,
187-
"type": "string",
188-
"sources": [label],
189-
}
137+
_make_extvar_dict_update(extvars, "string", key, val, label)
190138
return extvars
191139

140+
def _make_extvar_dict_update(extvars, extvar_type, key, val, label):
141+
if key in extvars:
142+
fail("duplicate extvar '{}' of type {} and {}"
143+
.format(key, extvar_type, extvars[key]["type"]))
144+
145+
if type(val) == "string" or type(val) == "File" or val == None:
146+
pass
147+
elif type(val) == "Target":
148+
val = val[DefaultInfo].files.to_list()[0]
149+
else:
150+
fail("unknown type of value {} for {} in {}".format(type(val), key, label))
151+
152+
extvars.update([[key, {
153+
"value": val,
154+
"type": extvar_type,
155+
"sources": [label],
156+
}]])
157+
192158
def _extvar_to_arguments(transitive_extvars, short_path = False):
193159
"""Converts an transitive_extvars to command line arguments
194160
@@ -206,14 +172,14 @@ def _extvar_to_arguments(transitive_extvars, short_path = False):
206172
elif val["type"] == "string_env":
207173
args.append("--ext-str %s" % _quote(key))
208174
elif val["type"] == "string_file":
209-
file = val["value"][0]
175+
file = val["value"]
210176
args.append("--ext-str-file %s=%s" % (_quote(key), _quote(file.short_path if short_path else file.path)))
211177
elif val["type"] == "code":
212178
args.append("--ext-code %s=%s" % (_quote(key), _quote(val["value"])))
213179
elif val["type"] == "code_env":
214180
args.append("--ext-code %s" % _quote(key))
215181
elif val["type"] == "code_library" or val["type"] == "code_file":
216-
file = val["value"][0]
182+
file = val["value"]
217183
args.append("--ext-code-file %s=%s" % (_quote(key), _quote(file.short_path if short_path else file.path)))
218184
else:
219185
fail("The {} key has an unknown extvar type {}: {}".format(key, val["type"], val["sources"]))
@@ -384,6 +350,12 @@ def _jsonnet_to_json_impl(ctx):
384350
jsonnet_tla_code_files = ctx.attr.tla_code_files
385351
jsonnet_tla_code_libraries = ctx.attr.tla_code_libraries
386352

353+
jsonnet_ext_strs, strs_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_strs, ctx, False)
354+
jsonnet_ext_code, code_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_code, ctx, False)
355+
jsonnet_tla_strs, tla_strs_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_strs, ctx, False)
356+
jsonnet_tla_code, tla_code_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_code, ctx, False)
357+
stamp_inputs = strs_stamp_inputs + code_stamp_inputs + tla_strs_stamp_inputs + tla_code_stamp_inputs
358+
387359
transitive_extvars = _make_extvar_dict(
388360
ctx.label,
389361
jsonnet_ext_code,
@@ -402,12 +374,6 @@ def _jsonnet_to_json_impl(ctx):
402374
transitive_extvars,
403375
)
404376

405-
jsonnet_ext_strs, strs_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_strs, ctx, False)
406-
jsonnet_ext_code, code_stamp_inputs = _make_stamp_resolve(ctx.attr.ext_code, ctx, False)
407-
jsonnet_tla_strs, tla_strs_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_strs, ctx, False)
408-
jsonnet_tla_code, tla_code_stamp_inputs = _make_stamp_resolve(ctx.attr.tla_code, ctx, False)
409-
stamp_inputs = strs_stamp_inputs + code_stamp_inputs + tla_strs_stamp_inputs + tla_code_stamp_inputs
410-
411377
if len(jsonnet_ext_str_file_vars) != len(jsonnet_ext_str_files):
412378
fail("Mismatch of ext_str_file_vars ({}) to ext_str_files ({})".format(jsonnet_ext_str_file_vars, jsonnet_ext_str_files))
413379

@@ -745,10 +711,10 @@ _jsonnet_library_attrs = {
745711
allow_files = _JSONNET_FILETYPE,
746712
),
747713
"ext_code": attr.string_dict(
748-
doc = "Include code from the dict value via extvar. Variable name matches the key"
714+
doc = "Include code from the dict value via extvar. Variable name matches the key",
749715
),
750716
"ext_code_envs": attr.string_list(
751-
doc = "Include code from an environment variable via extvar. Variable name matches the environment variable name"
717+
doc = "Include code from an environment variable via extvar. Variable name matches the environment variable name",
752718
),
753719
"ext_code_files": attr.label_keyed_string_dict(
754720
doc = "Include code from a file from dict key via extvar. Variable name matches the value",
@@ -759,14 +725,14 @@ _jsonnet_library_attrs = {
759725
providers = [JsonnetLibraryInfo],
760726
),
761727
"ext_str_envs": attr.string_list(
762-
doc = "Include string from an environment variable via extvar. Variable name matches the environment variable name"
728+
doc = "Include string from an environment variable via extvar. Variable name matches the environment variable name",
763729
),
764730
"ext_str_files": attr.label_keyed_string_dict(
765731
doc = "Include string from a file from dict key via extvar. Variable name matches the value",
766732
allow_files = True,
767733
),
768734
"ext_strs": attr.string_dict(
769-
doc = "Include string from the dict value via extvar. Variable name matches the key"
735+
doc = "Include string from the dict value via extvar. Variable name matches the key",
770736
),
771737
}
772738

0 commit comments

Comments
 (0)