Skip to content

Commit

Permalink
development grammar: remove string interpolation options & add sep
Browse files Browse the repository at this point in the history
…function (#381)

per openwdl/wdl#229
  • Loading branch information
illusional authored and mlin committed May 12, 2020
1 parent 4d426c1 commit 58c5aa2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
4 changes: 4 additions & 0 deletions WDL/StdLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def sub(input: Value.String, pattern: Value.String, replace: Value.String) -> Va
def defined(v: Value.Base):
return Value.Boolean(not isinstance(v, Value.Null))

@static([Type.String(), Type.Array(Type.String())], Type.String())
def sep(sep: Value.String, iterable: Value.Array) -> Value.String:
return Value.String(sep.value.join(v.value for v in iterable.value))

# write_*
static([Type.Array(Type.String())], Type.File(), "write_lines")(
self._write(_serialize_lines)
Expand Down
7 changes: 1 addition & 6 deletions WDL/_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,7 @@
output_decls: "output" "{" bound_decl* "}"
// WDL task commands: with {} and <<< >>> command and ${} and ~{} placeholder styles
!?placeholder_key: "default" | "false" | "true" | "sep"
?placeholder_value: string_literal
| INT -> int
| FLOAT -> float
placeholder_option: placeholder_key "=" placeholder_value
placeholder: placeholder_option* expr
placeholder: expr
?command: command1 | command2
Expand Down
44 changes: 44 additions & 0 deletions tests/test_5stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,50 @@ def test_zip_cross(self):
}
""", expected_exception=WDL.Error.EvalError)

def test_sep(self):
outputs = self._test_task(R"""
version development
task SepTest {
input {
Array[String] inp = ["value1", "value2", "value3"]
}
command {}
output {
String out = sep(",", inp)
}
}
""")
self.assertEqual("value1,value2,value3", outputs["out"])

outputs = self._test_task(R"""
version development
task SepTest {
input {
Array[String] inp = ["value1", "value2", "value3"]
}
command <<<
echo ~{sep(",", inp)}
>>>
output {
String out = read_string(stdout())
}
}
""")
self.assertEqual("value1,value2,value3", outputs["out"])

self._test_task(R"""
version development
task SepTest {
input {
Array[String] inp = ["value1", "value2", "value3"]
}
command <<<
echo ~{sep="," inp}
>>>
}
""", expected_exception=WDL.Error.SyntaxError)


def test_suffix(self):
outputs = self._test_task(R"""
Expand Down

0 comments on commit 58c5aa2

Please sign in to comment.