Skip to content
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

[expression] add capability of handling null in string related built-in functions #1395

Merged
merged 2 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 62 additions & 15 deletions libraries/botframework-expressions/src/builtInFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ export class BuiltInFunctions {
return error;
}

public static verifyStringOrNull(value: any, expression: Expression, _: number): string {
let error: string;
if (typeof value !== 'string' && value !== undefined) {
error = `${expression} is neither a string nor a null object.`;
}

return error;
}

/**
* Verify value is a number or string.
* @param value alue to check.
Expand Down Expand Up @@ -549,7 +558,7 @@ export class BuiltInFunctions {
* @param func Function to apply.
*/
public static stringTransform(type: string, func: (arg0: ReadonlyArray<any>) => any): ExpressionEvaluator {
return new ExpressionEvaluator(type, BuiltInFunctions.apply(func, BuiltInFunctions.verifyString),
return new ExpressionEvaluator(type, BuiltInFunctions.apply(func, BuiltInFunctions.verifyStringOrNull),
ReturnType.String, BuiltInFunctions.validateUnaryString);
}

Expand Down Expand Up @@ -677,6 +686,14 @@ export class BuiltInFunctions {
});
}

private static parseStringOrNull(input: string | undefined): string {
if (typeof input === "string") {
return input;
} else {
return "";
}
}

private static validateAccessor(expression: Expression): void {
const children: Expression[] = expression.children;
if (children.length === 0
Expand Down Expand Up @@ -1160,8 +1177,10 @@ export class BuiltInFunctions {
result = str.substr(start, length);
}
}
} else if (str === undefined) {
result = "";
} else {
error = `${expression.children[0]} is not a string.`;
error = `${expression.children[0]} is neither a string nor a null object.`;
}
}

Expand Down Expand Up @@ -1858,52 +1877,80 @@ export class BuiltInFunctions {
BuiltInFunctions.validateUnary),
new ExpressionEvaluator(
ExpressionType.Concat,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => ''.concat(...args), BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => ''.concat(...args.map(arg => BuiltInFunctions.parseStringOrNull(arg))), BuiltInFunctions.verifyStringOrNull),
ReturnType.String,
BuiltInFunctions.validateString),
new ExpressionEvaluator(
ExpressionType.Length,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].length, BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => (BuiltInFunctions.parseStringOrNull(args[0])).length, BuiltInFunctions.verifyStringOrNull),
ReturnType.Number,
BuiltInFunctions.validateUnaryString),
new ExpressionEvaluator(
ExpressionType.Replace,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].split(args[1]).join(args[2]), BuiltInFunctions.verifyString),
BuiltInFunctions.applyWithError((
args: ReadonlyArray<any>) =>
{
let error = undefined;1
let result = undefined;
if (BuiltInFunctions.parseStringOrNull(args[1]).length === 0) {
error = `${args[1]} should be a string with length at least 1`;
}

if (error === undefined) {
result = BuiltInFunctions.parseStringOrNull(args[0]).split(BuiltInFunctions.parseStringOrNull(args[1])).join(BuiltInFunctions.parseStringOrNull(args[2]))
}

return {value: result, error};
}, BuiltInFunctions.verifyStringOrNull),
ReturnType.String,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 3, 3, ReturnType.String)),
new ExpressionEvaluator(
ExpressionType.ReplaceIgnoreCase,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].replace(new RegExp(args[1], 'gi'), args[2]), BuiltInFunctions.verifyString),
BuiltInFunctions.applyWithError((
args: ReadonlyArray<any>) =>
{
let error = undefined;
let result = undefined;
if (BuiltInFunctions.parseStringOrNull(args[1]).length === 0) {
error = `${args[1]} should be a string with length at least 1`;
}

if (error === undefined) {
result = BuiltInFunctions.parseStringOrNull(args[0]).replace(new RegExp(BuiltInFunctions.parseStringOrNull(args[1]), 'gi'), BuiltInFunctions.parseStringOrNull(args[2]));
}

return {value: result, error};
}, BuiltInFunctions.verifyStringOrNull),
ReturnType.String,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 3, 3, ReturnType.String)),
new ExpressionEvaluator(
ExpressionType.Split,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].split(args[1]), BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => BuiltInFunctions.parseStringOrNull(args[0]).split(BuiltInFunctions.parseStringOrNull(args[1])), BuiltInFunctions.verifyStringOrNull),
ReturnType.Object,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String)),
new ExpressionEvaluator(
ExpressionType.Substring,
BuiltInFunctions.substring,
ReturnType.String,
(expression: Expression): void => BuiltInFunctions.validateOrder(expression, [ReturnType.Number], ReturnType.String, ReturnType.Number)),
BuiltInFunctions.stringTransform(ExpressionType.ToLower, (args: ReadonlyArray<any>) => String(args[0]).toLowerCase()),
BuiltInFunctions.stringTransform(ExpressionType.ToUpper, (args: ReadonlyArray<any>) => String(args[0]).toUpperCase()),
BuiltInFunctions.stringTransform(ExpressionType.Trim, (args: ReadonlyArray<any>) => String(args[0]).trim()),
BuiltInFunctions.stringTransform(ExpressionType.ToLower, (args: ReadonlyArray<any>) => String(BuiltInFunctions.parseStringOrNull(args[0])).toLowerCase()),
BuiltInFunctions.stringTransform(ExpressionType.ToUpper, (args: ReadonlyArray<any>) => String(BuiltInFunctions.parseStringOrNull(args[0])).toUpperCase()),
BuiltInFunctions.stringTransform(ExpressionType.Trim, (args: ReadonlyArray<any>) => String(BuiltInFunctions.parseStringOrNull(args[0])).trim()),
new ExpressionEvaluator(
ExpressionType.StartsWith,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].startsWith(args[1]), BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => BuiltInFunctions.parseStringOrNull(args[0]).startsWith(BuiltInFunctions.parseStringOrNull(args[1])), BuiltInFunctions.verifyStringOrNull),
ReturnType.Boolean,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String)
),
new ExpressionEvaluator(
ExpressionType.EndsWith,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].endsWith(args[1]), BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => BuiltInFunctions.parseStringOrNull(args[0]).endsWith(BuiltInFunctions.parseStringOrNull(args[1])), BuiltInFunctions.verifyStringOrNull),
ReturnType.Boolean,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String)
),
new ExpressionEvaluator(
ExpressionType.CountWord,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].trim().split(/\s+/).length, BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => BuiltInFunctions.parseStringOrNull(args[0]).trim().split(/\s+/).length, BuiltInFunctions.verifyStringOrNull),
ReturnType.Number,
BuiltInFunctions.validateUnaryString
),
Expand All @@ -1921,13 +1968,13 @@ export class BuiltInFunctions {
),
new ExpressionEvaluator(
ExpressionType.IndexOf,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].indexOf(args[1]), BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => BuiltInFunctions.parseStringOrNull(args[0]).indexOf(BuiltInFunctions.parseStringOrNull(args[1])), BuiltInFunctions.verifyStringOrNull),
ReturnType.Number,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String)
),
new ExpressionEvaluator(
ExpressionType.LastIndexOf,
BuiltInFunctions.apply((args: ReadonlyArray<any>) => args[0].lastIndexOf(args[1]), BuiltInFunctions.verifyString),
BuiltInFunctions.apply((args: ReadonlyArray<any>) => BuiltInFunctions.parseStringOrNull(args[0]).lastIndexOf(BuiltInFunctions.parseStringOrNull(args[1])), BuiltInFunctions.verifyStringOrNull),
ReturnType.Number,
(expression: Expression): void => BuiltInFunctions.validateArityAndAnyType(expression, 2, 2, ReturnType.String)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const badExpressions =
"replace(one, 'l', 'k')", // replace only accept string parameter
"replace('hi', 1, 'k')", // replace only accept string parameter
"replace('hi', 'l', 1)", // replace only accept string parameter
"replace('hi', nullObj, 'k')", // replace oldValue must string length not less than 1
"replaceIgnoreCase(hello)", // replaceIgnoreCase need three parameters
"replaceIgnoreCase('HI', nullObj, 'k')", // replaceIgnoreCase oldValue must string length not less than 1
"replaceIgnoreCase(one, 'l', 'k')", // replaceIgnoreCase only accept string parameter
"replaceIgnoreCase('hi', 1, 'k')", // replaceIgnoreCase only accept string parameter
"replaceIgnoreCase('hi', 'l', 1)", // replaceIgnoreCase only accept string parameter
Expand Down
24 changes: 24 additions & 0 deletions libraries/botframework-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,59 @@ const dataSource = [

// String functions tests
["concat(hello,world)", "helloworld"],
["concat(hello,nullObj)", "hello"],
["concat('hello','world')", "helloworld"],
["concat(\"hello\",\"world\")", "helloworld"],
["length('hello')", 5],
["length(nullObj)", 0],
["length(\"hello\")", 5],
["length(concat(hello,world))", 10],
["count('hello')", 5],
["count(\"hello\")", 5],
["count(concat(hello,world))", 10],
["replace('hello', 'l', 'k')", "hekko"],
["replace('hello', 'L', 'k')", "hello"],
["replace(nullObj, 'L', 'k')", ""],
["replace('hello', 'L', nullObj)", "hello"],
["replace(\"hello'\", \"'\", '\"')", "hello\""],
["replace('hello\"', '\"', \"'\")", "hello'"],
["replace('hello\"', '\"', '\n')", "hello\n"],
["replace('hello\n', '\n', '\\\\')", "hello\\"],
["replace('hello\\\\', '\\\\', '\\\\\\\\')", "hello\\\\"],
["replaceIgnoreCase('hello', 'L', 'k')", "hekko"],
["replaceIgnoreCase(nullObj, 'L', 'k')", ""],
["replaceIgnoreCase('hello', 'L', nullObj)", "heo"],
["split('hello','e')", ["h", "llo"]],
["split(nullObj,'e')", [""]],
["split('hello',nullObj)", ["h", "e", "l", "l", "o"]],
["split(nullObj,nullObj)", []],
["substring('hello', 0, 5)", "hello"],
["substring('hello', 0, 3)", "hel"],
["substring('hello', 3)", "lo"],
["substring(nullObj, 3)", ""],
["substring(nullObj, 0, 3)", ""],
["substring('hello', 0, bag.index)", "hel"],
["toLower('UpCase')", "upcase"],
["toLower(nullObj)", ""],
["toUpper('lowercase')", "LOWERCASE"],
["toUpper(nullObj)", ""],
["toLower(toUpper('lowercase'))", "lowercase"],
["trim(' hello ')", "hello"],
["trim(nullObj)", ""],
["trim(' hello')", "hello"],
["trim('hello')", "hello"],
["endsWith('hello','o')", true],
["endsWith('hello','a')", false],
["endsWith(nullObj,'a')", false],
["endsWith(nullObj, nullObj)", true],
["endsWith('hello',nullObj)", true],
["endsWith(hello,'o')", true],
["endsWith(hello,'a')", false],
["startsWith('hello','h')", true],
["startsWith('hello','a')", false],
["startsWith(nullObj,'a')", false],
["startsWith(nullObj, nullObj)", true],
["startsWith('hello',nullObj)", true],
["countWord(hello)", 1],
["countWord(concat(hello, ' ', world))", 2],
["addOrdinal(11)", "11th"],
Expand All @@ -116,6 +136,10 @@ const dataSource = [
["indexOf(newGuid(), '-')", 8],
["indexOf(newGuid(), '-')", 8],
["indexOf(hello, '-')", -1],
["indexOf(nullObj, '-')", -1],
["indexOf(hello, nullObj)", 0],
["lastIndexOf(nullObj, '-')", -1],
["lastIndexOf(hello, nullObj)", 5],
["lastIndexOf(newGuid(), '-')", 23],
["lastIndexOf(newGuid(), '-')", 23],
["lastIndexOf(hello, '-')", -1],
Expand Down