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

expandVar for JSON objects #752

Merged
merged 3 commits into from
Sep 27, 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
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Add: allow use expandVar with JSON objects (#703)
- Add: apply expandVar with JSON.parse to all fields of all actions (sms, smpp, email, post, update) (#746)
- Fix: check domain before access domain
- Fix: expandVar return a 'null' instead of null (#746)
Expand Down
93 changes: 52 additions & 41 deletions lib/myutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,67 @@ function logErrorIf(err, message, context) {
}
}

function expandVar(val, mapping, trycast) {
if (typeof val === 'string') {
var expanded = false;
var valObj = null;
var uniqueString = false;
Object.keys(mapping).forEach(function(p) {
if (typeof mapping[p] === 'string' && Object.keys(mapping).length === 1) {
uniqueString = true;
}
val = val.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]);
// javascript replace always return a string: we need to keep object value (#692)
if (!expanded && val === '[object Object]') {
valObj = mapping[p]; // for cases where mapping[p] is an object like { type: 'Point', coordinates: [] }
expanded = true;
}
});
if (valObj) {
val = valObj;
function expandVarString(val, mapping, trycast) {
var expanded = false;
var valObj = null;
var uniqueString = false;
Object.keys(mapping).forEach(function(p) {
if (typeof mapping[p] === 'string' && Object.keys(mapping).length === 1) {
uniqueString = true;
}
if (trycast && !uniqueString) {
try {
// Check if "${num}" and expand it as real value, non string (#362, #369)
var n = JSON.parse(val);
switch (typeof n) {
case 'number':
case 'boolean':
case 'object': // for a json in a string like: "{\"type\":\"Point\"}"
val = n;
expanded = true;
break;
// default: keep val value
}
} catch (e) {
// keep val value
val = val.replace(new RegExp('\\$\\{' + p + '\\}', 'g'), mapping[p]);
// javascript replace always return a string: we need to keep object value (#692)
if (!expanded && val === '[object Object]') {
valObj = mapping[p]; // for cases where mapping[p] is an object like { type: 'Point', coordinates: [] }
expanded = true;
}
});
if (valObj) {
val = valObj;
}
if (trycast && !uniqueString) {
try {
// Check if "${num}" and expand it as real value, non string (#362, #369)
var n = JSON.parse(val);
switch (typeof n) {
case 'number':
case 'boolean':
case 'object': // for a json in a string like: "{\"type\":\"Point\"}"
val = n;
expanded = true;
break;
// default: keep val value
}
} catch (e) {
// keep val value
}
if (!expanded) {
if (val !== 'null' || !uniqueString) {
// Set to 'null' all non expanded values (#469)
val = val.replace(/\$\{\w*\}/g, 'null');
if (val === 'null') {
// val is just one value (non string value composes of several values) (#746)
val = null;
}
}
if (!expanded) {
if (val !== 'null' || !uniqueString) {
// Set to 'null' all non expanded values (#469)
val = val.replace(/\$\{\w*\}/g, 'null');
if (val === 'null') {
// val is just one value (non string value composes of several values) (#746)
val = null;
}
}
}
return val;
}

function expandVar(val, mapping, trycast) {
if (typeof val === 'string') {
return expandVarString(val, mapping, trycast);
} else if (typeof val === 'object' && val !== null) {
// replace in each key value when a JSON object (#703)
Object.keys(val).forEach((key) => {
var v = expandVar(val[key], mapping, trycast);
val[key] = v;
});
}
return val;
}

function expandObject(templateObj, dictionary) {
var res = {};
if (templateObj && typeof templateObj === 'object') {
Expand Down
12 changes: 12 additions & 0 deletions test/component/myutils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ describe('Myutils', function() {
newStr.should.be.equal('666123123');
});
});
describe('When there are variables which is a string number to expand in a json object', function() {
it('should expand json object key values', function() {
var str = { type: 'Point', coordinates: ['${Lat}', '${Lon}'] },
map = { Lat: '22', Lon: '33' },
newStr;
newStr = myutils.expandVar(str, map, true);
should.exist(newStr);
newStr.type.should.be.equal('Point');
newStr.coordinates[0].should.be.equal(22);
newStr.coordinates[1].should.be.equal(33);
});
});
});
describe('#RequestHelper()', function() {
describe('When there is a network problem', function() {
Expand Down
Loading