Skip to content

Commit

Permalink
Fix exception on method call in the custom prop mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 committed Jul 30, 2024
1 parent 4b5d0cb commit e935295
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 7 additions & 5 deletions Ctoss.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
.Entity<ExampleNumericEntity>()
.Property("virtual", x =>
(x.SubEntity == null ? -1 : x.SubEntity.A) + (x.SubEntity == null ? -1 : x.SubEntity.B))
.Property("mc", x => int.Parse(x.A.ToString()))
.Apply()
.Entity<ExampleTextEntity>()
.Property(x => x.TextField, settings => { settings.IgnoreCase = true;})
.Property(x => x.TextField, settings => { settings.IgnoreCase = true; })
.Apply();

const string jsonFilter =
Expand Down Expand Up @@ -43,7 +44,7 @@
const string jsonNumericFilter =
"""
{
"virtual": {
"mc": {
"filterType": "number",
"condition1": {
"filterType": "number",
Expand Down Expand Up @@ -116,7 +117,8 @@
.WithPagination(1, 10)
.ToList();

foreach (var entity in numericEntities) Console.WriteLine($"A: {entity.A}, B: {entity.B}, SubEntity = ({entity.SubEntity.A + entity.SubEntity.B})");
foreach (var entity in numericEntities)
Console.WriteLine($"A: {entity.A}, B: {entity.B}, SubEntity = ({entity.SubEntity.A + entity.SubEntity.B})");

Check warning on line 121 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 121 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 121 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 121 in Ctoss.Example/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Console.WriteLine("\nText entities:");

Expand All @@ -125,8 +127,8 @@
TextField = "abc"
};

var textEntities = new List<ExampleTextEntity> {textEntity}.AsQueryable()
var textEntities = new List<ExampleTextEntity> { textEntity }.AsQueryable()
.WithFilter(jsonTextFilter) // <-- This is the extension method from the ctoss library
.ToList();

foreach (var entity in textEntities) Console.WriteLine(entity.TextField);
foreach (var entity in textEntities) Console.WriteLine(entity.TextField);
15 changes: 10 additions & 5 deletions Ctoss/Configuration/CtossSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public static class CtossSettings
if (propertyMapping == null)
return null;

var unaryExpression = (UnaryExpression)propertyMapping.Body;
var binaryExpression = (BinaryExpression)unaryExpression.Operand;

var resultType = binaryExpression.Type;
return resultType;
var expression = propertyMapping.Body;

return expression switch
{
UnaryExpression unaryExpression => unaryExpression.Operand.Type,
BinaryExpression binaryExpression => binaryExpression.Type,
MethodCallExpression methodCallExpression => methodCallExpression.Method.ReturnType,
MemberExpression memberExpression => memberExpression.Type,
_ => throw new InvalidOperationException($"Unhandled expression type: {expression.GetType().Name}")
};
}

internal static PropertySettings? GetPropertySettings<TEntity>(string propertyName)
Expand Down

0 comments on commit e935295

Please sign in to comment.