Skip to content

Commit

Permalink
Merge pull request #752 from telefonicaid/task/expandVar_with_objects
Browse files Browse the repository at this point in the history
expandVar for JSON objects
  • Loading branch information
fgalan authored Sep 27, 2023
2 parents 9181b45 + d8853ff commit 11950e3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 41 deletions.
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

0 comments on commit 11950e3

Please sign in to comment.