-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathQueryKitPropertyMappings.cs
More file actions
217 lines (187 loc) · 8.5 KB
/
Copy pathQueryKitPropertyMappings.cs
File metadata and controls
217 lines (187 loc) · 8.5 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
namespace QueryKit;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using Operators;
public class QueryKitPropertyMappings
{
private readonly Dictionary<string, QueryKitPropertyInfo> _propertyMappings = new();
private readonly Dictionary<string, QueryKitPropertyInfo> _derivedPropertyMappings = new();
internal IReadOnlyDictionary<string, QueryKitPropertyInfo> PropertyMappings => _propertyMappings;
internal IReadOnlyDictionary<string, QueryKitPropertyInfo> DerivedPropertyMappings => _derivedPropertyMappings;
public QueryKitPropertyMapping<TModel> Property<TModel>(Expression<Func<TModel, object>>? propertySelector)
{
var fullPath = GetFullPropertyPath(propertySelector);
var propertyInfo = new QueryKitPropertyInfo
{
Name = fullPath,
CanFilter = true,
CanSort = true,
QueryName = fullPath
};
_propertyMappings[fullPath] = propertyInfo;
return new QueryKitPropertyMapping<TModel>(propertyInfo);
}
public QueryKitPropertyMapping<TModel> DerivedProperty<TModel>(Expression<Func<TModel, object>>? propertySelector)
{
var fullPath = GetFullPropertyPath(propertySelector);
if (propertySelector == null)
throw new ArgumentNullException(nameof(propertySelector));
if(propertySelector.NodeType != ExpressionType.Lambda)
throw new ArgumentException("Property selector must be a lambda expression", nameof(propertySelector));
var body = propertySelector.Body;
var propertyInfo = new QueryKitPropertyInfo
{
Name = fullPath,
CanFilter = true,
CanSort = true,
QueryName = fullPath,
DerivedExpression = body
};
_derivedPropertyMappings[fullPath] = propertyInfo;
return new QueryKitPropertyMapping<TModel>(propertyInfo);
}
public string ReplaceAliasesWithPropertyPaths(string input)
{
var operators = ComparisonOperator.List.Select(x => x.Operator()).ToList();
foreach (var alias in _propertyMappings.Values)
{
var propertyPath = GetPropertyPathByQueryName(alias.QueryName);
if (!string.IsNullOrEmpty(propertyPath))
{
foreach (var op in operators)
{
// Use regular expression to isolate left side of the expression
var regex = new Regex($@"\b{alias.QueryName}\b(?=\s*{op})", RegexOptions.IgnoreCase);
input = regex.Replace(input, propertyPath);
}
}
}
// foreach (var alias in _derivedPropertyMappings.Values)
// {
// foreach (var op in operators)
// {
// // Use regular expression to isolate left side of the expression
// var regex = new Regex($@"\b{alias.QueryName}\b(?=\s*{op})", RegexOptions.IgnoreCase);
// input = regex.Replace(input, $"~||~||~{alias.QueryName}");
// }
// }
return input;
}
private static string GetFullPropertyPath(Expression? expression)
{
if (expression == null)
throw new ArgumentNullException(nameof(expression));
switch (expression.NodeType)
{
case ExpressionType.Call:
var call = (MethodCallExpression)expression;
if (call.Method.DeclaringType == typeof(Enumerable) && call.Method.Name == "Select" ||
call.Method.DeclaringType == typeof(Queryable) && call.Method.Name == "Select" ||
call.Method.DeclaringType == typeof(Enumerable) && call.Method.Name == "SelectMany" ||
call.Method.DeclaringType == typeof(Queryable) && call.Method.Name == "SelectMany")
{
var propertyPath = GetFullPropertyPath(call.Arguments[1]);
var prevPath = GetFullPropertyPath(call.Arguments[0]);
return $"{prevPath}.{propertyPath}";
}
break;
case ExpressionType.Lambda:
var lambda = (LambdaExpression)expression;
return GetFullPropertyPath(lambda.Body);
case ExpressionType.Convert:
var unary = (UnaryExpression)expression;
return GetFullPropertyPath(unary.Operand);
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression)expression;
return memberExpression?.Expression?.NodeType == ExpressionType.Parameter
? memberExpression.Member.Name
: $"{GetFullPropertyPath(memberExpression?.Expression)}.{memberExpression?.Member?.Name}";
case ExpressionType.Add:
case ExpressionType.Subtract:
case ExpressionType.Multiply:
case ExpressionType.Divide:
case ExpressionType.Modulo:
case ExpressionType.And:
case ExpressionType.Or:
case ExpressionType.AndAlso:
case ExpressionType.OrElse:
case ExpressionType.GreaterThan:
case ExpressionType.LessThan:
case ExpressionType.GreaterThanOrEqual:
case ExpressionType.LessThanOrEqual:
case ExpressionType.Equal:
var binary = (BinaryExpression)expression;
var left = GetFullPropertyPath(binary.Left);
var right = GetFullPropertyPath(binary.Right);
var op = GetOperator(binary.NodeType);
return $"{left} {op} {right}";
case ExpressionType.Constant:
var constant = (ConstantExpression)expression;
return constant.Value?.ToString() ?? "";
default:
throw new NotSupportedException($"Expression type '{expression.NodeType}' is not supported.");
}
throw new NotSupportedException($"Expression type '{expression.NodeType}' is not supported.");
}
private static string GetOperator(ExpressionType nodeType)
{
return nodeType switch
{
ExpressionType.Add => "+",
ExpressionType.Subtract => "-",
ExpressionType.Multiply => "*",
ExpressionType.Divide => "/",
ExpressionType.Modulo => "%",
ExpressionType.And => "&",
ExpressionType.Or => "|",
ExpressionType.AndAlso => "&&",
ExpressionType.OrElse => "||",
ExpressionType.GreaterThan => ">",
ExpressionType.LessThan => "<",
ExpressionType.GreaterThanOrEqual => ">=",
ExpressionType.LessThanOrEqual => "<=",
ExpressionType.Equal => "==",
_ => throw new NotSupportedException($"Operator for expression type '{nodeType}' is not supported.")
};
}
public QueryKitPropertyInfo? GetPropertyInfo(string? propertyName)
=> _propertyMappings.TryGetValue(propertyName, out var info) ? info : null;
public QueryKitPropertyInfo? GetPropertyInfoByQueryName(string? queryName)
=> _propertyMappings.Values.FirstOrDefault(info => info.QueryName != null && info.QueryName.Equals(queryName, StringComparison.InvariantCultureIgnoreCase));
public QueryKitPropertyInfo? GetDerivedPropertyInfoByQueryName(string? queryName)
=> _derivedPropertyMappings.Values.FirstOrDefault(info => info.QueryName != null && info.QueryName.Equals(queryName, StringComparison.InvariantCultureIgnoreCase));
public string? GetPropertyPathByQueryName(string? queryName)
=> GetPropertyInfoByQueryName(queryName)?.Name ?? null;
}
public class QueryKitPropertyMapping<TModel>
{
private readonly QueryKitPropertyInfo _propertyInfo;
public QueryKitPropertyMapping(QueryKitPropertyInfo propertyInfo)
{
_propertyInfo = propertyInfo;
}
public QueryKitPropertyMapping<TModel> PreventFilter()
{
_propertyInfo.CanFilter = false;
return this;
}
public QueryKitPropertyMapping<TModel> PreventSort()
{
_propertyInfo.CanSort = false;
return this;
}
public QueryKitPropertyMapping<TModel> HasQueryName(string queryName)
{
_propertyInfo.QueryName = queryName;
return this;
}
}
public class QueryKitPropertyInfo
{
public string? Name { get; set; }
public bool CanFilter { get; set; }
public bool CanSort { get; set; }
public string? QueryName { get; set; }
internal Expression DerivedExpression { get; set; }
}