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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"JSONPath": "^0.11.2",
"clone": "^1.0.2",
"velocityjs": "^0.7.4"
"velocityjs": "^2.0.6"
},
"devDependencies": {
"adm-zip": "^0.4.7",
Expand Down
53 changes: 53 additions & 0 deletions test/fixtures/complexExpectedOutput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = [
{
status: 'TO_DELIVER',
client: {
name: 'AClient',
id: '9da8ca23-7508-4b0b-98c5-6d198562d76e',
},
rentalPeriod: {
id: '12bec83f-1ab2-4ec6-9238-b7c06799c530',
},
contract: {
id: '33d21c93-62c3-475d-aabf-ede4a8b22038',
amendments: [
{
id: "7036793c-e00f-4aa2-a187-51c060e4c4e2",
options: [
{name: "4 summer tires"}
]
}
],
order: {
id: 'b0ad6ddb-6cf1-4413-abd5-7f14f86d9536',
options: [],
},
},
model: {
id: '98e6426b-6b07-4562-be32-f5859d343984',
},
},
{
status: 'TO_DELIVER',
client: {
name: 'AnotherClient',
id: '9da8ca23-7508-4b0b-98c5-6d198562d76e',
},
rentalPeriod: {
id: '12bec83f-1ab2-4ec6-9238-b7c06799c530',
},
contract: {
id: '33d21c93-62c3-475d-aabf-ede4a8b22038',
amendments: [],
order: {
id: 'b0ad6ddb-6cf1-4413-abd5-7f14f86d9536',
options: [
{name: "4 summer tires"}
],
},
},
model: {
id: 'a9f14adc-5306-42a4-891a-d799198095af',
},
}
]
99 changes: 99 additions & 0 deletions test/fixtures/complexInput.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"Items": [
{
"clientId": {
"S": "9da8ca23-7508-4b0b-98c5-6d198562d76e"
},
"sk": {
"S": "RP#33d21c93-62c3-475d-aabf-ede4a8b22038#12bec83f-1ab2-4ec6-9238-b7c06799c530"
},
"clientName": {
"S": "AClient"
},
"amendments": {
"L": [
{
"M": {
"id": {
"S": "7036793c-e00f-4aa2-a187-51c060e4c4e2"
},
"options": {
"L": [
{
"M": {
"name": {
"S": "4 summer tires"
}
}
}
]
}
}
}
]
},
"status": {
"S": "TO_DELIVER"
},
"carModel": {
"M": {
"id": {
"S": "98e6426b-6b07-4562-be32-f5859d343984"
}
}
},
"order": {
"M": {
"options": {
"L": []
},
"id": {
"S": "b0ad6ddb-6cf1-4413-abd5-7f14f86d9536"
}
}
}
},
{
"clientId": {
"S": "9da8ca23-7508-4b0b-98c5-6d198562d76e"
},
"sk": {
"S": "RP#33d21c93-62c3-475d-aabf-ede4a8b22038#12bec83f-1ab2-4ec6-9238-b7c06799c530"
},
"clientName": {
"S": "AnotherClient"
},
"amendments": {
"L": []
},
"status": {
"S": "TO_DELIVER#1645022598375"
},
"carModel": {
"M": {
"id": {
"S": "a9f14adc-5306-42a4-891a-d799198095af"
}
}
},
"order": {
"M": {
"options": {
"L": [
{
"M": {
"name": {
"S": "4 summer tires"
}
}
}
]
},
"id": {
"S": "b0ad6ddb-6cf1-4413-abd5-7f14f86d9536"
}
}
}
}
]
}
43 changes: 43 additions & 0 deletions test/fixtures/complexMapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = `
#set($inputRoot = $input.path('$.Items'))
[
#foreach($car in $inputRoot)
{
"status": "$car.status.S.split('#')[0]",
"client": {
"name": "$car.clientName.S",
"id": "$car.clientId.S"
},
"rentalPeriod": {
"id": "$car.sk.S.split('#')[2]"
},
"contract": {
"id": "$car.sk.S.split('#')[1]",
"amendments": [
#foreach($amendment in $car.amendments.L) {
"id": "$amendment.M.id.S",
"options": [
#foreach($option in $amendment.M.options.L) {
"name": "$option.M.name.S"
}#if($foreach.hasNext),#end
#end
]
}#if($foreach.hasNext),#end
#end
],
"order": {
"id": "$car.order.M.id.S",
"options": [
#foreach($option in $car.order.M.options.L) {
"name": "$option.M.name.S"
}#if($foreach.hasNext),#end
#end
]
}
},
"model": {
"id": "$car.carModel.M.id.S"
}
}#if($foreach.hasNext),#end
#end
]`
14 changes: 13 additions & 1 deletion test/input.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
var assert = require('assert');
var mappingTemplate = require('../');
const fs = require("fs");
const path = require("path");
const complexTemplate = require("./fixtures/complexMapping");
const complexExpectedOutput = require("./fixtures/complexExpectedOutput");

describe('$input', function() {
describe('null object references', function () {
Expand Down Expand Up @@ -107,4 +111,12 @@ describe('$input', function() {
});
});
});
});

describe('Complex example', function () {
it('Handle nested foreach', function () {
const payload = fs.readFileSync(path.resolve(__dirname, 'fixtures', 'complexInput.json'))
const result = mappingTemplate({template: complexTemplate, payload});
assert.deepEqual(JSON.parse(result), complexExpectedOutput);
});
});
});