Skip to content

Commit

Permalink
Allow map_each to return None in TemplateDict#add_joined
Browse files Browse the repository at this point in the history
Mimics the behavior of `Args#add_all`'s `map_each` callback and can be
used to skip over values.

Also improves the error message when the callback returns an unexpected
type by including the value.
  • Loading branch information
fmeum committed Dec 6, 2022
1 parent 2e2f5d9 commit 846ff8f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ public String getValue() throws EvalException {
/*kwargs=*/ ImmutableMap.of());
if (ret instanceof String) {
parts.add((String) ret);
continue;
} else if (ret != Starlark.NONE) {
throw Starlark.errorf(
"Function provided to map_each must return a String or None, but returned type "
+ "%s for key '%s' and value: %s",
Starlark.type(ret), getKey(), Starlark.repr(val));
}
throw Starlark.errorf(
"Function provided to map_each must return a String, but returned type %s for key:"
+ " %s",
Starlark.type(ret), getKey());
} catch (InterruptedException e) {
// Report the error to the user, but the stack trace is not of use to them
throw Starlark.errorf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3626,7 +3626,7 @@ public void testTemplateExpansionComputedSubstitution() throws Exception {
scratch.file(
"test/rules.bzl",
"def _artifact_to_basename(file):",
" return file.basename",
" return file.basename if file.basename != 'ignored.txt' else None",
"",
"def _undertest_impl(ctx):",
" template_dict = ctx.actions.template_dict()",
Expand Down Expand Up @@ -3657,7 +3657,7 @@ public void testTemplateExpansionComputedSubstitution() throws Exception {
"undertest_rule(",
" name = 'undertest',",
" template = ':template.txt',",
" srcs = ['foo.txt', 'bar.txt', 'baz.txt'],",
" srcs = ['foo.txt', 'bar.txt', 'baz.txt', 'ignored.txt'],",
")",
"testing_rule(",
" name = 'testing',",
Expand Down Expand Up @@ -3914,8 +3914,8 @@ public void testTemplateExpansionComputedSubstitutionMapEachBadReturnType() thro
assertThat(evalException)
.hasMessageThat()
.isEqualTo(
"Function provided to map_each must return a String, but returned type Label for key:"
+ " %files%");
"Function provided to map_each must return a String or None, but returned "
+ "type Label for key '%files%' and value: <source file test/template.txt>");
}

@Test
Expand Down

0 comments on commit 846ff8f

Please sign in to comment.