Skip to content

Commit

Permalink
correct Pair JSON serialization to match development spec (#425)
Browse files Browse the repository at this point in the history
{"left": x, "right": y} instead of [x,y]
  • Loading branch information
mlin committed Sep 5, 2020
1 parent dcb8a85 commit 4ce66ed
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
6 changes: 3 additions & 3 deletions WDL/Value.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def __str__(self) -> str:
@property
def json(self) -> Any:
""
return [self.value[0].json, self.value[1].json]
return {"left": self.value[0].json, "right": self.value[1].json}

@property
def children(self) -> Iterable[Base]:
Expand Down Expand Up @@ -416,11 +416,11 @@ def from_json(type: Type.Base, value: Any) -> Base:
return String(value)
if isinstance(type, Type.Array) and isinstance(value, list):
return Array(type, [from_json(type.item_type, item) for item in value])
if isinstance(type, Type.Pair) and isinstance(value, list) and len(value) == 2:
if isinstance(type, Type.Pair) and isinstance(value, dict) and set(value) == {"left", "right"}:
return Pair(
type.left_type,
type.right_type,
(from_json(type.left_type, value[0]), from_json(type.right_type, value[1])),
(from_json(type.left_type, value["left"]), from_json(type.right_type, value["right"])),
)
if (
isinstance(type, Type.Map)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_0eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ def test_json(self):
(WDL.Type.Map((WDL.Type.String(), WDL.Type.Int())), {"cats": 42, "dogs": 99}),
(pty, {"name": "Alyssa", "age": 42, "pets": None}),
(pty, {"name": "Alyssa", "age": 42, "pets": {"cats": 42, "dogs": 99}}),
(WDL.Type.Array(WDL.Type.Pair(WDL.Type.String(), WDL.Type.Int())), [["a",0],["b",1]]),
(WDL.Type.Array(WDL.Type.Pair(WDL.Type.String(), WDL.Type.Int())), [{"left": "a", "right": 0},{"left": "b", "right": 1}]),

(WDL.Type.Boolean(), 42, WDL.Error.InputError),
(WDL.Type.Float(), "your president", WDL.Error.InputError),
Expand Down
21 changes: 18 additions & 3 deletions tests/test_5stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ def test_flatten(self):
""")
self.assertEqual(outputs["ai"], [1, 2, 3, 1, 21, 22])
self.assertEqual(outputs["af"], ["/tmp/X.txt", "/tmp/Y.txt", "/tmp/Z.txt"])
self.assertEqual(outputs["ap"], [[0.1, "mouse"], [3, "cat"], [15, "dog"]])
self.assertEqual(outputs["ap"], [
{"left": 0.1, "right": "mouse"},
{"left": 3, "right": "cat"},
{"left": 15, "right": "dog"}
])

def test_size(self):
with open(os.path.join(self._dir, "alyssa.txt"), "w") as outfile:
Expand Down Expand Up @@ -696,8 +700,19 @@ def test_zip_cross(self):
}
}
""")
self.assertEqual(outputs["zipped"], [[1, "a"], [2, "b"], [3, "c"]])
self.assertEqual(outputs["crossed"], [[1, "d"], [1, "e"], [2, "d"], [2, "e"], [3, "d"], [3, "e"]])
self.assertEqual(outputs["zipped"], [
{"left": 1, "right": "a"},
{"left": 2, "right": "b"},
{"left": 3, "right": "c"}
])
self.assertEqual(outputs["crossed"], [
{"left": 1, "right": "d"},
{"left": 1, "right": "e"},
{"left": 2, "right": "d"},
{"left": 2, "right": "e"},
{"left": 3, "right": "d"},
{"left": 3, "right": "e"}
])

outputs = self._test_task(R"""
version 1.0
Expand Down
22 changes: 20 additions & 2 deletions tests/test_6workflowrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,16 @@ def test_scatters(self):
}
}
""", {"m": 4, "n": 2})
self.assertEqual(outputs["pairs"], [[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1]])
self.assertEqual(outputs["pairs"], [
{"left": 0, "right": 0},
{"left": 0, "right": 1},
{"left": 1, "right": 0},
{"left": 1, "right": 1},
{"left": 2, "right": 0},
{"left": 2, "right": 1},
{"left": 3, "right": 0},
{"left": 3, "right": 1}
])

outputs = self._test_workflow("""
version 1.0
Expand Down Expand Up @@ -213,7 +222,16 @@ def test_scatters(self):
}
}
""", {"m": 4, "n": 2})
self.assertEqual(outputs["pairs"], [[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1]])
self.assertEqual(outputs["pairs"], [
{"left": 0, "right": 0},
{"left": 0, "right": 1},
{"left": 1, "right": 0},
{"left": 1, "right": 1},
{"left": 2, "right": 0},
{"left": 2, "right": 1},
{"left": 3, "right": 0},
{"left": 3, "right": 1}
])

def test_ifs(self):
outputs = self._test_workflow("""
Expand Down

0 comments on commit 4ce66ed

Please sign in to comment.