Skip to content

Commit ae00d4f

Browse files
committed
docs: centralize String coercion rules
1 parent 1747a3f commit ae00d4f

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

SPEC.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Revisions to this specification are made periodically in order to correct errors
5252
- [Type Conversion](#type-conversion)
5353
- [Primitive Conversion to String](#primitive-conversion-to-string)
5454
- [Type Coercion](#type-coercion)
55+
- [String-to-Primitive Coercion](#string-to-primitive-coercion)
5556
- [Order of Precedence](#order-of-precedence)
5657
- [Coercion of Optional Types](#coercion-of-optional-types)
5758
- [Struct/Object Coercion from Map](#structobject-coercion-from-map)
@@ -1954,9 +1955,9 @@ Whether a coercion is valid is determined from the source and target types durin
19541955
| ---------------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
19551956
| `File` | `String` | |
19561957
| `Directory` | `String` |
1957-
| `Int` | `String` | The `String` is parsed using the same rules as [`read_int`](#read_int) |
1958-
| `Float` | `String` | The `String` is parsed using the same rules as [`read_float`](#read_float) |
1959-
| `Boolean` | `String` | The `String` is parsed using the same rules as [`read_boolean`](#read_boolean) |
1958+
| `Int` | `String` | See [String-to-Primitive Coercion](#string-to-primitive-coercion) |
1959+
| `Float` | `String` | See [String-to-Primitive Coercion](#string-to-primitive-coercion) |
1960+
| `Boolean` | `String` | See [String-to-Primitive Coercion](#string-to-primitive-coercion) |
19601961
| `Float` | `Int` | May cause overflow error |
19611962
| `Y?` | `X` | `X` must be coercible to `Y` |
19621963
| `Array[Y]` | `Array[X]` | `X` must be coercible to `Y` |
@@ -1973,6 +1974,16 @@ Whether a coercion is valid is determined from the source and target types durin
19731974
| `Enum` | `String` | `String` value must exactly match one of the enum's choice names |
19741975
| `String` | `Enum` | The enum choice is serialized to its choice name |
19751976

1977+
###### String-to-Primitive Coercion
1978+
1979+
When a `String` is coerced to an `Int`, `Float`, or `Boolean`, leading and trailing whitespace is ignored and the remaining content is converted as follows:
1980+
1981+
* An `Int` conversion succeeds if the content represents a valid integer in the range of the `Int` type.
1982+
* A `Float` conversion succeeds if the content represents a valid integer or floating point number in the range of the `Float` type.
1983+
* A `Boolean` conversion succeeds if the content is `true` or `false`, compared case-insensitively.
1984+
1985+
If the content does not meet the requirements of the target type, the coercion fails with an error during dynamic evaluation.
1986+
19761987
<details>
19771988
<summary>
19781989
Example: string_to_primitives.wdl
@@ -8984,7 +8995,7 @@ Example output:
89848995
Int read_int(File)
89858996
```
89868997

8987-
Reads a file that contains a single line containing only an integer and (optional) whitespace. If the line contains a valid integer, that value is returned as an `Int`. If the file is empty or does not contain a single integer, an error is raised.
8998+
Reads the contents of a file as a `String` and [coerces](#string-to-primitive-coercion) it to an `Int`. If the file is empty or its contents cannot be coerced to an `Int`, an error is raised.
89888999

89899000
**Parameters**
89909001

@@ -9033,7 +9044,7 @@ Example output:
90339044
Float read_float(File)
90349045
```
90359046

9036-
Reads a file that contains only a numeric value and (optional) whitespace. If the line contains a valid floating point number, that value is returned as a `Float`. If the file is empty or does not contain a single float, an error is raised.
9047+
Reads the contents of a file as a `String` and [coerces](#string-to-primitive-coercion) it to a `Float`. If the file is empty or its contents cannot be coerced to a `Float`, an error is raised.
90379048

90389049
**Parameters**
90399050

@@ -9085,7 +9096,7 @@ Example output:
90859096
Boolean read_boolean(File)
90869097
```
90879098

9088-
Reads a file that contains a single line containing only a boolean value and (optional) whitespace. If the non-whitespace content of the line is "true" or "false", that value is returned as a `Boolean`. If the file is empty or does not contain a single boolean, an error is raised. The comparison is case- and whitespace-insensitive.
9099+
Reads the contents of a file as a `String` and [coerces](#string-to-primitive-coercion) it to a `Boolean`. If the file is empty or its contents cannot be coerced to a `Boolean`, an error is raised.
90899100

90909101
**Parameters**
90919102

0 commit comments

Comments
 (0)