forked from beyond-code-github/LinqToQuerystring
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTreeNode.cs
More file actions
182 lines (153 loc) · 6.48 KB
/
TreeNode.cs
File metadata and controls
182 lines (153 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
namespace LinqToQuerystring.Core.TreeNodes.Base
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
public abstract class TreeNode : CommonTree, IComparable<TreeNode>
{
protected internal readonly Type inputType;
protected internal readonly IToken payload;
protected internal readonly TreeNodeFactory factory;
protected internal Context Context => this.factory.Context;
protected TreeNode(Type inputType, IToken payload, TreeNodeFactory treeNodeFactory)
: base(payload)
{
this.inputType = inputType;
this.payload = payload;
this.factory = treeNodeFactory;
}
/// <summary>
/// This hacky property overwrites the base property which has a bug when using tree rewrites
/// </summary>
protected IEnumerable<TreeNode> ChildNodes
{
get
{
var result = new List<TreeNode>();
if (base.ChildCount <= 0)
{
return result;
}
foreach (var child in base.Children)
{
var node = child as TreeNode;
if (node == null)
{
node = (TreeNode)this.factory.Create(new CommonToken(child.Type, child.Text));
var tree = child as CommonTree;
if (tree != null)
{
node.AddChildren(tree.Children);
}
}
result.Add(node);
}
return result;
}
}
public abstract Expression BuildLinqExpression(
IQueryable query, Expression expression, Expression item = null);
public virtual Expression BuildLinqExpressionWithComparison(IQueryable query, Expression expression, Expression item = null, Expression compareExpression = null)
{
return BuildLinqExpression(query, expression, item);
}
public virtual int CompareTo(TreeNode other)
{
return 0;
}
protected void NormalizeTypes(ref Expression leftSide, ref Expression rightSide)
{
var rightSideIsConstant = rightSide is ConstantExpression;
var leftSideIsConstant = leftSide is ConstantExpression;
if (rightSideIsConstant && leftSideIsConstant)
{
return;
}
if (rightSideIsConstant)
{
// If we are comparing to an object try to cast it to the same type as the constant
if (leftSide.Type == typeof(object))
{
leftSide = MapAndCast(leftSide, rightSide);
}
else
{
rightSide = MapAndCast(rightSide, leftSide);
}
}
if (leftSideIsConstant)
{
// If we are comparing to an object try to cast it to the same type as the constant
if (rightSide.Type == typeof(object))
{
rightSide = MapAndCast(rightSide, leftSide);
}
else
{
leftSide = MapAndCast(leftSide, rightSide);
}
}
}
private Expression MapAndCast(Expression from, Expression to)
{
var mapped = Context.TypeConversionMap(from.Type, to.Type);
if (mapped != from.Type)
{
from = CastIfNeeded(from, mapped);
}
return CastIfNeeded(from, to.Type);
}
protected Expression CastIfNeeded(Expression expression, Type type)
{
var converted = expression;
if (!type.IsAssignableFrom(expression.Type))
{
var convertToType = Context.TypeConversionMap(expression.Type, type);
if (convertToType.IsEnum && expression.Type == typeof(string))
{
}
converted = Expression.Convert(expression, convertToType);
}
return converted;
}
protected static Expression ApplyEnsuringNullablesHaveValues(Func<Expression, Expression, Expression> produces, Expression leftExpression, Expression rightExpression)
{
var leftExpressionIsNullable = (Nullable.GetUnderlyingType(leftExpression.Type) != null);
var rightExpressionIsNullable = (Nullable.GetUnderlyingType(rightExpression.Type) != null);
if (leftExpressionIsNullable && !rightExpressionIsNullable)
{
return Expression.AndAlso(
Expression.NotEqual(leftExpression, Expression.Constant(null)),
produces(Expression.Property(leftExpression, "Value"), rightExpression));
}
if (rightExpressionIsNullable && !leftExpressionIsNullable)
{
return Expression.AndAlso(
Expression.NotEqual(rightExpression, Expression.Constant(null)),
produces(leftExpression, Expression.Property(rightExpression, "Value")));
}
return produces(leftExpression, rightExpression);
}
protected static Expression ApplyWithNullAsValidAlternative(Func<Expression, Expression, Expression> produces, Expression leftExpression, Expression rightExpression)
{
var leftExpressionIsNullable = (Nullable.GetUnderlyingType(leftExpression.Type) != null);
var rightExpressionIsNullable = (Nullable.GetUnderlyingType(rightExpression.Type) != null);
if (leftExpressionIsNullable && !rightExpressionIsNullable)
{
return Expression.OrElse(
Expression.Equal(leftExpression, Expression.Constant(null)),
produces(Expression.Property(leftExpression, "Value"), rightExpression));
}
if (rightExpressionIsNullable && !leftExpressionIsNullable)
{
return Expression.OrElse(
Expression.Equal(rightExpression, Expression.Constant(null)),
produces(leftExpression, Expression.Property(rightExpression, "Value")));
}
return produces(leftExpression, rightExpression);
}
}
}