Skip to content

Commit ef1627e

Browse files
authored
Add example for dynamic combinator (#62)
This example illustrates how to invoke an action from the current package (the package containing the composition).
1 parent ee3f7fd commit ef1627e

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

docs/COMBINATORS.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ assumed.
6969

7070
Examples:
7171
```javascript
72-
composer.action('hello')
72+
composer.action('hello') // default package
7373
composer.action('myPackage/myAction')
7474
composer.action('/whisk.system/utils/echo')
7575
```
76+
To be clear, if no package is specified, the default package is assumed even if
77+
the composition itself is not deployed to the default package. To invoke an
78+
action from the same package as the composition the [`dynamic`](#dynamic)
79+
combinator may be used as illustrated [below](#example).
7680

7781
### Action definition
7882

@@ -532,3 +536,29 @@ Other fields of the input parameter object are ignored.
532536
The `dynamic` combinator invokes the action named _name_ with the input
533537
parameter object _params_. The output parameter object for the composition is
534538
the output parameter object of the action invocation.
539+
540+
### Example
541+
542+
The `dynamic` combinator may be used for example to invoke an action that
543+
belongs to the same package as the composition, without having to specify the
544+
package name beforehand.
545+
546+
```javascript
547+
const composer = require('openwhisk-composer')
548+
549+
function invoke (actionShortName) {
550+
return composer.let(
551+
{ actionShortName },
552+
params => ({ type: 'action', params, name: process.env.__OW_ACTION_NAME.split('/').slice(0, -1).concat(actionShortName).join('/') }),
553+
composer.dynamic())
554+
}
555+
556+
module.exports = composer.seq(
557+
composer.action('echo'), // echo action from the default package
558+
invoke('echo') // echo action from the same package as the composition
559+
)
560+
```
561+
In this example, `let` captures the target action short name at compile time
562+
without expanding it to a fully qualified name. Then, at run time, the package
563+
name is obtained from the environment variable `__OW_ACTION_NAME` and combined
564+
with the action short name. Finally, `dynamic` is used to invoke the action.

0 commit comments

Comments
 (0)