Skip to content

extend autocast feature to control each attribute #1037

Closed
@AlvaroVega

Description

@AlvaroVega

autocast feature is performed by

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions