Skip to content

Commit

Permalink
Merge pull request #4066 from microsoft/hond/concatlist
Browse files Browse the repository at this point in the history
[Expression]Enable list concatenation for function "concat"
  • Loading branch information
Tom Laird-McConnell authored Jun 17, 2020
2 parents cebef0c + 1347098 commit 98ae302
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
35 changes: 25 additions & 10 deletions libraries/AdaptiveExpressions/ExpressionFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3118,21 +3118,36 @@ private static IDictionary<string, ExpressionEvaluator> GetStandardFunctions()
// String
new ExpressionEvaluator(
ExpressionType.Concat,
Apply(
ApplySequence(
args =>
{
var builder = new StringBuilder();
foreach (var arg in args)
var firstItem = args[0];
var secondItem = args[1];
var isFirstList = TryParseList(firstItem, out var firstList);
var isSecondList = TryParseList(secondItem, out var secondList);
if (firstItem == null && secondItem == null)
{
if (arg != null)
{
builder.Append(arg.ToString());
}
return null;
}
else if (firstItem == null && isSecondList)
{
return secondList;
}
else if (secondItem == null && isFirstList)
{
return firstList;
}
else if (isFirstList && isSecondList)
{
return firstList.OfType<object>().Concat(secondList.OfType<object>()).ToList();
}
else
{
return $"{firstItem?.ToString()}{secondItem?.ToString()}";
}
return builder.ToString();
}),
ReturnType.String,
ReturnType.Array | ReturnType.String,
ValidateAtLeastOne),
new ExpressionEvaluator(
ExpressionType.Length,
Expand Down
3 changes: 3 additions & 0 deletions tests/AdaptiveExpressions.Tests/ExpressionParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,9 @@ public class ExpressionParserTests
Test("contains(items, 'hi')", false),
Test("contains(bag, 'three')", true),
Test("contains(bag, 'xxx')", false),
Test("concat(null, [1, 2], null)", new List<object> { 1, 2 }),
Test("concat(createArray(1, 2), createArray(3, 4))", new List<object> { 1, 2, 3, 4 }),
Test("concat(['a', 'b'], ['b', 'c'], ['c', 'd'])", new List<object> { "a", "b", "b", "c", "c", "d" }),
Test("count(split(hello,'e'))", 2),
Test("count(createArray('h', 'e', 'l', 'l', 'o'))", 5),
Test("empty('')", true),
Expand Down

0 comments on commit 98ae302

Please sign in to comment.