Skip to content

Commit 07d936e

Browse files
committed
Algorithm for coercing list values
1 parent b294537 commit 07d936e

File tree

1 file changed

+61
-12
lines changed

1 file changed

+61
-12
lines changed

spec/Section 3 -- Type System.md

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,20 +1771,69 @@ This allows inputs which accept one or many arguments (sometimes referred to as
17711771
single value, a client can just pass that value directly rather than
17721772
constructing the list.
17731773

1774+
The result of coercion of a value {value} to a list type {listType} is
1775+
{CoerceListValue(value, listType)}.
1776+
1777+
CoerceListValue(value, listType):
1778+
1779+
- If {value} is {null}, return {null}.
1780+
- Let {itemType} be the inner type of {listType}.
1781+
- Let {coercedList} be an empty list.
1782+
- If {value} is a list:
1783+
- For each {itemValue} in {value}:
1784+
- Let {coercedItemValue} be {CoerceListItemValue(itemValue, itemType)}.
1785+
- Append {coercedItemValue} to {coercedList}.
1786+
- Otherwise:
1787+
- Let {coercedItemValue} be {CoerceListItemValue(value, itemType)}.
1788+
- Append {coercedItemValue} to {coercedList}.
1789+
- Return {coercedList}.
1790+
1791+
CoerceListItemValue(itemValue, itemType):
1792+
1793+
- If {itemValue} is {null}, return {null}.
1794+
- Otherwise, if {itemValue} is a Variable:
1795+
- Let {runtimeValue} be the runtime value of that variable, or {null} if no
1796+
runtime value is provided.
1797+
- If {runtimeValue} is {null} and {itemType} is a non-null type, a _field
1798+
error_ must be raised.
1799+
- Return {runtimeValue}.
1800+
- Otherwise, return the result of coercing {itemValue} according to the input
1801+
coercion rules for {itemType}.
1802+
17741803
Following are examples of input coercion with various list types and values:
17751804

1776-
| Expected Type | Literal Value | Variable Values | Coerced Value |
1777-
| ------------- | ---------------- | --------------- | --------------------------- |
1778-
| `[Int]` | `[1, 2, 3]` | `{}` | `[1, 2, 3]` |
1779-
| `[Int]` | `[1, "b", true]` | `{}` | Error: Incorrect item value |
1780-
| `[Int]` | `1` | `{}` | `[1]` |
1781-
| `[Int]` | `null` | `{}` | `null` |
1782-
| `[[Int]]` | `[[1], [2, 3]]` | `{}` | `[[1], [2, 3]]` |
1783-
| `[[Int]]` | `[1, 2, 3]` | `{}` | `[[1], [2], [3]]` |
1784-
| `[[Int]]` | `[1, null, 3]` | `{}` | `[[1], null, [3]]` |
1785-
| `[[Int]]` | `[[1], ["b"]]` | `{}` | Error: Incorrect item value |
1786-
| `[[Int]]` | `1` | `{}` | `[[1]]` |
1787-
| `[[Int]]` | `null` | `{}` | `null` |
1805+
| Expected Type | Literal Value | Variable Values | Coerced Value |
1806+
| ------------- | ---------------- | --------------- | ---------------------------- |
1807+
| `[Int]` | `[1, 2, 3]` | `{}` | `[1, 2, 3]` |
1808+
| `[Int]` | `[1, null]` | `{}` | `[1, null]` |
1809+
| `[Int]` | `[1, "b", true]` | `{}` | Error: Incorrect item value |
1810+
| `[Int]` | `1` | `{}` | `[1]` |
1811+
| `[Int]` | `null` | `{}` | `null` |
1812+
| `[Int]` | `[1, $b]` | `{}` | `[1, null]` |
1813+
| `[Int]` | `[1, $b]` | `{"b": 2}` | `[1, 2]` |
1814+
| `[Int]` | `[1, $b]` | `{"b": null}` | `[1, null]` |
1815+
| `[Int]!` | `[null]` | `{}` | `[null]` |
1816+
| `[Int]!` | `null` | `{}` | Error: Must be non-null |
1817+
| `[Int!]` | `[1, 2, 3]` | `{}` | `[1, 2, 3]` |
1818+
| `[Int!]` | `[1, null]` | `{}` | Error: Item must be non-null |
1819+
| `[Int!]` | `[1, "b", true]` | `{}` | Error: Incorrect item value |
1820+
| `[Int!]` | `1` | `{}` | `[1]` |
1821+
| `[Int!]` | `null` | `{}` | `null` |
1822+
| `[Int!]` | `[1, $b]` | `{}` | Error: Item must be non-null |
1823+
| `[Int!]` | `[1, $b]` | `{"b": 2}` | `[1, 2]` |
1824+
| `[Int!]` | `[1, $b]` | `{"b": null}` | Error: Item must be non-null |
1825+
| `[[Int]]` | `[[1], [2, 3]]` | `{}` | `[[1], [2, 3]]` |
1826+
| `[[Int]]` | `[1, 2, 3]` | `{}` | `[[1], [2], [3]]` |
1827+
| `[[Int]]` | `[1, null, 3]` | `{}` | `[[1], null, [3]]` |
1828+
| `[[Int]]` | `[[1], ["b"]]` | `{}` | Error: Incorrect item value |
1829+
| `[[Int]]` | `1` | `{}` | `[[1]]` |
1830+
| `[[Int]]` | `null` | `{}` | `null` |
1831+
| `[[Int]]` | `[1, [$b]]` | `{}` | `[[1],[null]]` |
1832+
| `[[Int]]` | `[1, [$b]]` | `{"b": null}` | `[[1],[null]]` |
1833+
| `[[Int]]` | `[1, [$b]]` | `{"b": 2}` | `[[1],[2]]` |
1834+
| `[[Int]]` | `[1, $b]` | `{"b": [2]}` | `[[1],[2]]` |
1835+
| `[[Int]]` | `[1, $b]` | `{"b": 2}` | `[[1],[2]]` |
1836+
| `[[Int]]` | `[1, $b]` | `{"b": null}` | `[[1],null]` |
17881837

17891838
## Non-Null
17901839

0 commit comments

Comments
 (0)