Skip to content

extend autocast feature to control each attribute #1037

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

Closed
AlvaroVega opened this issue May 12, 2021 · 4 comments
Closed

extend autocast feature to control each attribute #1037

AlvaroVega opened this issue May 12, 2021 · 4 comments

Comments

@AlvaroVega
Copy link
Member

AlvaroVega commented May 12, 2021

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)

@fgalan
Copy link
Member

fgalan commented May 12, 2021

As follow up of this issue, the current autocast feature in IOTA-UL (IOTA_AUTOCAST env var and associated config.js setting) should be removed. It makes no sense to keep both system at the same time, it can be confusing for users.

@AlvaroVega AlvaroVega pinned this issue May 27, 2021
@Sunny-NEC
Copy link

Sunny-NEC commented Sep 17, 2021

@AlvaroVega I am interested to work in this issue.
As per my understanding , In castJsonNativeAttributes casting take place on the basis of type.
and we have to extend the castJsonNativeAttributes or do casting on the basis of ( name, autocastType) attribute too.
Please confirm.

@AlvaroVega
Copy link
Member Author

autocast should be done in base of proposed "autocastType" field and "autocast" flag of each attribute, possible in castJsonNativeAttributes or in another new function

@fgalan
Copy link
Member

fgalan commented Sep 28, 2021

@Sunny-NEC thanks for your willingness to work on this issue! Please go ahead and create the pull request with your contribution, so we can evaluate and provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants