Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions lib/state-machines/state-types/instrinsics/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const statesFormat = require('./format')
const statesArray = require('./array')
const statesStringToJson = require('./stringToJson')
const statesJsonToString = require('./jsonToString')

module.exports = {
Format: statesFormat,
Array: statesArray,
StringToJson: statesStringToJson,
JsonToString: statesJsonToString
Format: require('./format'),
Array: require('./array'),
StringToJson: require('./stringToJson'),
JsonToString: require('./jsonToString'),
UUID: require('./uuid')
}
21 changes: 21 additions & 0 deletions lib/state-machines/state-types/instrinsics/uuid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Description from https://states-language.net/#appendix-b

Use the States.UUID intrinsic function to return a version 4 universally unique identifier (v4 UUID) generated using random numbers.
For example, you can use this function to call other AWS services or resources that need a UUID parameter or insert items in a DynamoDB table.

The States.UUID function is called with no arguments specified:

"uuid.$": "States.UUID()"
The function returns a randomly generated UUID, as in the following example:

{"uuid": "ca4c1140-dcc1-40cd-ad05-7b4aa23df4a8" }
*/

const { v4: uuid } = require('uuid')

function statesUUID () {
return uuid()
}

module.exports = statesUUID
4 changes: 3 additions & 1 deletion test/fixtures/state-machines/intrinsic-functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ module.exports = {
Array_emptyArray: require('./array/empty-array.json'),
Array_numbers: require('./array/numbers.json'),
Array_strings: require('./array/strings.json'),
Array_nestedStatesArray: require('./array/nested-states-array.json')
Array_nestedStatesArray: require('./array/nested-states-array.json'),

UUID: require('./uuid.json')
}
13 changes: 13 additions & 0 deletions test/fixtures/state-machines/intrinsic-functions/uuid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Comment": "Example of the Intrinsic Function State",
"StartAt": "First",
"States": {
"First": {
"Type": "Pass",
"Parameters": {
"foo.$": "States.UUID()"
},
"End": true
}
}
}
48 changes: 37 additions & 11 deletions test/intrinsic-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ describe('Intrinsic Functions', function () {
describe('Function parsing', () => {
describe('function call', () => {
const goodCalls = [
"States.Format('hello {}', 'world')",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of this was a result of standard

'States.Format(\'hello {}\', \'world\')',
'States.StringToJson($path)',
'States.JsonToString($path)',
'States.Array()',
'States.Array(99)',
'States.Array(true)',
'States.Array(false)',
"States.Array('fridge-freezer')",
'States.Array(\'fridge-freezer\')',
'States.Array(null)',
'States.Array(States.Array(1, 2))',
'States.Array(1, 2, States.Array(3, 4))',
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Intrinsic Functions', function () {

describe('tokenise arguments', () => {
const args = [
["'a string'", 'string:a string'],
['\'a string\'', 'string:a string'],
['123', 'number:123'],
['123.45', 'number:123.45'],
['-123', 'number:-123'],
Expand All @@ -66,11 +66,11 @@ describe('Intrinsic Functions', function () {
['null', 'null:null'],
['$.path', 'path:$.path'],
['$.array[0:2]', 'path:$.array[0:2]'],
["'\\''", "string:\\'"],
["'\\}'", 'string:\\}'],
["'\\{'", 'string:\\{'],
["'\\\\'", 'string:\\\\'],
["'embedded \\{\\'hello\\'\\}'", "string:embedded \\{\\'hello\\'\\}"]
['\'\\\'\'', 'string:\\\''],
['\'\\}\'', 'string:\\}'],
['\'\\{\'', 'string:\\{'],
['\'\\\\\'', 'string:\\\\'],
['\'embedded \\{\\\'hello\\\'\\}\'', 'string:embedded \\{\\\'hello\\\'\\}']
]

const context = {
Expand Down Expand Up @@ -127,9 +127,9 @@ describe('Intrinsic Functions', function () {
[['{}', null], 'null'],
[['{}{}', null, null], 'nullnull'],
[['{}', '\\{\\}'], '{}'],
[["\\\\\\{\\'Socks\\'\\}\\\\"], "\\{'Socks'}\\"],
[['{}', "\\\\\\{\\'Socks\\'\\}\\\\"], "\\{'Socks'}\\"],
[['\\\\\\{{}\\}\\\\', "\\'Socks\\'"], "\\{'Socks'}\\"]
[['\\\\\\{\\\'Socks\\\'\\}\\\\'], '\\{\'Socks\'}\\'],
[['{}', '\\\\\\{\\\'Socks\\\'\\}\\\\'], '\\{\'Socks\'}\\'],
[['\\\\\\{{}\\}\\\\', '\\\'Socks\\\''], '\\{\'Socks\'}\\']
]

for (const [args, expected] of goodFormatTests) {
Expand Down Expand Up @@ -187,6 +187,13 @@ describe('Intrinsic Functions', function () {
}
})

describe('States.UUID', () => {
it('States.UUID()', () => {
const uuid = intrinsicFunctions.UUID()
expect(uuid).to.be.a('string')
})
})

describe('In State Machines', () => {
let statebox

Expand Down Expand Up @@ -260,5 +267,24 @@ describe('Intrinsic Functions', function () {
}
})
}

describe('UUID', () => {
it('UUID', async () => {
const stateMachineName = 'UUID'

let executionDescription = await statebox.startExecution(
{},
stateMachineName,
{} // options
)

executionDescription = await statebox.waitUntilStoppedRunning(executionDescription.executionName)

expect(executionDescription.status).to.eql('SUCCEEDED')
expect(executionDescription.stateMachineName).to.eql(stateMachineName)
expect(executionDescription.currentResource).to.eql(undefined)
expect(executionDescription.ctx.foo).to.be.a('string')
})
})
}) // called from state machines
})