Skip to content

Commit 5491bbf

Browse files
authored
support constant [] and {} expression (#1734)
1 parent edfb269 commit 5491bbf

File tree

20 files changed

+6422
-6248
lines changed

20 files changed

+6422
-6248
lines changed

libraries/adaptive-expressions/src/builtInFunction.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,46 @@ export class BuiltInFunctions {
16751675
return {value: result, error};
16761676
}
16771677

1678+
private static isEqual(args: any []): boolean {
1679+
if (args.length === 0 ) {
1680+
return false;
1681+
}
1682+
1683+
if (args[0] === undefined || args[0] === null) {
1684+
return args[1] === undefined || args[1] === null;
1685+
}
1686+
1687+
if (Array.isArray(args[0]) && args[0].length === 0 && Array.isArray(args[1]) && args[1].length === 0) {
1688+
return true;
1689+
}
1690+
1691+
if (BuiltInFunctions.getPropertyCount(args[0]) === 0 && BuiltInFunctions.getPropertyCount(args[1]) === 0) {
1692+
return true;
1693+
}
1694+
1695+
try
1696+
{
1697+
return args[0] === args[1];
1698+
}
1699+
catch
1700+
{
1701+
return false;
1702+
}
1703+
}
1704+
1705+
private static getPropertyCount(obj: any): number {
1706+
let count = -1;
1707+
if (!Array.isArray(obj)) {
1708+
if (obj instanceof Map) {
1709+
count = obj.size;
1710+
} else if (typeof obj === 'object') {
1711+
count = Object.keys(obj).length;
1712+
}
1713+
}
1714+
1715+
return count;
1716+
}
1717+
16781718
// tslint:disable-next-line: max-func-body-length
16791719
private static buildFunctionLookup(): Map<string, ExpressionEvaluator> {
16801720
// tslint:disable-next-line: no-unnecessary-local-variable
@@ -1883,10 +1923,10 @@ export class BuiltInFunctions {
18831923
(args: any []): boolean => args[0] <= args[1], BuiltInFunctions.validateBinaryNumberOrString, BuiltInFunctions.verifyNumberOrString),
18841924
BuiltInFunctions.comparison(
18851925
ExpressionType.Equal,
1886-
(args: any []): boolean => args[0] === args[1], BuiltInFunctions.validateBinary),
1926+
this.isEqual, BuiltInFunctions.validateBinary),
18871927
BuiltInFunctions.comparison(
18881928
ExpressionType.NotEqual,
1889-
(args: any []): boolean => args[0] !== args[1], BuiltInFunctions.validateBinary),
1929+
(args: any []): boolean => !this.isEqual(args), BuiltInFunctions.validateBinary),
18901930
BuiltInFunctions.comparison(
18911931
ExpressionType.GreaterThan,
18921932
(args: any []): boolean => args[0] > args[1], BuiltInFunctions.validateBinaryNumberOrString, BuiltInFunctions.verifyNumberOrString),

libraries/adaptive-expressions/src/parser/Expression.g4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ expression
1717

1818
primaryExpression
1919
: '(' expression ')' #parenthesisExp
20+
| CONSTANT #constantAtom
2021
| NUMBER #numericAtom
2122
| STRING #stringAtom
2223
| IDENTIFIER #idAtom
@@ -42,4 +43,6 @@ NEWLINE : '\r'? '\n' -> skip;
4243

4344
STRING : ('\'' (~'\'')* '\'') | ('"' (~'"')* '"');
4445

46+
CONSTANT : ('[' WHITESPACE* ']') | ('{' WHITESPACE* '}');
47+
4548
INVALID_TOKEN_DEFAULT_MODE : . ;

libraries/adaptive-expressions/src/parser/expressionEngine.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ export class ExpressionEngine implements ExpressionParserInterface {
117117
}
118118
}
119119

120+
public visitConstantAtom(context: ep.ConstantAtomContext): Expression {
121+
let text: string = context.text;
122+
if (text.startsWith('[') && text.endsWith(']')) {
123+
text = text.substr(1, text.length - 2).trim();
124+
if (text === '') {
125+
return new Constant([]);
126+
}
127+
}
128+
129+
if (text.startsWith('{') && text.endsWith('}')) {
130+
text = text.substr(1, text.length - 2).trim();
131+
if (text === '') {
132+
return new Constant({});
133+
}
134+
}
135+
136+
throw new Error(`Unrecognized constant: ${ text }`);
137+
}
138+
120139
protected defaultResult = (): Expression => new Constant('');
121140

122141
private readonly MakeExpression = (type: string, ...children: Expression[]): Expression =>

0 commit comments

Comments
 (0)