From 59e029becbf6ac561846866d3789a5111acc5f45 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 28 Nov 2023 17:17:41 +0100 Subject: [PATCH] Update function argument parsing for strings (#758) --- .../Parser/ExpressionParser.cs | 12 ++-- .../DynamicExpressionParserTests.cs | 59 +++++++++++++++++-- .../Helpers/Models/User.cs | 2 + 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs b/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs index 709622d0..88f498ad 100644 --- a/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs +++ b/src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs @@ -914,6 +914,13 @@ private AnyOf ParseStringLiteral(bool forceParseAsString) } } + // While the next token is also a string, keep concatenating these strings and get next token + while (_textParser.CurrentToken.Id == TokenId.StringLiteral) + { + stringValue += _textParser.CurrentToken.Text; + _textParser.NextToken(); + } + return ConstantExpressionHelper.CreateLiteral(stringValue, stringValue); } @@ -2188,11 +2195,6 @@ private Expression[] ParseArguments() _textParser.NextToken(); } - //if (argList.OfType().Count() > 1) - //{ - // throw ParseError(_textParser.CurrentToken.Pos, Res.OutVariableSingleRequired); - //} - return argList.ToArray(); } diff --git a/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs b/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs index 9dea54f5..82d987c1 100644 --- a/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs +++ b/test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs @@ -262,6 +262,11 @@ public static class StaticHelper { return Guid.NewGuid(); } + + public static string Filter(string filter) + { + return filter; + } } public class TestCustomTypeProvider : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider @@ -1379,14 +1384,58 @@ public void DynamicExpressionParser_ParseLambda_Operator_Less_Greater_With_Guids // Act var lambda = DynamicExpressionParser.ParseLambda(config, typeof(User), null, expressionText, user); - var guidLambda = lambda as Expression>; - Assert.NotNull(guidLambda); + var guidLambda = (Expression>)lambda; - var del = lambda.Compile(); - var result = (Guid)del.DynamicInvoke(user); + var del = guidLambda.Compile(); + var result = (Guid?)del.DynamicInvoke(user); // Assert - Assert.Equal(anotherId, result); + result.Should().Be(anotherId); + } + + [Fact] + public void DynamicExpressionParser_ParseLambda_CustomType_Method_With_ExpressionString() + { + // Arrange + var config = new ParsingConfig + { + CustomTypeProvider = new TestCustomTypeProvider() + }; + + var user = new User(); + + // Act : char + var expressionTextChar = "StaticHelper.Filter(\"C == 'x'\")"; + var lambdaChar = DynamicExpressionParser.ParseLambda(config, typeof(User), null, expressionTextChar, user); + var funcChar = (Expression>)lambdaChar; + + var delegateChar = funcChar.Compile(); + var resultChar = (string?)delegateChar.DynamicInvoke(user); + + // Assert : int + resultChar.Should().Be("C == 'x'"); + + // Act : int + var expressionTextIncome = "StaticHelper.Filter(\"Income == 5\")"; + var lambdaIncome = DynamicExpressionParser.ParseLambda(config, typeof(User), null, expressionTextIncome, user); + var funcIncome = (Expression>)lambdaIncome; + + var delegateIncome = funcIncome.Compile(); + var resultIncome = (string?)delegateIncome.DynamicInvoke(user); + + // Assert : int + resultIncome.Should().Be("Income == 5"); + + // Act : string + var expressionTextUserName = "StaticHelper.Filter(\"UserName == \"\"x\"\"\")"; + var lambdaUserName = DynamicExpressionParser.ParseLambda(config, typeof(User), null, expressionTextUserName, user); + var funcUserName = (Expression>)lambdaUserName; + + var delegateUserName = funcUserName.Compile(); + var resultUserName = (string?)delegateUserName.DynamicInvoke(user); + + // Assert : string + resultUserName.Should().Be(@"UserName == ""x"""""""); } [Theory] diff --git a/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs b/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs index b758d408..8b6bc5a9 100644 --- a/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs +++ b/test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs @@ -18,6 +18,8 @@ public class User public int Income { get; set; } + public char C { get; set; } + public UserProfile Profile { get; set; } public UserState State { get; set; }