Skip to content

Commit d2bc276

Browse files
authored
Merge pull request #990 from telefonicaid/task/fix_proper_expression_plugin_parser_in_multientity_plugin
check and set proper expression plugin to use in multientity
2 parents edac17a + 0e07b09 commit d2bc276

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

CHANGES_NEXT_RELEASE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Fix: ensure use proper expression parser in multientity plugin (#989)
12
Fix: ensure registrationId is updated when defice with commands is updated
23
Add: return apikey in GET device if device was provisioned with explicit apikey (#977)
34
Add: allow update device `apikey` using Update (put) API (#978)

lib/plugins/expressionParser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727

2828
const Parser = require('jison').Parser;
2929
const errors = require('../errors');
30+
const logger = require('logops');
3031
const config = require('../commonConfig');
3132
const _ = require('underscore');
33+
const logContext = {
34+
op: 'IoTAgentNGSI.Expression'
35+
};
3236
const grammar = {
3337
lex: {
3438
rules: [
@@ -159,7 +163,7 @@ function applyExpression(expression, context, typeInformation) {
159163
for (let i = 0; i < substitutions.length; i++) {
160164
expressionResult = expressionResult.replace(substitutions[i].original, substitutions[i].value);
161165
}
162-
166+
logger.debug(logContext, 'applyExpression "[%j]" over "[%j]" result "[%j]" ', expression, context, expressionResult);
163167
return expressionResult;
164168
}
165169

lib/plugins/jexlParser.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function parse(expression, context, callback) {
4646

4747
try {
4848
result = jexl.evalSync(expression, context);
49-
logger.debug(logContext, 'parse expression result "[%j]" ', result);
49+
logger.debug(logContext, 'parse expression "[%j]" over "[%j]" result "[%j]" ', expression, context, result);
5050
} catch (e) {
5151
error = new errors.InvalidExpression(expression);
5252
if (callback) {
@@ -68,7 +68,10 @@ function extractContext(attributeList) {
6868
let value;
6969

7070
for (let i = 0; i < attributeList.length; i++) {
71-
if (!Number.isNaN(Number.parseFloat(attributeList[i].value)) && !Number.isInteger(Number.parseFloat(attributeList[i].value))) {
71+
if (
72+
!Number.isNaN(Number.parseFloat(attributeList[i].value)) &&
73+
!Number.isInteger(Number.parseFloat(attributeList[i].value))
74+
) {
7275
value = Number.parseFloat(attributeList[i].value);
7376
} else if (!Number.isNaN(Number.parseInt(attributeList[i].value))) {
7477
value = Number.parseInt(attributeList[i].value);
@@ -94,8 +97,9 @@ function extractContext(attributeList) {
9497

9598
function applyExpression(expression, context, typeInformation) {
9699
const result = jexl.evalSync(expression, context);
97-
logger.debug(logContext, 'applyExpression result "[%j]" ', result);
98-
return result;
100+
logger.debug(logContext, 'applyExpression "[%j]" over "[%j]" result "[%j]" ', expression, context, result);
101+
let expressionResult = result !== undefined ? result : expression;
102+
return expressionResult;
99103
}
100104

101105
function expressionApplier(context, typeInformation) {

lib/plugins/multiEntity.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828

2929
const _ = require('underscore');
3030
const constants = require('../constants');
31-
const parser = require('./expressionParser');
31+
const legacyParser = require('./expressionParser');
32+
const jexlParser = require('./jexlParser');
3233
const config = require('../commonConfig');
3334
/* eslint-disable-next-line no-unused-vars */
3435
const logger = require('logops');
@@ -40,6 +41,26 @@ const utils = require('./pluginUtils');
4041
/* eslint-disable-next-line no-unused-vars */
4142
const aliasPlugin = require('./attributeAlias');
4243

44+
45+
function checkJexl(typeInformation) {
46+
if (
47+
config.getConfig().defaultExpressionLanguage === 'jexl' &&
48+
typeInformation.expressionLanguage &&
49+
typeInformation.expressionLanguage !== 'legacy'
50+
) {
51+
return true;
52+
} else if (config.getConfig().defaultExpressionLanguage === 'jexl' && !typeInformation.expressionLanguage) {
53+
return true;
54+
} else if (
55+
config.getConfig().defaultExpressionLanguage === 'legacy' &&
56+
typeInformation.expressionLanguage &&
57+
typeInformation.expressionLanguage === 'jexl'
58+
) {
59+
return true;
60+
}
61+
return false;
62+
}
63+
4364
function hasEntityName(item) {
4465
return item.entity_name;
4566
}
@@ -109,7 +130,10 @@ function generateNewCEsNgsi1(entity, newEntities, entityTypes, typeInformation,
109130
let newEntityAttributes;
110131
let newEntityAttributeNames;
111132
let entityName;
112-
133+
let parser = legacyParser;
134+
if (checkJexl(typeInformation)) {
135+
parser = jexlParser;
136+
}
113137
function filterByEntityName(entityName) {
114138
return function (item) {
115139
return item.entity_name === entityName;
@@ -158,7 +182,10 @@ function generateNewCEsNgsi2(entity, newEntities, entityTypes, typeInformation,
158182
let newEntityAttributeNames;
159183
let newEntityAttributeObjectIds;
160184
let entityName;
161-
185+
let parser = legacyParser;
186+
if (checkJexl(typeInformation)) {
187+
parser = jexlParser;
188+
}
162189
function filterByEntityName(entityName) {
163190
return function (item) {
164191
return item.entity_name === entityName;

0 commit comments

Comments
 (0)