Skip to content

Commit

Permalink
ConstantExpressionWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored and ascott18 committed Jul 10, 2020
1 parent 44c4dfe commit b38c1b5
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 22 deletions.
80 changes: 80 additions & 0 deletions src/System.Linq.Dynamic.Core/Parser/ConstantExpressionWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@ public void Wrap(ref Expression expression)
{
expression = WrappedConstant((bool)constantExpression.Value);
}
else if (constantExpression.Type == typeof(bool?))
{
expression = WrappedConstant((bool?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(char))
{
expression = WrappedConstant((char)constantExpression.Value);
}
else if (constantExpression.Type == typeof(char?))
{
expression = WrappedConstant((char?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(byte))
{
expression = WrappedConstant((byte)constantExpression.Value);
}
else if (constantExpression.Type == typeof(byte?))
{
expression = WrappedConstant((byte?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(sbyte))
{
expression = WrappedConstant((sbyte)constantExpression.Value);
Expand All @@ -36,54 +48,106 @@ public void Wrap(ref Expression expression)
{
expression = WrappedConstant((float)constantExpression.Value);
}
else if (constantExpression.Type == typeof(float?))
{
expression = WrappedConstant((float?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(decimal))
{
expression = WrappedConstant((decimal)constantExpression.Value);
}
else if (constantExpression.Type == typeof(decimal?))
{
expression = WrappedConstant((decimal?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(double))
{
expression = WrappedConstant((double)constantExpression.Value);
}
else if (constantExpression.Type == typeof(double?))
{
expression = WrappedConstant((double?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(long))
{
expression = WrappedConstant((long)constantExpression.Value);
}
else if (constantExpression.Type == typeof(long?))
{
expression = WrappedConstant((long?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(ulong))
{
expression = WrappedConstant((ulong)constantExpression.Value);
}
else if (constantExpression.Type == typeof(ulong?))
{
expression = WrappedConstant((ulong?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(int))
{
expression = WrappedConstant((int)constantExpression.Value);
}
else if (constantExpression.Type == typeof(int?))
{
expression = WrappedConstant((int?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(uint))
{
expression = WrappedConstant((uint)constantExpression.Value);
}
else if (constantExpression.Type == typeof(uint?))
{
expression = WrappedConstant((uint?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(short))
{
expression = WrappedConstant((short)constantExpression.Value);
}
else if (constantExpression.Type == typeof(short?))
{
expression = WrappedConstant((short?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(ushort))
{
expression = WrappedConstant((ushort)constantExpression.Value);
}
else if (constantExpression.Type == typeof(ushort?))
{
expression = WrappedConstant((ushort?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(Guid))
{
expression = WrappedConstant((Guid)constantExpression.Value);
}
else if (constantExpression.Type == typeof(Guid?))
{
expression = WrappedConstant((Guid?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(DateTime))
{
expression = WrappedConstant((DateTime)constantExpression.Value);
}
else if (constantExpression.Type == typeof(DateTime?))
{
expression = WrappedConstant((DateTime?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(DateTimeOffset))
{
expression = WrappedConstant((DateTimeOffset)constantExpression.Value);
}
else if (constantExpression.Type == typeof(DateTimeOffset?))
{
expression = WrappedConstant((DateTimeOffset?)constantExpression.Value);
}
else if (constantExpression.Type == typeof(TimeSpan))
{
expression = WrappedConstant((TimeSpan)constantExpression.Value);
}
else if (constantExpression.Type == typeof(TimeSpan?))
{
expression = WrappedConstant((TimeSpan?)constantExpression.Value);
}

return;
}
Expand All @@ -94,18 +158,34 @@ public void Wrap(ref Expression expression)
{
expression = WrappedConstant(Expression.Lambda<Func<Guid>>(newExpression).Compile()());
}
else if (newExpression.Type == typeof(Guid?))
{
expression = WrappedConstant(Expression.Lambda<Func<Guid?>>(newExpression).Compile()());
}
else if (newExpression.Type == typeof(DateTime))
{
expression = WrappedConstant(Expression.Lambda<Func<DateTime>>(newExpression).Compile()());
}
else if (newExpression.Type == typeof(DateTime?))
{
expression = WrappedConstant(Expression.Lambda<Func<DateTime?>>(newExpression).Compile()());
}
else if (newExpression.Type == typeof(DateTimeOffset))
{
expression = WrappedConstant(Expression.Lambda<Func<DateTimeOffset>>(newExpression).Compile()());
}
else if (newExpression.Type == typeof(DateTimeOffset?))
{
expression = WrappedConstant(Expression.Lambda<Func<DateTimeOffset?>>(newExpression).Compile()());
}
else if (newExpression.Type == typeof(TimeSpan))
{
expression = WrappedConstant(Expression.Lambda<Func<TimeSpan>>(newExpression).Compile()());
}
else if (newExpression.Type == typeof(TimeSpan?))
{
expression = WrappedConstant(Expression.Lambda<Func<TimeSpan?>>(newExpression).Compile()());
}
}
}

Expand Down
71 changes: 49 additions & 22 deletions test/System.Linq.Dynamic.Core.Tests/DynamicExpressionParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,25 @@ public Type ResolveTypeBySimpleName(string typeName)
}
}

[Fact]
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_false()
{
// Assign
var config = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = false
};

// Act
var expression = DynamicExpressionParser.ParseLambda<string, bool>(config, true, "s => s == \"x\"");

// Assert
dynamic constantExpression = (ConstantExpression)(expression.Body as BinaryExpression).Right;
string value = constantExpression.Value;

Check.That(value).IsEqualTo("x");
}

[Fact]
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_true()
{
Expand All @@ -261,16 +280,43 @@ public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQu
};

// Act
var expression = DynamicExpressionParser.ParseLambda<string, bool>(config, true, "s => s == \"x\"");
var expression = DynamicExpressionParser.ParseLambda<Person, bool>(config, false, "Id = 42");
string expressionAsString = expression.ToString();

// Assert
Check.That(expressionAsString).IsEqualTo("Param_0 => (Param_0.Id == value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[System.Int32]).Value)");

dynamic constantExpression = ((MemberExpression)(expression.Body as BinaryExpression).Right).Expression as ConstantExpression;
dynamic wrappedObj = constantExpression.Value;

var propertyInfo = wrappedObj.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public);
string value = propertyInfo.GetValue(wrappedObj) as string;
int value = (int) propertyInfo.GetValue(wrappedObj);

Check.That(value).IsEqualTo("x");
Check.That(value).IsEqualTo(42);
}

[Fact]
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_ForNullableProperty_true()
{
// Assign
var config = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = true
};

// Act
var expression = DynamicExpressionParser.ParseLambda<Person, bool>(config, false, "NullableId = 42");
string expressionAsString = expression.ToString();

// Assert
Check.That(expressionAsString).IsEqualTo("Param_0 => (Param_0.NullableId == value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[System.Nullable`1[System.Int32]]).Value)");
dynamic constantExpression = ((MemberExpression)(expression.Body as BinaryExpression).Right).Expression as ConstantExpression;
dynamic wrappedObj = constantExpression.Value;

var propertyInfo = wrappedObj.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public);
int? value = (int?) propertyInfo.GetValue(wrappedObj);

Check.That(value).IsEqualTo(42);
}

[Theory]
Expand Down Expand Up @@ -298,25 +344,6 @@ public void DynamicExpressionParser_ParseLambda_WithStructWithEquality(string qu
Check.That(result.ToArray()[0]).Equals(expected[0]);
}

[Fact]
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_false()
{
// Assign
var config = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = false
};

// Act
var expression = DynamicExpressionParser.ParseLambda<string, bool>(config, true, "s => s == \"x\"");

// Assert
dynamic constantExpression = (ConstantExpression)(expression.Body as BinaryExpression).Right;
string value = constantExpression.Value;

Check.That(value).IsEqualTo("x");
}

[Fact]
public void DynamicExpressionParser_ParseLambda_ToList()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ public void ExpressionHelper_WrapConstantExpression_false()
Check.That(expression.ToString()).Equals("\"test\"");
}

[Fact]
public void ExpressionHelper_WrapNullableConstantExpression_false()
{
// Assign
var config = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = false
};
var expressionHelper = new ExpressionHelper(config);

int? value = 42;
Expression expression = Expression.Constant(value);

// Act
expressionHelper.WrapConstantExpression(ref expression);

// Assert
Check.That(expression).IsInstanceOf<ConstantExpression>();
Check.That(expression.ToString()).Equals("42");
}

[Fact]
public void ExpressionHelper_WrapConstantExpression_true()
{
Expand All @@ -57,6 +78,28 @@ public void ExpressionHelper_WrapConstantExpression_true()
Check.That(expression.ToString()).Equals("value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[System.String]).Value");
}

[Fact]
public void ExpressionHelper_WrapNullableConstantExpression_true()
{
// Assign
var config = new ParsingConfig
{
UseParameterizedNamesInDynamicQuery = true
};
var expressionHelper = new ExpressionHelper(config);

int? value = 42;
Expression expression = Expression.Constant(value);

// Act
expressionHelper.WrapConstantExpression(ref expression);
expressionHelper.WrapConstantExpression(ref expression);

// Assert
Check.That(expression.GetType().FullName).Equals("System.Linq.Expressions.PropertyExpression");
Check.That(expression.ToString()).Equals("value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[System.Int32]).Value");
}

[Fact]
public void ExpressionHelper_OptimizeStringForEqualityIfPossible_Guid()
{
Expand Down

0 comments on commit b38c1b5

Please sign in to comment.