Skip to content

Commit

Permalink
CreateEqualCondition added to ExpressionUtility
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Sep 28, 2023
1 parent 80affce commit a2bab47
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
48 changes: 48 additions & 0 deletions 01-Core/Jinget.Core/Utilities/Expressions/ExpressionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,53 @@ private static Expression GetBinaryExpression(MemberExpression left, ConstantExp
_ => throw new JingetException($"Operator of type {@operator} is not supported by Jinget!"),
};
}

public static Expression<Func<T, bool>> CreateEqualCondition<T, TValueType>(string propertyName, object value, string parameterName = "Param_0")
{
ParameterExpression parameter = Expression.Parameter(typeof(T), parameterName);

Type valueType = typeof(TValueType);

var rightExpression = Expression.Constant(value, valueType.IsNullable() ? Nullable.GetUnderlyingType(valueType) : valueType);

MemberExpression propertyAccess = GetMemberAccessExpression(parameter, propertyName, typeof(T));
if (valueType.IsNullable())
{
MemberExpression propertyValueAccess = GetMemberAccessExpression(parameter, $"{propertyName}.Value", typeof(T));
MemberExpression hasValueAccess = GetMemberAccessExpression(parameter, $"{propertyName}.HasValue", typeof(T));
return Expression.Lambda<Func<T, bool>>(
Expression.Condition(hasValueAccess, Expression.Equal(propertyValueAccess, rightExpression), Expression.Constant(false)), parameter);
}

return Expression.Lambda<Func<T, bool>>(Expression.Equal(propertyAccess, rightExpression), parameter);
}

private static MemberExpression GetMemberAccessExpression(Expression expression, string members, Type type)
{
List<string> memberList = members.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).ToList();
string firstMember = memberList.First();
MemberExpression memberExpression = CreateMemberAccessExpression(type, expression, firstMember);

foreach (var item in memberList.Skip(1))
{
memberExpression = CreateMemberAccessExpression(((PropertyInfo)memberExpression.Member).PropertyType, memberExpression, item);
}
return memberExpression;
}

private static MemberExpression CreateMemberAccessExpression(Type type, Expression expression, string member)
{
var membersInfo = type.GetMember(member);
if (membersInfo.Any())
{
MemberInfo memberInfo = membersInfo.FirstOrDefault();
if (memberInfo != null)
return Expression.MakeMemberAccess(expression, memberInfo);

throw new JingetException($"Jinget Says: {type.Name} does not {member} member.", 1000);
}

throw new JingetException($"Jinget Says: {type.Name} does not {member} member.", 1000);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using Jinget.Core.Utilities.Expressions;
using Jinget.Core.Types;
using static iText.StyledXmlParser.Jsoup.Select.Evaluator;

namespace Jinget.Core.ExtensionMethods.Expressions.Tests
{
Expand Down Expand Up @@ -36,6 +37,15 @@ public void should_create_a_member_access_expression()
Assert.AreEqual(expectedExpression.Type, result.Type);
}

[TestMethod()]
public void should_create_a_equal_condition_expression()
{
Expression<Func<TestClass, bool>> expectedResult = x => x.Property1 == 1;
var result = ExpressionUtility.CreateEqualCondition<TestClass, int>("Property1", 1, "x");

Assert.AreEqual(expectedResult.Type, result.Type);
}

#region null or empty filters

[TestMethod]
Expand Down

0 comments on commit a2bab47

Please sign in to comment.