dotnet-dynamic-linq/PoweredSoft.DynamicLinq/Resolver/PathExpressionResolver.cs

191 lines
8.1 KiB
C#
Raw Normal View History

2018-04-10 21:08:27 -04:00
using PoweredSoft.DynamicLinq.Helpers;
using PoweredSoft.DynamicLinq.Parser;
2018-04-02 12:47:30 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
2018-04-10 21:08:27 -04:00
using System.Text;
2018-04-02 12:47:30 -04:00
2018-04-10 21:08:27 -04:00
namespace PoweredSoft.DynamicLinq.Resolver
2018-04-02 12:47:30 -04:00
{
public class PathExpressionResolver
{
2018-04-10 21:08:27 -04:00
public bool NullChecking { get; set; } = false;
public SelectCollectionHandling CollectionHandling { get; set; } = SelectCollectionHandling.LeaveAsIs;
public ExpressionParser Parser { get; protected set; }
public Expression Result { get; protected set; }
public PathExpressionResolver(ExpressionParser parser)
{
Parser = parser;
}
2018-04-10 21:08:27 -04:00
protected Expression CompileGroup(ExpressionParserPieceGroup group, bool nullChecking)
2018-04-10 20:00:40 -04:00
{
var expr = group.Parameter as Expression;
group.Pieces.ForEach(piece =>
{
expr = Expression.PropertyOrField(expr, piece.Name);
});
return expr;
}
public void Resolve()
{
Result = null;
// parse the expression.
2018-04-11 23:18:10 -04:00
if (!Parser.IsParsed)
Parser.Parse();
// group the piece by common parameters
var groups = Parser.GroupBySharedParameters();
2018-04-05 20:29:50 -04:00
Expression currentExpression = null;
2018-04-10 21:08:27 -04:00
foreach (var group in groups.Reversed())
{
var isLastGroup = groups.IndexOf(group) == groups.Count - 1;
2018-04-05 20:29:50 -04:00
if (currentExpression == null)
{
2018-04-10 21:08:27 -04:00
var groupExpression = CompileGroup(group, NullChecking);
2018-04-05 20:29:50 -04:00
var groupExpressionLambda = Expression.Lambda(groupExpression, group.Parameter);
if (group.Parent == null)
{
currentExpression = groupExpression;
if (NullChecking != false)
currentExpression = CheckNullOnFirstGroup(group, currentExpression);
currentExpression = Expression.Lambda(currentExpression, group.Parameter);
2018-04-10 20:29:43 -04:00
continue;
}
var parent = group.Parent;
2018-04-10 21:08:27 -04:00
var parentExpression = CompileGroup(parent, NullChecking);
2018-04-10 20:00:40 -04:00
// check null with where.
var isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(groupExpression);
2018-04-10 21:08:27 -04:00
if (NullChecking != false)
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup && !isSelectMany);
2018-04-10 20:00:40 -04:00
// the select expression.
if (isSelectMany)
2018-04-11 22:39:54 -04:00
{
var selectType = parent.GroupEnumerableType();
var selectExpression = Expression.Call(typeof(Enumerable), "SelectMany",
new Type[] { selectType, groupExpression.Type.GenericTypeArguments.First() },
parentExpression, groupExpressionLambda);
currentExpression = selectExpression;
}
else
{
var selectType = parent.GroupEnumerableType();
var selectExpression = Expression.Call(typeof(Enumerable), "Select",
new Type[] { selectType, groupExpression.Type },
parentExpression, groupExpressionLambda);
currentExpression = selectExpression;
}
}
else
{
if (group.Parent == null)
{
2018-04-10 21:08:27 -04:00
if (NullChecking != false)
2018-04-10 20:29:43 -04:00
currentExpression = CheckNullOnFirstGroup(group, currentExpression);
2018-04-05 20:29:50 -04:00
currentExpression = Expression.Lambda(currentExpression, group.Parameter);
2018-04-10 20:29:43 -04:00
continue;
}
var parent = group.Parent;
2018-04-10 21:08:27 -04:00
var parentExpression = CompileGroup(parent, NullChecking);
var selectType = parent.GroupEnumerableType();
bool isSelectMany = CollectionHandling == SelectCollectionHandling.Flatten && QueryableHelpers.IsGenericEnumerable(currentExpression);
2018-04-10 21:08:27 -04:00
if (NullChecking != false)
parentExpression = CheckNullOnEnumerableParent(group, parent, parentExpression, isLastGroup && !isSelectMany);
if (isSelectMany)
2018-04-11 22:39:54 -04:00
{
var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter);
currentExpression = Expression.Call(typeof(Enumerable), "SelectMany",
new Type[] { selectType, currentExpression.Type.GenericTypeArguments.First() },
parentExpression, currentExpressionLambda);
}
else
{
var currentExpressionLambda = Expression.Lambda(currentExpression, group.Parameter);
currentExpression = Expression.Call(typeof(Enumerable), "Select",
new Type[] { selectType, currentExpression.Type },
parentExpression, currentExpressionLambda);
}
}
2018-04-10 20:29:43 -04:00
}
2018-04-10 19:26:44 -04:00
Result = currentExpression;
}
2018-04-10 20:29:43 -04:00
private Expression CheckNullOnFirstGroup(ExpressionParserPieceGroup group, Expression currentExpression)
{
var path = string.Join(".", group.Pieces.Select(t => t.Name));
var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path,
ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
2018-04-10 20:29:43 -04:00
parameter: group.Parameter, nullChecking: true);
var whereBodyExpression = (whereExpression as LambdaExpression).Body;
whereBodyExpression =Expression.Not(whereBodyExpression);
2018-04-10 20:29:43 -04:00
2018-04-10 21:08:27 -04:00
var nullType = currentExpression.Type;
2018-04-10 20:29:43 -04:00
Expression ifTrueExpression = null;
2018-04-10 21:08:27 -04:00
if (QueryableHelpers.IsGenericEnumerable(nullType))
2018-04-10 20:29:43 -04:00
{
2018-04-10 21:08:27 -04:00
var listType = typeof(List<>).MakeGenericType(nullType.GenericTypeArguments.First());
2018-04-10 20:29:43 -04:00
ifTrueExpression = Expression.New(listType);
}
else
{
ifTrueExpression = Expression.Default(nullType);
2018-04-10 20:29:43 -04:00
}
return Expression.Condition(whereBodyExpression, ifTrueExpression, currentExpression, currentExpression.Type);
}
private static Expression CheckNullOnEnumerableParent(ExpressionParserPieceGroup group, ExpressionParserPieceGroup parent, Expression parentExpression, bool shouldSkipLast)
2018-04-10 20:29:43 -04:00
{
2018-04-10 21:08:27 -04:00
string path = null;
if (shouldSkipLast)
2018-04-10 21:08:27 -04:00
path = string.Join(".", group.Pieces.Take(group.Pieces.Count - 1).Select(t => t.Name));
else
path = string.Join(".", group.Pieces.Select(t => t.Name));
if (!string.IsNullOrEmpty(path))
2018-04-10 20:29:43 -04:00
{
var whereExpression = QueryableHelpers.CreateConditionExpression(group.Parameter.Type, path,
ConditionOperators.NotEqual, null, QueryConvertStrategy.ConvertConstantToComparedPropertyOrField,
parameter: group.Parameter, nullChecking: true);
//public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
parentExpression = Expression.Call(typeof(Enumerable), "Where",
new Type[] { parent.GroupEnumerableType() },
parentExpression, whereExpression);
}
return parentExpression;
}
2018-04-11 23:18:10 -04:00
public Expression GetResultBodyExpression()
{
if (Result == null)
return Result;
return ((LambdaExpression)Result).Body;
}
2018-04-02 12:47:30 -04:00
}
}