Skip to content

Commit 1c1c9db

Browse files
committed
refactor: cleanup rightexpr
1 parent fc4b77e commit 1c1c9db

1 file changed

Lines changed: 25 additions & 33 deletions

File tree

QueryKit/FilterParser.cs

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
namespace QueryKit;
33

4-
using System.Collections;
54
using System.Globalization;
65
using System.Linq.Expressions;
76
using System.Reflection;
@@ -191,16 +190,14 @@ private static Expression CreateRightExpr(Expression leftExpr, string right)
191190
var dtCtor = typeof(DateTime).GetConstructor(new[] { typeof(long), typeof(DateTimeKind) });
192191
var newExpr = Expression.New(dtCtor, Expression.Constant(dt.Ticks), Expression.Constant(dt.Kind));
193192

194-
if (rawType == typeof(DateTime?))
195-
{
196-
var nullableDtCtor = typeof(DateTime?).GetConstructor(new[] { typeof(DateTime) });
197-
newExpr = Expression.New(nullableDtCtor, newExpr);
198-
}
193+
var isNullable = rawType == typeof(DateTime?);
194+
if (!isNullable) return newExpr;
199195

196+
var nullableDtCtor = typeof(DateTime?).GetConstructor(new[] { typeof(DateTime) });
197+
newExpr = Expression.New(nullableDtCtor, newExpr);
200198
return newExpr;
201199
}
202200

203-
204201
if (targetType == typeof(DateTimeOffset))
205202
{
206203
var dtStyle = right.EndsWith("Z") ? DateTimeStyles.AdjustToUniversal : DateTimeStyles.AssumeLocal;
@@ -209,12 +206,11 @@ private static Expression CreateRightExpr(Expression leftExpr, string right)
209206
var dtoCtor = typeof(DateTimeOffset).GetConstructor(new[] { typeof(long), typeof(TimeSpan) });
210207
var newExpr = Expression.New(dtoCtor, Expression.Constant(dto.Ticks), Expression.Constant(dto.Offset));
211208

212-
if (rawType == typeof(DateTimeOffset?))
213-
{
214-
var nullableDtoCtor = typeof(DateTimeOffset?).GetConstructor(new[] { typeof(DateTimeOffset) });
215-
newExpr = Expression.New(nullableDtoCtor, newExpr);
216-
}
217-
209+
var isNullable = rawType == typeof(DateTimeOffset?);
210+
if (!isNullable) return newExpr;
211+
212+
var nullableDtoCtor = typeof(DateTimeOffset?).GetConstructor(new[] { typeof(DateTimeOffset) });
213+
newExpr = Expression.New(nullableDtoCtor, newExpr);
218214
return newExpr;
219215
}
220216

@@ -224,12 +220,11 @@ private static Expression CreateRightExpr(Expression leftExpr, string right)
224220
var dateCtor = typeof(DateOnly).GetConstructor(new[] { typeof(int), typeof(int), typeof(int) });
225221
var newExpr = Expression.New(dateCtor, Expression.Constant(date.Year), Expression.Constant(date.Month), Expression.Constant(date.Day));
226222

227-
if (rawType == typeof(DateOnly?))
228-
{
229-
var nullableDateCtor = typeof(DateOnly?).GetConstructor(new[] { typeof(DateOnly) });
230-
newExpr = Expression.New(nullableDateCtor, newExpr);
231-
}
232-
223+
var isNullable = rawType == typeof(DateOnly?);
224+
if (!isNullable) return newExpr;
225+
226+
var nullableDateCtor = typeof(DateOnly?).GetConstructor(new[] { typeof(DateOnly) });
227+
newExpr = Expression.New(nullableDateCtor, newExpr);
233228
return newExpr;
234229
}
235230

@@ -238,40 +233,37 @@ private static Expression CreateRightExpr(Expression leftExpr, string right)
238233
var time = TimeOnly.Parse(right, CultureInfo.InvariantCulture);
239234

240235
int millisecond = 0, microsecond = 0;
241-
if (right.Contains("."))
236+
if (right.Contains('.'))
242237
{
243-
var fractionalSeconds = right.Split('.')[1]; // Extract fractional seconds part
238+
var fractionalSeconds = right.Split('.')[1];
244239
if (fractionalSeconds.Length >= 3)
245240
{
246-
millisecond = int.Parse(fractionalSeconds.Substring(0, 3)); // Extract milliseconds
241+
millisecond = int.Parse(fractionalSeconds.Substring(0, 3));
247242
}
248243
if (fractionalSeconds.Length >= 6)
249244
{
250-
microsecond = int.Parse(fractionalSeconds.Substring(3, 3)); // Extract microseconds
245+
microsecond = int.Parse(fractionalSeconds.Substring(3, 3));
251246
}
252247
}
253248

254249
var timeCtor = typeof(TimeOnly).GetConstructor(new[] { typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });
255250
var newExpr = Expression.New(timeCtor, Expression.Constant(time.Hour), Expression.Constant(time.Minute), Expression.Constant(time.Second), Expression.Constant(millisecond), Expression.Constant(microsecond));
256251

257-
if (rawType == typeof(TimeOnly?))
258-
{
259-
var nullableTimeCtor = typeof(TimeOnly?).GetConstructor(new[] { typeof(TimeOnly) });
260-
newExpr = Expression.New(nullableTimeCtor, newExpr);
261-
}
262-
252+
var isNullable = rawType == typeof(TimeOnly?);
253+
if (!isNullable) return newExpr;
254+
255+
var nullableTimeCtor = typeof(TimeOnly?).GetConstructor(new[] { typeof(TimeOnly) });
256+
newExpr = Expression.New(nullableTimeCtor, newExpr);
263257
return newExpr;
264258
}
265259

266-
267-
var convertedValue = conversionFunction(right);
268-
269260
if (targetType == typeof(Guid))
270261
{
271262
var guidParseMethod = typeof(Guid).GetMethod("Parse", new[] { typeof(string) });
272-
return Expression.Call(guidParseMethod, Expression.Constant(convertedValue.ToString(), typeof(string)));
263+
return Expression.Call(guidParseMethod, Expression.Constant(right, typeof(string)));
273264
}
274265

266+
var convertedValue = conversionFunction(right);
275267
return Expression.Constant(convertedValue, leftExpr.Type);
276268
}
277269

0 commit comments

Comments
 (0)