Skip to main content

Scripting

Inline scripting involves embedding and executing code directly within Godspeed YAML code (e.g. workflows, datasources, eventsource, events, etc.,) enhancing seamless integration of script-based logic or functionality.
It is used directly by embedding the code/variables in <% %> tags. This code is evaluated whenever the yaml is needed to be evaluated e.g. at loadtime or runtime.

Loadtime evaluation There are some variables/code which should be evaluated at loadtime. For example, using configs and mappings in datasources, events, or eventsources as given below:

type: axios
base_url: <% config.api.base_url %>
"http.post./helloworld":
fn: helloworld
body:
content:
application/json:
schema:
type: object
properties:
name:
type: string
gender:
type: string
enum: <% mappings.gender %>

Runtime evaluation There are some variables/code which should be evaluated at runtime. For example, using GSContext properties in workflows as given below:

id: helloworld
tasks:
- id: first_task
fn: com.gs.return
args: <% "Hello, The gender of " + inputs.body.name + " is " + inputs.body.gender %>

Default language at global level

The default language is js. You can change the default language globally in defaults.lang key in config/default.yaml. It will be applicable everywhere unless overridden explicitly.

defaults:
lang: js #coffee

Override the default language

You can override the default language by specifying the language inside the starting tag like <coffee% or <js%

type: axios
base_url: <js% config.api.base_url %>
port: <coffee% config.port %>

Scripting in datasources

Within datasources, config or mappings, can be accessed at loadtime.

type: axios
base_url: <% config.api.base_url %>

Scripting in eventsources and events

Within datasources, you can use scripting as given in the below examples:

type: express
port: <% config.http.port %>
docs:
endpoint: /api-docs
jwt:
issuer: <% config.jwt.iss %>
audience: <% config.jwt.aud %>
secretOrKey: <% config.jwt.sec %>
authz:
- fn: com.gs.transform
id: authz_task
args: | # if this condition fails, the else gets executed
<js%
if (inputs.user.role !== 'admin') {
return {
success: false,
code: 403,
message: "Authorization failed"
}
}
%>
"http.post./helloworld":
fn: helloworld
body:
content:
application/json:
schema:
type: object
properties:
name:
type: string
gender:
type: string
enum: <% mappings.gender %>
authz:
- fn: com.gs.transform
id: authz_task
args: | # if this condition fails, the else gets executed
<js%
if (inputs.user.role !== 'system admin') {
return {
success: false,
code: 403,
message: "Authorization failed"
}
}
%>