Skip to content

Commit b6c2f66

Browse files
authored
Topics > Inputs: Clarify the purpose of handling nullables in JS. (#402)
* Topics > Inputs: Clarify the purpose of handling nullables in JS. - The narrative about "using an or operator" appeared to have missed the point about the purpose. In fact, there's nothing special about the "or" (double-pipe || ) logical operator in the JS expression when it comes to checking the actual underlying type of a union-typed value. The problem is twofold: 1. The underlying issue is not that the *value* may "contain one of the exclusive input values"; it's their *type* difference that causes incompatibilities. 2. For this, the solution is to *handle* the possible types, and it only happens that in this particular example, a standard "default" syntax with the shortcircuiting boolean operator would do. Therefore the content is revised to explicitly warn of the possibility of encountering the "wrong" type, and give a better rationale of using the || syntax in the expression. - Minor normalization of blocks. Use three backticks when these blocks are siblings without nesting. * Topics > Inputs: Further clarify how || works in the JS expression - No need to mention "any other" falsy value - Quantify the word "expression" to mean the expression as a whole, not any of its parts that may also stand as an expression. * Topics > Inputs: Remove the mentions of "truthy" and "falsy". These terms are too specific to JS, and likely a distraction from the main point.
1 parent 1ecadfc commit b6c2f66

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/topics/inputs.md

+25-17
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ two commands and the expected output from the command line:
5656

5757
```{runcmd} cwltool inp.cwl inp-job.yml
5858
:working-directory: src/_includes/cwl/inputs
59-
````
59+
```
6060

6161
```{tip}
6262
<p class="rubric">Where did those `/tmp` paths come from?</p>
@@ -223,7 +223,7 @@ In the first example, you can't provide `itemA` without also providing `itemB`.
223223
```{runcmd} cwltool record.cwl record-job2.yml
224224
:working-directory: src/_includes/cwl/inputs
225225
:emphasize-lines: 4, 10-11, 23
226-
````
226+
```
227227

228228
```{code-block} console
229229
$ cat output.txt
@@ -242,7 +242,7 @@ matching item (`itemC`) is added to the command line and remaining item (`itemD`
242242
```{runcmd} cwltool record.cwl record-job3.yml
243243
:working-directory: src/_includes/cwl/inputs
244244
:emphasize-lines: 9-10, 22
245-
````
245+
```
246246

247247
```{code-block} console
248248
$ cat output.txt
@@ -254,10 +254,11 @@ command line.
254254

255255
### Exclusive Input Parameters with Expressions
256256

257-
If you use exclusive input parameters combined with expressions, you need to be
258-
aware that the `inputs` JavaScript object will contain one of the exclusive
259-
input values. This means that you might need to use an **or** boolean operator
260-
to check which values are present.
257+
If you use exclusive input parameters and reference them in expressions, you
258+
need to be aware that the `inputs` JavaScript object will contain one of the
259+
possible, mutually-exclusive input values. Because the types of these exclusive
260+
values may differ, you may need to check which type is in use when you
261+
reference the properties of the `input` object.
261262

262263
Let's use an example that contains an exclusive `file_format` input parameter
263264
that accepts `null` (i.e. no value provided), or any value from an enum.
@@ -268,28 +269,35 @@ that accepts `null` (i.e. no value provided), or any value from an enum.
268269
:name: exclusive-parameter-expressions.cwl
269270
```
270271

271-
Note how the JavaScript expression uses the value of the exclusive input parameter
272-
without taking into consideration a `null` value. If you provide a valid value,
273-
such as fasta (one of the values of the enum), your command should execute
274-
successfully:
272+
Note how the JavaScript expression uses the value of the exclusive input
273+
parameter without taking into consideration a `null` value. If you provide a
274+
valid value, such as `fasta` (one of the possible values of the enum), your
275+
command should execute successfully:
275276

276277
```{runcmd} cwltool exclusive-parameter-expressions.cwl --file_format fasta
277278
:working-directory: src/_includes/cwl/inputs
278-
````
279+
```
279280

280281
However, if you do not provide any input value, then `file_format` will be
281-
evaluated to a `null` value, which does not match the expected type for the
282+
evaluated to `null`, which does not match the expected type for the
282283
output field (a `string`), resulting in failure when running your workflow.
283284

284285
```{runcmd} cwltool exclusive-parameter-expressions.cwl
285286
:working-directory: src/_includes/cwl/inputs
286287
:emphasize-lines: 5-10
287288
```
288289

289-
To correct it, you must remember to use an or operator in your JavaScript expression
290-
when using exclusive parameters, or any parameter that allows `null`. For example,
291-
the expression could be changed to `$(inputs.file_format || 'auto')`, to have
292-
a default value if none was provided in the command line or job input file.
290+
To correct it, you should explicitly handle the possibility of a `null` value.
291+
For example, the expression could be changed to `$(inputs.file_format ||
292+
'auto')`, to have a default value `"auto"` if none was provided in the command
293+
line or job input file.
294+
295+
Here, the boolean “or” operator `||` in JavaScript is used for its
296+
_short-circuiting_ property. If `inputs.file_format` is “true” in a boolean
297+
context (e.g. a valid non-empty string from the enum), the evaluation of the
298+
expression stops at the first operand of `||`; it “short-circuits”. If however
299+
`inputs.file_format` is `null`, the whole expression’s value becomes that of
300+
the second operand, which is why a reasonable default can be provided there.
293301

294302
% TODO
295303
%

0 commit comments

Comments
 (0)