-
Notifications
You must be signed in to change notification settings - Fork 91
Closed
Description
autocast feature is performed by
iotagent-node-lib/lib/services/ngsi/ngsiUtils.js
Lines 98 to 134 in 9557e75
| function castJsonNativeAttributes(payload) { | |
| if (!config.getConfig().autocast) { | |
| return payload; | |
| } | |
| for (const key in payload) { | |
| if ( | |
| /* eslint-disable-next-line no-prototype-builtins */ | |
| payload.hasOwnProperty(key) && | |
| payload[key].value && | |
| payload[key].type && | |
| typeof payload[key].value === 'string' | |
| ) { | |
| if (payload[key].type === 'Number' && isFloat(payload[key].value)) { | |
| payload[key].value = Number.parseFloat(payload[key].value); | |
| } else if (payload[key].type === 'Number' && Number.parseInt(payload[key].value)) { | |
| payload[key].value = Number.parseInt(payload[key].value); | |
| } else if (payload[key].type === 'Boolean') { | |
| payload[key].value = payload[key].value === 'true' || payload[key].value === '1'; | |
| } else if (payload[key].type === 'None') { | |
| payload[key].value = null; | |
| } else if (payload[key].type === 'Array' || payload[key].type === 'Object') { | |
| try { | |
| const parsedValue = JSON.parse(payload[key].value); | |
| payload[key].value = parsedValue; | |
| } catch (e) { | |
| logger.error( | |
| context, | |
| 'Bad attribute value type. Expecting JSON Array or JSON Object. Received:%s', | |
| payload[key].value | |
| ); | |
| } | |
| } | |
| } | |
| } | |
| return payload; | |
| } |
It was introduced by dcalvoalonso#7 in 2.7.0 release of iota-node-lib
Currently is performing casting based on some attribute types (Boolean, Number, None) which is not the best.
But instead of that (which is not a best approach) control cast per attribute is proposed:
{
"name": "myNumber",
"type": "MyCustomType",
"autocastType": "Number",
"autocast" : true
}
as was suggested in #1020 (comment)
(autocast feature is used by iotagent-ul, but not used by iotagent-json)
Metadata
Metadata
Assignees
Labels
No labels