Skip to content

Commit dab5a50

Browse files
committed
Support for translating constant lambda expressions
1 parent e277228 commit dab5a50

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

ReadableExpressions.UnitTests/WhenTranslatingConstants.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Data.Common;
66
using System.IO;
7+
using System.Linq;
78
using System.Linq.Expressions;
89
using Microsoft.VisualStudio.TestTools.UnitTesting;
910

@@ -387,6 +388,21 @@ public void ShouldTranslateAnObjectConstant()
387388

388389
Assert.AreEqual("123", translated);
389390
}
391+
392+
[TestMethod]
393+
public void ShouldTranslateALambdaConstant()
394+
{
395+
Expression<Func<int, int>> lambda =
396+
num => Enumerable.Range(num, 10).Select(i => new { Index = i }).Sum(d => d.Index);
397+
398+
var lambdaConstant = Expression.Constant(lambda, lambda.GetType());
399+
400+
var translated = lambdaConstant.ToReadableString();
401+
402+
const string EXPECTED = @"num => Enumerable.Range(num, 10).Select(i => new { Index = i }).Sum(d => d.Index)";
403+
404+
Assert.AreEqual(EXPECTED.TrimStart(), translated);
405+
}
390406
}
391407

392408
internal class GenericOne<T> { }

ReadableExpressions/Translators/ConstantExpressionTranslator.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,18 @@ public override string Translate(Expression expression, TranslationContext conte
3131
return constant.Type.GetFriendlyName() + "." + constant.Value;
3232
}
3333

34-
if (TryTranslateByTypeCode(constant, out var translation))
34+
if (TryTranslateByTypeCode(constant, context, out var translation))
3535
{
3636
return translation;
3737
}
3838

3939
return constant.Value.ToString();
4040
}
4141

42-
private static bool TryTranslateByTypeCode(ConstantExpression constant, out string translation)
42+
private static bool TryTranslateByTypeCode(
43+
ConstantExpression constant,
44+
TranslationContext context,
45+
out string translation)
4346
{
4447
switch ((Nullable.GetUnderlyingType(constant.Type) ?? constant.Type).GetTypeCode())
4548
{
@@ -78,6 +81,7 @@ private static bool TryTranslateByTypeCode(ConstantExpression constant, out stri
7881

7982
case NetStandardTypeCode.Object:
8083
if (IsType(constant, out translation) ||
84+
IsLambda(constant, context, out translation) ||
8185
IsFunc(constant, out translation) ||
8286
IsDefault<Guid>(constant, out translation) ||
8387
IsTimeSpan(constant, out translation))
@@ -236,6 +240,21 @@ private static bool IsType(ConstantExpression constant, out string translation)
236240
return false;
237241
}
238242

243+
private static bool IsLambda(
244+
ConstantExpression constant,
245+
TranslationContext context,
246+
out string translation)
247+
{
248+
if (constant.Value is LambdaExpression lambda)
249+
{
250+
translation = context.TranslateAsCodeBlock(lambda);
251+
return true;
252+
}
253+
254+
translation = null;
255+
return false;
256+
}
257+
239258
private static readonly Regex _funcMatcher = new Regex(@"^System\.(?:Func|Action)`\d+\[.+\]$");
240259

241260
private static bool IsFunc(ConstantExpression constant, out string translation)

0 commit comments

Comments
 (0)