Skip to content

Commit ec2a810

Browse files
authored
Merge pull request #16 from pdevito3/develop
Develop
2 parents a59bf6c + a6b45bb commit ec2a810

11 files changed

Lines changed: 75 additions & 27 deletions

File tree

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -501,21 +501,21 @@ public async Task can_filter_complex_expression()
501501
var fakePersonOne = new FakeTestingPersonBuilder()
502502
.WithTitle("Waffle & Chicken")
503503
.WithAge(35)
504-
.WithBirthMonth("January")
504+
.WithBirthMonth(BirthMonthEnum.January)
505505
.WithRating(4.0M)
506506
.WithSpecificDate(new DateTime(2022, 07, 01, 00, 00, 03, DateTimeKind.Utc))
507507
.WithDate(DateOnly.FromDateTime(new DateTime(2022, 07, 01)))
508508
.Build();
509509
var fakePersonTwo = new FakeTestingPersonBuilder()
510510
.WithTitle("Lamb")
511511
.WithAge(17)
512-
.WithBirthMonth("February")
512+
.WithBirthMonth(BirthMonthEnum.February)
513513
.WithRating(3.4M)
514514
.WithSpecificDate(new DateTime(2022, 07, 01, 00, 00, 03, DateTimeKind.Utc))
515515
.Build();
516516
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
517517

518-
var input = $"""""((Title @=* "waffle & chicken" && Age > 30) || Id == "{fakePersonOne.Id}" || Title == "lamb" || Title == null) && (Age < 18 || (BirthMonth == "January" && Title _= "ally")) || Rating > 3.5 || SpecificDate == 2022-07-01T00:00:03Z && (Date == 2022-07-01 || Time == 00:00:03)""""";
518+
var input = $"""""((Title @=* "waffle & chicken" && Age > 30) || Id == "{fakePersonOne.Id}" || Title == "lamb" || Title == null) && (Age < 18 || (BirthMonth == 1 && Title _= "ally")) || Rating > 3.5 || SpecificDate == 2022-07-01T00:00:03Z && (Date == 2022-07-01 || Time == 00:00:03)""""";
519519

520520
// Act
521521
var queryablePeople = testingServiceScope.DbContext().People;

QueryKit.IntegrationTests/Tests/DatabaseSortingTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,22 @@ public async Task can_sort_items_with_mixed_order_directions()
1616
var fakePersonOne = new FakeTestingPersonBuilder()
1717
.WithTitle("alpha")
1818
.WithAge(10)
19-
.WithBirthMonth("January")
19+
.WithBirthMonth(BirthMonthEnum.January)
2020
.Build();
2121
var fakePersonTwo = new FakeTestingPersonBuilder()
2222
.WithTitle("beta")
2323
.WithAge(100)
24-
.WithBirthMonth("February")
24+
.WithBirthMonth(BirthMonthEnum.February)
2525
.Build();
2626
var fakePersonThree = new FakeTestingPersonBuilder()
2727
.WithTitle("beta")
2828
.WithAge(50)
29-
.WithBirthMonth("March")
29+
.WithBirthMonth(BirthMonthEnum.March)
3030
.Build();
3131
var fakePersonFour = new FakeTestingPersonBuilder()
3232
.WithTitle("beta")
3333
.WithAge(20)
34-
.WithBirthMonth("April")
34+
.WithBirthMonth(BirthMonthEnum.April)
3535
.Build();
3636
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo, fakePersonThree, fakePersonFour);
3737

@@ -177,12 +177,12 @@ public async Task can_sort_multiple_items_with_same_order_direction()
177177
var fakePersonOne = new FakeTestingPersonBuilder()
178178
.WithTitle("alpha")
179179
.WithAge(10)
180-
.WithBirthMonth("January")
180+
.WithBirthMonth(BirthMonthEnum.January)
181181
.Build();
182182
var fakePersonTwo = new FakeTestingPersonBuilder()
183183
.WithTitle("beta")
184184
.WithAge(100)
185-
.WithBirthMonth("February")
185+
.WithBirthMonth(BirthMonthEnum.February)
186186
.Build();
187187
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo);
188188

@@ -213,17 +213,17 @@ public async Task can_sort_items_with_null_value_property()
213213
var fakePersonTwo = new FakeTestingPersonBuilder()
214214
.WithTitle("alpha")
215215
.WithAge(100)
216-
.WithBirthMonth("January")
216+
.WithBirthMonth(BirthMonthEnum.January)
217217
.Build();
218218
var fakePersonThree = new FakeTestingPersonBuilder()
219219
.WithTitle("beta")
220220
.WithAge(50)
221-
.WithBirthMonth("March")
221+
.WithBirthMonth(BirthMonthEnum.March)
222222
.Build();
223223
var fakePersonFour = new FakeTestingPersonBuilder()
224224
.WithTitle("beta")
225225
.WithAge(20)
226-
.WithBirthMonth("February")
226+
.WithBirthMonth(BirthMonthEnum.February)
227227
.Build();
228228
await testingServiceScope.InsertAsync(fakePersonOne, fakePersonTwo, fakePersonThree, fakePersonFour);
229229

QueryKit.UnitTests/FilterParserTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public void escaped_double_quote()
3838
public void complex_with_lots_of_types()
3939
{
4040
var input =
41-
"""""((Title @=* "waffle & chicken" && Age > 30) || Id == "aa648248-cb69-4217-ac95-d7484795afb2" || Title == "lamb" || Title == null) && (Age < 18 || (BirthMonth == "January" && Title _= "ally")) || Rating > 3.5 || SpecificDate == 2022-07-01T00:00:03Z && (Date == 2022-07-01 || Time == 00:00:03)""""";
41+
"""""((Title @=* "waffle & chicken" && Age > 30) || Id == "aa648248-cb69-4217-ac95-d7484795afb2" || Title == "lamb" || Title == null) && (Age < 18 || (BirthMonth == 1 && Title _= "ally")) || Rating > 3.5 || SpecificDate == 2022-07-01T00:00:03Z && (Date == 2022-07-01 || Time == 00:00:03)""""";
4242

4343
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
4444
filterExpression.ToString().Should()
45-
.Be(""""x => (((((((x.Title.ToLower().Contains("waffle & chicken".ToLower()) AndAlso (x.Age > 30)) OrElse (x.Id == Parse("aa648248-cb69-4217-ac95-d7484795afb2"))) OrElse (x.Title == "lamb")) OrElse (x.Title == null)) AndAlso ((x.Age < 18) OrElse ((x.BirthMonth == "January") AndAlso x.Title.StartsWith("ally")))) OrElse (x.Rating > 3.5)) OrElse ((x.SpecificDate == new Nullable`1(new DateTimeOffset(637922304030000000, 00:00:00))) AndAlso ((x.Date == new Nullable`1(new DateOnly(2022, 7, 1))) OrElse (x.Time == new Nullable`1(new TimeOnly(0, 0, 3, 0, 0))))))"""");
45+
.Be(""""x => (((((((x.Title.ToLower().Contains("waffle & chicken".ToLower()) AndAlso (x.Age > 30)) OrElse (x.Id == Parse("aa648248-cb69-4217-ac95-d7484795afb2"))) OrElse (x.Title == "lamb")) OrElse (x.Title == null)) AndAlso ((x.Age < 18) OrElse ((x.BirthMonth == new Nullable`1(January)) AndAlso x.Title.StartsWith("ally")))) OrElse (x.Rating > 3.5)) OrElse ((x.SpecificDate == new Nullable`1(new DateTimeOffset(637922304030000000, 00:00:00))) AndAlso ((x.Date == new Nullable`1(new DateOnly(2022, 7, 1))) OrElse (x.Time == new Nullable`1(new TimeOnly(0, 0, 3, 0, 0))))))"""");
4646
}
4747

4848
[Fact]

QueryKit.WebApiTestProject/Entities/TestingPerson.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class TestingPerson
44
{
55
public string? Title { get; set; }
66
public int? Age { get; set; }
7-
public string? BirthMonth { get; set; }
7+
public BirthMonthEnum? BirthMonth { get; set; }
88
public decimal? Rating { get; set; }
99
public DateOnly? Date { get; set; }
1010
public bool? Favorite { get; set; }
@@ -14,4 +14,20 @@ public class TestingPerson
1414
public Guid Id { get; set; } = Guid.NewGuid();
1515
public EmailAddress Email { get; set; }
1616
public Address PhysicalAddress { get; set; }
17+
}
18+
19+
public enum BirthMonthEnum
20+
{
21+
January = 1,
22+
February = 2,
23+
March = 3,
24+
April = 4,
25+
May = 5,
26+
June = 6,
27+
July = 7,
28+
August = 8,
29+
September = 9,
30+
October = 10,
31+
November = 11,
32+
December = 12
1733
}

QueryKit.WebApiTestProject/Migrations/20230724020352_BaseTestingMigration.Designer.cs renamed to QueryKit.WebApiTestProject/Migrations/20231222014351_BaseTestingMigration.Designer.cs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

QueryKit.WebApiTestProject/Migrations/20230724020352_BaseTestingMigration.cs renamed to QueryKit.WebApiTestProject/Migrations/20231222014351_BaseTestingMigration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected override void Up(MigrationBuilder migrationBuilder)
2222
id = table.Column<Guid>(type: "uuid", nullable: false),
2323
title = table.Column<string>(type: "text", nullable: true),
2424
age = table.Column<int>(type: "integer", nullable: true),
25-
birth_month = table.Column<string>(type: "text", nullable: true),
25+
birth_month = table.Column<int>(type: "integer", nullable: true),
2626
rating = table.Column<decimal>(type: "numeric", nullable: true),
2727
date = table.Column<DateOnly>(type: "date", nullable: true),
2828
favorite = table.Column<bool>(type: "boolean", nullable: true),

QueryKit.WebApiTestProject/Migrations/TestingDbContextModelSnapshot.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1818
{
1919
#pragma warning disable 612, 618
2020
modelBuilder
21-
.HasAnnotation("ProductVersion", "7.0.5")
21+
.HasAnnotation("ProductVersion", "8.0.0")
2222
.HasAnnotation("Relational:MaxIdentifierLength", 63);
2323

2424
NpgsqlModelBuilderExtensions.HasPostgresExtension(modelBuilder, "fuzzystrmatch");
@@ -174,8 +174,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
174174
.HasColumnType("integer")
175175
.HasColumnName("age");
176176

177-
b.Property<string>("BirthMonth")
178-
.HasColumnType("text")
177+
b.Property<int?>("BirthMonth")
178+
.HasColumnType("integer")
179179
.HasColumnName("birth_month");
180180

181181
b.Property<DateOnly?>("Date")

QueryKit/FilterParser.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ from closingBracket in Parse.Char(']')
156156
{ typeof(ulong), value => ulong.Parse(value, CultureInfo.InvariantCulture) },
157157
{ typeof(ushort), value => ushort.Parse(value, CultureInfo.InvariantCulture) },
158158
{ typeof(sbyte), value => sbyte.Parse(value, CultureInfo.InvariantCulture) },
159-
// { typeof(Enum), value => Enum.Parse(typeof(T), value) },
160159
};
161160

162161
private static Expression CreateRightExpr(Expression leftExpr, string right)
@@ -181,7 +180,7 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ
181180
}
182181

183182
var rawType = targetType;
184-
183+
185184
targetType = TransformTargetTypeIfNullable(targetType);
186185

187186
if (TypeConversionFunctions.TryGetValue(targetType, out var conversionFunction))
@@ -305,6 +304,24 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ
305304
return Expression.Constant(convertedValue, leftExprType);
306305
}
307306

307+
if (rawType.IsEnum || (Nullable.GetUnderlyingType(rawType)?.IsEnum ?? false))
308+
{
309+
var enumType = Nullable.GetUnderlyingType(rawType) ?? rawType;
310+
311+
if (right == "null" && Nullable.GetUnderlyingType(rawType) != null)
312+
{
313+
return Expression.Constant(null, rawType);
314+
}
315+
316+
var enumValue = Enum.Parse(enumType, right);
317+
var constant = Expression.Constant(enumValue, enumType);
318+
319+
if (rawType == enumType) return constant;
320+
321+
var nullableCtor = rawType.GetConstructor(new[] {enumType});
322+
return Expression.New(nullableCtor, constant);
323+
}
324+
308325
throw new InvalidOperationException($"Unsupported value '{right}' for type '{targetType.Name}'");
309326
}
310327

QueryKit/QueryKit.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<PackageId>QueryKit</PackageId>
88
<PackageTags>QueryKit;Fitler;Filtering;Sort;Sorting</PackageTags>
9-
<Version>1.0.0</Version>
9+
<Version>1.1.0</Version>
1010
<Authors>Paul DeVito</Authors>
1111
<Summary>QueryKit is a .NET library that makes it easier to query your data by providing a fluent and intuitive syntax for filtering and sorting.</Summary>
1212
<Description>QueryKit is a .NET library that makes it easier to query your data by providing a fluent and intuitive syntax for filtering and sorting.</Description>

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ If you want to filter on the count of a collection, you can prefix some of the o
187187
var input = """"Ingredients #>= 0"""";
188188
```
189189

190+
#### Filtering Enums
191+
192+
You can filter enums with their respective integer value:
193+
194+
```csharp
195+
var input = "BirthMonth == 1";
196+
197+
public enum BirthMonthEnum
198+
{
199+
January = 1,
200+
February = 2,
201+
//...
202+
}
203+
```
204+
190205
### Settings
191206

192207
#### Property Settings

0 commit comments

Comments
 (0)