Skip to content

Commit

Permalink
Merge pull request #376 from telefonicaid/task/disable_ngsiv2_type_ca…
Browse files Browse the repository at this point in the history
…st_values

Do not cast using NGSIv2 types in updateAction (version 2)
  • Loading branch information
fgalan committed Oct 17, 2019
2 parents 0f02d91 + b2b1106 commit 875c32d
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
@@ -1,3 +1,4 @@
- Add: castType (PERSEO_CAST_TYPE) to enable NGSIv2 type based casting (default is to use JSON native types in values)
- Add authentication config env vars (#349)
- Add full support for pagination in APIs /rules and /vrules (#364)
- Add Fiware-Total-Count header to response when count
Expand Down
3 changes: 2 additions & 1 deletion bin/perseo
Expand Up @@ -73,6 +73,7 @@ function loadConfiguration() {
'PERSEO_SMPP_ENABLED',
'PERSEO_NOTICES_PATH',
'PERSEO_RULES_PATH',
'PERSEO_CAST_TYPES'
'PERSEO_AUTHENTICATION_HOST',
'PERSEO_AUTHENTICATION_PORT',
'PERSEO_AUTHENTICATION_USER',
Expand Down Expand Up @@ -157,7 +158,7 @@ function loadConfiguration() {
config.smpp.enabled = process.env.PERSEO_SMPP_ENABLED || config.smpp.enabled;
config.endpoint.noticesPath = process.env.PERSEO_NOTICES_PATH || config.endpoint.noticesPath;
config.endpoint.rulesPath = process.env.PERSEO_RULES_PATH || config.endpoint.rulesPath;

config.castTypes = process.env.PERSEO_CAST_TYPES || config.castTypes;
config.authentication = config.authentication || {};
config.authentication.host = process.env.PERSEO_AUTHENTICATION_HOST || config.authentication.host;
config.authentication.port = process.env.PERSEO_AUTHENTICATION_PORT || config.authentication.port;
Expand Down
5 changes: 5 additions & 0 deletions config.js
Expand Up @@ -214,4 +214,9 @@ config.checkDB = {
*/
config.restBase = null;

/**
* Cast attribute values in updateAction using NGSIv2 types
*/
config.castTypes = false;

module.exports = config;
2 changes: 2 additions & 0 deletions documentation/configuration.md
Expand Up @@ -48,6 +48,7 @@ The following table shows the environment variables available for Perseo configu
| PERSEO_AUTHENTICATION_PORT | Port of the authentication endpoint |
| PERSEO_AUTHENTICATION_USER | User to perform authentication |
| PERSEO_AUTHENTICATION_PASSWORD | Password for the user to perform authentication |
| PERSEO_CAST_TYPE | If true, enable attribute value casting based in NGSIv2 attribute types if true. If false (default), the JSON native type for the attribute value is used. |

### Basic Configuration

Expand Down Expand Up @@ -101,5 +102,6 @@ Options for Authentication through PEP (for update action)
- `config.authentication.port`: port,
- `config.authentication.user`: provisioned user for CEP in Keystone
- `config.authentication.password`: provisioned password for CEP in Keystone
- `config.castTypes`: cast or not attribute values to expected type conform NGSIv2 (false by default)

URL format for mongoDB could be found at `http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html`
106 changes: 56 additions & 50 deletions lib/models/updateAction.js
Expand Up @@ -239,31 +239,34 @@ function processOptionParams(action, event) {
let theType = myutils.expandVar(attr.type, event);
// Metadata should be null or object
let theMeta = attr.metadata;
var date;

switch (theType) {
case 'Text':
theValue = theValue.toString();
break;
case 'Number':
theValue = parseFloat(theValue);
break;
case 'Boolean':
if (typeof theValue === 'string') {
theValue = theValue.toLowerCase().trim() === 'true';
}
break;
case 'DateTime':
if (parseInt(theValue).toString() === theValue) {
// Parse String with number (timestamp__ts in Perseo events)
theValue = parseInt(theValue);
}
date = new Date(theValue);
theValue = isNaN(date.getTime()) ? theValue : date.toISOString();
break;
case 'None':
theValue = null;
break;

if (config.castTypes) {
// Cast using NGSIv2 types
var date;
switch (theType) {
case 'Text':
theValue = theValue.toString();
break;
case 'Number':
theValue = parseFloat(theValue);
break;
case 'Boolean':
if (typeof theValue === 'string') {
theValue = theValue.toLowerCase().trim() === 'true';
}
break;
case 'DateTime':
if (parseInt(theValue).toString() === theValue) {
// Parse String with number (timestamp__ts in Perseo events)
theValue = parseInt(theValue);
}
date = new Date(theValue);
theValue = isNaN(date.getTime()) ? theValue : date.toISOString();
break;
case 'None':
theValue = null;
break;
}
}
var key = myutils.expandVar(attr.name, event);
changes[key] = {
Expand Down Expand Up @@ -298,31 +301,34 @@ function processUpdateOptionParams(action, entity) {

// Metadata should be null or object
let theMeta = attr.metadata;
var date;

switch (theType) {
case 'Text':
theValue = theValue.toString();
break;
case 'Number':
theValue = parseFloat(theValue);
break;
case 'Boolean':
if (typeof theValue === 'string') {
theValue = theValue.toLowerCase().trim() === 'true';
}
break;
case 'DateTime':
if (parseInt(theValue).toString() === theValue) {
// Parse String with number (timestamp__ts in Perseo events)
theValue = parseInt(theValue);
}
date = new Date(theValue);
theValue = isNaN(date.getTime()) ? theValue : date.toISOString();
break;
case 'None':
theValue = null;
break;

if (config.castTypes) {
// Cast using NGSIv2 types
var date;
switch (theType) {
case 'Text':
theValue = theValue.toString();
break;
case 'Number':
theValue = parseFloat(theValue);
break;
case 'Boolean':
if (typeof theValue === 'string') {
theValue = theValue.toLowerCase().trim() === 'true';
}
break;
case 'DateTime':
if (parseInt(theValue).toString() === theValue) {
// Parse String with number (timestamp__ts in Perseo events)
theValue = parseInt(theValue);
}
date = new Date(theValue);
theValue = isNaN(date.getTime()) ? theValue : date.toISOString();
break;
case 'None':
theValue = null;
break;
}
}
var key = attr.name;
options[key] = {
Expand Down
22 changes: 11 additions & 11 deletions test/unit/updateAction.js
Expand Up @@ -106,7 +106,7 @@ var action1 = {
{
name: 'isBool2',
type: 'Boolean',
value: 'TRUE'
value: 'true'
},
{
name: 'isBool3',
Expand All @@ -116,7 +116,7 @@ var action1 = {
{
name: 'isBool4',
type: 'Boolean',
value: 'False'
value: 'false'
},
{
name: 'isBool5',
Expand Down Expand Up @@ -156,7 +156,7 @@ var action1 = {
{
name: 'refNone',
type: '${refNoneType}',
value: 'futureNull'
value: null
},
{
name: 'refNone2',
Expand All @@ -182,7 +182,7 @@ var event1 = {
addressLocality: 'Stockholm',
laststatus: 'allright',
powerState: 'on',
stringDate: '2018-12-05T11:31:39.00Z',
stringDate: '2018-12-05T11:31:39.000Z',
stringDateMs: '1548843060657',
numberDateMs: 1548843229832,
subservice: '/',
Expand Down Expand Up @@ -212,23 +212,23 @@ var expectedChanges = {
type: 'Text'
},
textBoolLit: {
value: 'false',
value: false,
type: 'Text'
},
textNumberLit: {
value: '666',
value: 666,
type: 'Text'
},
textObjLit: {
value: '[object Object]',
value: { a: 1, b: 2 },
type: 'Text'
},
refNone: {
value: null,
type: 'None'
},
refNone2: {
value: null,
value: 123,
type: 'None'
},
refNone3: {
Expand All @@ -253,7 +253,7 @@ var expectedChanges = {
type: 'Boolean'
},
isBool5: {
value: false,
value: 'other',
type: 'Boolean'
},
isBool6: {
Expand Down Expand Up @@ -293,11 +293,11 @@ var expectedChanges = {
type: 'DateTime'
},
lastchange2: {
value: '2019-01-30T10:11:00.657Z',
value: 1548843060657,
type: 'DateTime'
},
lastchange3: {
value: '2019-01-30T10:13:49.832Z',
value: 1548843229832,
type: 'DateTime'
}
}
Expand Down

0 comments on commit 875c32d

Please sign in to comment.