Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/payload template top level array #83

Merged
merged 3 commits into from
Nov 29, 2023
Merged
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
251 changes: 251 additions & 0 deletions __tests__/InputOutputProcessing.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { JSONValue } from '../src/typings/JSONValue';
import { StatesResultPathMatchFailureError } from '../src/error/predefined/StatesResultPathMatchFailureError';
import {
processInputPath,
Expand Down Expand Up @@ -268,6 +269,131 @@ describe('Input processing', () => {
});
});

test('should return `Parameters` array payload with values replaced according to path fields', () => {
const parameters: JSONValue = [
{ field1: 50, 'field2.$': '$.movies[0].director' },
{
field3: false,
field4: {
'field5.$': '$.metadata',
field6: [1, 2, 3],
},
},
];

const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual([
{ field1: 50, field2: 'Quentin Tarantino' },
{
field3: false,
field4: {
field5: {
lastUpdated: '2020-05-27T08:00:00Z',
},
field6: [1, 2, 3],
},
},
]);
});

test('should return `Parameters` numeric payload as is', () => {
const parameters = 3.141592;
const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual(3.141592);
});

test('should return `Parameters` string payload as is', () => {
const parameters = 'Hello, world!';
const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual('Hello, world!');
});

test('should return `Parameters` boolean payload as is', () => {
const parameters = true;
const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual(true);
});

// TODO: Add test to assert field value is valid JSONPath when field name ends with `.$` suffix.
// For instance: { 'path.$': 'movies' } would not be a valid JSONPath, as the value doesn't begin with `$.`
});
Expand Down Expand Up @@ -443,6 +569,131 @@ describe('Output processing', () => {
});
});

test('should return `ResultSelector` array payload with values replaced according to path fields', () => {
const parameters: JSONValue = [
{ field1: 50, 'field2.$': '$.movies[0].director' },
{
field3: false,
field4: {
'field5.$': '$.metadata',
field6: [1, 2, 3],
},
},
];

const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual([
{ field1: 50, field2: 'Quentin Tarantino' },
{
field3: false,
field4: {
field5: {
lastUpdated: '2020-05-27T08:00:00Z',
},
field6: [1, 2, 3],
},
},
]);
});

test('should return `ResultSelector` numeric payload as is', () => {
const parameters = 3.141592;
const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual(3.141592);
});

test('should return `ResultSelector` string payload as is', () => {
const parameters = 'Hello, world!';
const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual('Hello, world!');
});

test('should return `ResultSelector` boolean payload as is', () => {
const parameters = true;
const input = {
movies: [
{
director: 'Quentin Tarantino',
title: 'Reservoir Dogs',
year: 1992,
},
{
director: 'Brian De Palma',
title: 'Mission: Impossible',
year: 1996,
},
],
metadata: {
lastUpdated: '2020-05-27T08:00:00Z',
},
};
const context = {};

const result = processPayloadTemplate(parameters, input, context);

expect(result).toEqual(true);
});

// TODO: Add test to assert field value is valid JSONPath when field name ends with `.$` suffix.
// For instance: { 'path.$': 'movies' } would not be a valid JSONPath, as the value doesn't begin with `$.`
});
Expand Down
18 changes: 16 additions & 2 deletions src/stateMachine/InputOutputProcessing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { PayloadTemplate } from '../typings/InputOutputProcessing';
import type { JSONValue } from '../typings/JSONValue';
import type { Context } from '../typings/Context';
import { isPlainObj } from '../util';
Expand Down Expand Up @@ -34,11 +33,26 @@ function processInputPath(path: string | null | undefined, input: JSONValue, con
* @param context The context object to evaluate, if the path expression starts with `$$`.
* @returns The processed payload template.
*/
function processPayloadTemplate(payloadTemplate: PayloadTemplate, json: JSONValue, context: Context): PayloadTemplate {
function processPayloadTemplate(payloadTemplate: JSONValue, json: JSONValue, context: Context): JSONValue {
if (typeof payloadTemplate !== 'object' || payloadTemplate === null) {
// Processing a primitive value is not described in the spec, but allowed by the AWS implementation
return payloadTemplate;
}

if (Array.isArray(payloadTemplate)) {
// Processing an array value is not described in the spec, but allowed by the AWS implementation
return payloadTemplate.map((value) => processPayloadTemplate(value, json, context));
}

const resolvedProperties = Object.entries(payloadTemplate).map(([key, value]) => {
let sanitizedKey = key;
let resolvedValue = value;

// Recursively process child array
if (Array.isArray(value)) {
resolvedValue = value.map((innerValue) => processPayloadTemplate(innerValue, json, context));
}

// Recursively process child object
if (isPlainObj(value)) {
resolvedValue = processPayloadTemplate(value, json, context);
Expand Down
8 changes: 4 additions & 4 deletions src/typings/InputOutputProcessing.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { JSONObject } from './JSONValue';
import type { JSONArray, JSONObject, JSONValue } from './JSONValue';

export type PayloadTemplate = JSONObject;
export type PayloadTemplate = JSONObject | JSONArray;

export interface CanHaveInputPath {
InputPath?: string | null;
}

export interface CanHaveParameters {
Parameters?: PayloadTemplate;
Parameters?: JSONValue;
}

export interface CanHaveResultSelector {
ResultSelector?: PayloadTemplate;
ResultSelector?: JSONValue;
}

export interface CanHaveResultPath {
Expand Down