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

Feature: Added support for implicit type conversions #166

Merged
merged 5 commits into from
May 26, 2018
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
21 changes: 20 additions & 1 deletion src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,21 @@ Expression ParseComparisonOperator()
}
}


if (!typesAreSameAndImplementCorrectInterface)
{
CheckAndPromoteOperands(isEquality ? typeof(IEqualitySignatures) : typeof(IRelationalSignatures), op.Text, ref left, ref right, op.Pos);
if (left.Type.GetTypeInfo().IsClass && right is ConstantExpression && HasImplicitConversion(left.Type, right.Type))
{
left = Expression.Convert(left, right.Type);
}
else if (right.Type.GetTypeInfo().IsClass && left is ConstantExpression && HasImplicitConversion(right.Type, left.Type))
{
right = Expression.Convert(right, left.Type);
}
else
{
CheckAndPromoteOperands(isEquality ? typeof(IEqualitySignatures) : typeof(IRelationalSignatures), op.Text, ref left, ref right, op.Pos);
}
}
}

Expand Down Expand Up @@ -533,6 +545,13 @@ Expression ParseComparisonOperator()
return left;
}

private bool HasImplicitConversion(Type baseType, Type targetType)
{
return baseType.GetMethods(BindingFlags.Public | BindingFlags.Static)
.Where(mi => mi.Name == "op_Implicit" && mi.ReturnType == targetType)
.Any(mi => mi.GetParameters().FirstOrDefault()?.ParameterType == baseType);
}

private ConstantExpression ParseEnumToConstantExpression(int pos, Type leftType, ConstantExpression constantExpr)
{
return Expression.Constant(ParseConstantExpressionToEnum(pos, leftType, constantExpr), leftType);
Expand Down
28 changes: 28 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,34 @@ public void ExpressionTests_SkipAndTake()
}
}

[Fact]
public void ExpressionTests_ImplicitCast()
{
//Arrange
Guid code1 = new Guid("651E08E3-85B1-42D1-80AF-68E28E2B7DA6");
Guid code2 = new Guid("6451FEB2-3226-41D0-961C-B72B7B5A0157");

var samples = User.GenerateSampleModels(3);
samples[0].State = new UserState() { StatusCode = code1, Description = "alive" };
samples[1].State = new UserState() { StatusCode = code2, Description = "deceased" };

//Act
string queryString = "State == @0";
IList<User> result = samples.AsQueryable().Where(queryString, code1).ToList();

string queryString2 = "@0 == State";
IList<User> result2 = samples.AsQueryable().Where(queryString2, code1).ToList();

//Assert
Assert.Equal(1, result.Count);
Assert.Equal(code1, result[0].State.StatusCode);
Assert.Equal("alive", result[0].State.Description);

Assert.Equal(1, result2.Count);
Assert.Equal(code1, result2[0].State.StatusCode);
Assert.Equal("alive", result2[0].State.Description);
}

[Fact]
public void ExpressionTests_StringCompare()
{
Expand Down
4 changes: 3 additions & 1 deletion test/System.Linq.Dynamic.Core.Tests/Helpers/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class User

public UserProfile Profile { get; set; }

public UserState State { get; set; }

public List<Role> Roles { get; set; }

public bool TestMethod1()
Expand Down Expand Up @@ -62,4 +64,4 @@ public static IList<User> GenerateSampleModels(int total, bool allowNullableProf
return list.ToArray();
}
}
}
}
15 changes: 15 additions & 0 deletions test/System.Linq.Dynamic.Core.Tests/Helpers/Models/UserState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace System.Linq.Dynamic.Core.Tests.Helpers.Models
{
public class UserState
{
public Guid StatusCode { get; set; }
public string Description { get; set; }

public static implicit operator Guid(UserState state)
=> state?.StatusCode ?? Guid.Empty;
}
}