Skip to content

Commit

Permalink
Fix '--make-template' for union types (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-tan authored Nov 8, 2022
1 parent 14f402f commit 6af9320
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 7 additions & 5 deletions cwltool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ def generate_example_input(
else:
comment = "optional"
else:
example = CommentedSeq()
for index, entry in enumerate(inptype):
example, comment = generate_example_input(inptype[0], default)
type_names = []
for entry in inptype:
value, e_comment = generate_example_input(entry, default)
example.append(value)
example.yaml_add_eol_comment(e_comment, index)
if e_comment:
type_names.append(e_comment)
comment = "one of " + ", ".join(type_names)
if optional:
comment = "optional"
comment = f"{comment} (optional)"
elif isinstance(inptype, Mapping) and "type" in inptype:
if inptype["type"] == "array":
first_item = cast(MutableSequence[CWLObjectType], inptype["items"])[0]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_make_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@
def test_anonymous_record() -> None:
inputs = cmap([{"type": "record", "fields": []}])
assert main.generate_example_input(inputs, None) == ({}, "Anonymous record type.")


def test_union() -> None:
"""Test for --make-template for a union type."""
inputs = cmap(["string", "string[]"])
assert main.generate_example_input(inputs, None) == (
"a_string",
'one of type "string", type "string[]"',
)


def test_optional_union() -> None:
"""Test for --make-template for an optional union type."""
inputs = cmap(["null", "string", "string[]"])
assert main.generate_example_input(inputs, None) == (
"a_string",
'one of type "string", type "string[]" (optional)',
)

0 comments on commit 6af9320

Please sign in to comment.