Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WX-958 write_map() should write its last entry with a newline #7022

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion wom/src/main/scala/wom/values/WomMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ final case class WomMap private(womType: WomMapType, value: Map[WomValue, WomVal

def tsvSerialize: Try[String] = {
(womType.keyType, womType.valueType) match {
case (_: WomPrimitiveType, _: WomPrimitiveType) if value.isEmpty =>
// WDL 1.1 spec on `write_map()` https://github.com/openwdl/wdl/blob/main/versions/1.1/SPEC.md#file-write_mapmapstring-string
// Earlier versions of the spec are not as opinionated on `write_map()` behavior.
// "If the Map is empty, an empty file is written."
Success("")
case (_: WomPrimitiveType, _: WomPrimitiveType) =>
Success(value.map({case (k, v) => s"${k.valueString}\t${v.valueString}"}).mkString("\n"))
// "All lines are terminated by the newline (\n) character."
Success(value.map({case (k, v) => s"${k.valueString}\t${v.valueString}"}).mkString(start="", sep="\n", end="\n"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per WDL 1.1, the best source we have, If the Map is empty, an empty file is written., which I don't think is the case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I noticed the 1.1 spec defines write_map() as a function for Map[String, String] only, though the code and tests here should work for all primitive types.

case _ =>
Failure(new UnsupportedOperationException("Can only TSV serialize a Map[Primitive, Primitive]"))
}
Expand Down
29 changes: 29 additions & 0 deletions wom/src/test/scala/wom/types/WomMapTypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,33 @@ class WomMapTypeSpec extends AnyFlatSpec with CromwellTimeoutSpec with Matchers
case _: UnsupportedOperationException => // expected
}
}

it should "tsvSerialize non-empty maps correctly and with newlines after every line" in {
stringIntMap.tsvSerialize shouldEqual Success("a\t1\nb\t2\nc\t3\n")

val intIntMap = WomMap(WomMapType(WomIntegerType, WomIntegerType), Map(
WomInteger(4) -> WomInteger(1),
WomInteger(5) -> WomInteger(2),
WomInteger(6) -> WomInteger(3),
))
intIntMap.tsvSerialize shouldEqual Success("4\t1\n5\t2\n6\t3\n")

val stringStringMap = WomMap(WomMapType(WomStringType, WomStringType), Map(
WomString("a") -> WomString("x"),
WomString("b") -> WomString("y"),
WomString("c") -> WomString("z")
))
stringStringMap.tsvSerialize shouldEqual Success("a\tx\nb\ty\nc\tz\n")
}

it should "tsvSerialize empty maps to empty Strings" in {
val emptyStringIntMap = WomMap(WomMapType(WomStringType, WomIntegerType), Map.empty)
emptyStringIntMap.tsvSerialize shouldEqual Success("")

val emptyIntIntMap = WomMap(WomMapType(WomIntegerType, WomIntegerType), Map.empty)
emptyIntIntMap.tsvSerialize shouldEqual Success("")

val emptyStringStringMap = WomMap(WomMapType(WomStringType, WomStringType), Map.empty)
emptyStringStringMap.tsvSerialize shouldEqual Success("")
}
}