Variable insertion
Variable insertion (Interpolation) is a way to dynamically modify a string expression or object at runtime.
Usage Engine uses curly brackets to indicate that a variable insertion will take place. Variable insertion is supported in some functions' configuration settings, like Script and Script aggregator.
Within the curly brackets, you can access variables that belong to different scopes and use them to generate a dynamically changed output at runtime. Depending on what you want to achieve or manipulate, the output from variable insertion can be a string (if prefixed by '$') or an object( if prefixed by '@' ).
Scopes
Usage Engine allows you to perform Variable Insertion using the following scopes:
payload
meta
sharedStore
The table below describes how to use these scopes with a few examples. You can also perform nested Variable Insertion using the same or different scopes.
Note!
While using '$' or '@', note the following:
${scope} will result in a string. ${scope} behaves like a JavaScript template Variable Insertion with the following exceptions:
Returns 'undefined' instead of throwing an error when accessing non-existent paths
Returns full object (payload) for ${payload}
Returns a JSON representation of objects instead of [Object object]
@{scope} will result in an object
Example
If you have a nested data structure in payload.foo
, and you want to keep the structure and not transform it into a string, then you must use @{payload.foo}
instead of ${payload.foo}
.
Variable insertion with scopes
You can use variable insertion using the following scopes/variables/objects only:
Scope | Explanation | Example |
---|---|---|
You can perform variable insertion on any data from the current record.
Note!
| // Payload = {
allFruits:
{
banana: 1,
apple: 2,
orange: 3
},
myFruit: 'apple'
} To access or, if you want to use nested Variable Insertion, you can use square brackets '[]'
| |
meta | Meta covers additional information that gives further context to the data (metadata).
You can use variable insertion using any data from the current record's metadata. You can use Note!
| For example, if you want to try to read from an Amazon S3 forwarder function using a Script function: meta.fileName = "myFile"
log.info(meta.fileName);
await push(payload); And, do variable insertion on the filename: |
sharedStore
| Data that is currently stored in the shared store. The The See Script for more information. | sharedStore:
1 = 5.csv
2 = 10.csv
anotherProperty = {
first:
{
second: "third"
}
}
${sharedStore[1]} ----> 5.csv
// SharedStore = { file1: 'my-first-file.csv', file2: 'my-second-file.csv'} |