Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling dynamic types for short-circuti expression parsing #633

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/RulesEngine/ExpressionBuilders/RuleExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;

namespace RulesEngine.ExpressionBuilders
{
public class RuleExpressionParser
{
private readonly ReSettings _reSettings;
private readonly IDictionary<string, MethodInfo> _methodInfo;
private readonly IDictionary<string, MethodInfo> _methodInfo;
private readonly Regex propertyErrorPattern = new Regex(@"No property or field '(.*?)' exists in type '(.*?)'",RegexOptions.Compiled | RegexOptions.IgnoreCase);

public RuleExpressionParser(ReSettings reSettings = null)
{
Expand All @@ -36,11 +39,32 @@ public Expression Parse(string expression, ParameterExpression[] parameters, Typ
var config = new ParsingConfig {
CustomTypeProvider = new CustomTypeProvider(_reSettings.CustomTypes),
IsCaseSensitive = _reSettings.IsExpressionCaseSensitive
};
return new ExpressionParser(parameters, expression, new object[] { }, config).Parse(returnType);

};

// Instead of immediately returning default values, allow for expression parsing to handle dynamic evaluation.
try
{
return new ExpressionParser(parameters, expression, Array.Empty<object>(), config).Parse(returnType);
}
catch (ParseException ex) when (propertyErrorPattern.IsMatch(ex.Message))
{
return Expression.Constant(GetDefaultValueForType(returnType));
}
catch (Exception ex)
{
throw new Exception($"Expression parsing error: {ex.Message}", ex);
}
}

private object GetDefaultValueForType(Type type)
{
if (type == typeof(bool))
return false;
if (type == typeof(int) || type == typeof(float) || type == typeof(double))
return 0;
return null;
}

public Func<object[], T> Compile<T>(string expression, RuleParameter[] ruleParams)
{
var rtype = typeof(T);
Expand Down
Loading