From 3e7a97f7e1e3d36ce0ddbd01ca84bdb2afe93ef7 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 27 Sep 2023 15:55:31 +0200 Subject: [PATCH 1/3] expandVar for objects with string in values --- lib/myutils.js | 93 +++++++++++++++++++--------------- test/component/myutils_test.js | 12 +++++ 2 files changed, 64 insertions(+), 41 deletions(-) diff --git a/lib/myutils.js b/lib/myutils.js index b235901b..072cf0ee 100644 --- a/lib/myutils.js +++ b/lib/myutils.js @@ -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 + 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') { diff --git a/test/component/myutils_test.js b/test/component/myutils_test.js index b9fdbd52..d8e25581 100644 --- a/test/component/myutils_test.js +++ b/test/component/myutils_test.js @@ -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() { From f744234c6d097ecf814630ab9702146567e2d0a5 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 27 Sep 2023 15:58:10 +0200 Subject: [PATCH 2/3] update CNR --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 1adb8572..40ff7dd5 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -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) From d8853ff760961808bab85107854c044222c4cc54 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Wed, 27 Sep 2023 16:25:26 +0200 Subject: [PATCH 3/3] add comment --- lib/myutils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/myutils.js b/lib/myutils.js index 072cf0ee..66271ef4 100644 --- a/lib/myutils.js +++ b/lib/myutils.js @@ -103,7 +103,7 @@ 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 + // 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;