Skip to content

Commit

Permalink
ExpressionPrinter fixes/cleanup
Browse files Browse the repository at this point in the history
Fix to #18709 - Query: expression printer should still print type argument for Cast and OfType
Fix to #18413 - Query: clean-up expression printer when we can do breaking changes

Resolves #18709
Resolves #18413
  • Loading branch information
maumar committed Nov 5, 2019
1 parent 83b5ad6 commit 37f0135
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
65 changes: 36 additions & 29 deletions src/EFCore/Query/ExpressionPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public ExpressionPrinter()
}

private int? CharacterLimit { get; set; }

private bool GenerateUniqueParameterIds { get; set; }
private bool Verbose { get; set; }

public virtual void VisitList<T>(
IReadOnlyList<T> items,
Expand Down Expand Up @@ -110,26 +109,26 @@ private void AppendLine([NotNull] string message)
public virtual string Print(
Expression expression,
int? characterLimit = null)
=> PrintCore(expression, characterLimit, generateUniqueParameterIds: false);
=> PrintCore(expression, characterLimit, verbose: false);

public virtual string PrintDebug(
Expression expression,
int? characterLimit = null,
bool generateUniqueParameterIds = true)
=> PrintCore(expression, characterLimit, generateUniqueParameterIds);
bool verbose = true)
=> PrintCore(expression, characterLimit, verbose);

protected virtual string PrintCore(
Expression expression,
int? characterLimit,
bool generateUniqueParameterIds)
bool verbose)
{
_stringBuilder.Clear();
_parametersInScope.Clear();
_namelessParameters.Clear();
_encounteredParameters.Clear();

CharacterLimit = characterLimit;
GenerateUniqueParameterIds = generateUniqueParameterIds;
Verbose = verbose;

Visit(expression);

Expand Down Expand Up @@ -552,8 +551,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
var methodArguments = methodCallExpression.Arguments.ToList();
var method = methodCallExpression.Method;

// TODO: issue #18413
var extensionMethod = !GenerateUniqueParameterIds
var extensionMethod = !Verbose
&& methodCallExpression.Arguments.Count > 0
&& method.IsDefined(typeof(ExtensionAttribute), inherit: false);

Expand All @@ -564,6 +562,11 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
_stringBuilder.AppendLine();
_stringBuilder.Append($".{method.Name}");
methodArguments = methodArguments.Skip(1).ToList();
if (method.Name == nameof(Enumerable.Cast)
|| method.Name == nameof(Enumerable.OfType))
{
PrintGenericArguments(method, _stringBuilder);
}
}
else
{
Expand All @@ -573,23 +576,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCallExp
}

_stringBuilder.Append(method.Name);
if (method.IsGenericMethod)
{
_stringBuilder.Append("<");
var first = true;
foreach (var genericArgument in method.GetGenericArguments())
{
if (!first)
{
_stringBuilder.Append(", ");
}

_stringBuilder.Append(genericArgument.ShortDisplayName());
first = false;
}

_stringBuilder.Append(">");
}
PrintGenericArguments(method, _stringBuilder);
}

_stringBuilder.Append("(");
Expand Down Expand Up @@ -647,6 +634,27 @@ var argumentNames
}

return methodCallExpression;

static void PrintGenericArguments(MethodInfo method, IndentedStringBuilder stringBuilder)
{
if (method.IsGenericMethod)
{
stringBuilder.Append("<");
var first = true;
foreach (var genericArgument in method.GetGenericArguments())
{
if (!first)
{
stringBuilder.Append(", ");
}

stringBuilder.Append(genericArgument.ShortDisplayName());
first = false;
}

stringBuilder.Append(">");
}
}
}

protected override Expression VisitNew(NewExpression newExpression)
Expand Down Expand Up @@ -756,8 +764,7 @@ protected override Expression VisitParameter(ParameterExpression parameterExpres
}
else
{
// TODO: issue #18413
if (GenerateUniqueParameterIds)
if (Verbose)
{
Append("(Unhandled parameter: ");
Append(parameterExpression.Name);
Expand All @@ -769,7 +776,7 @@ protected override Expression VisitParameter(ParameterExpression parameterExpres
}
}

if (GenerateUniqueParameterIds)
if (Verbose)
{
var parameterIndex = _encounteredParameters.Count;
if (_encounteredParameters.Contains(parameterExpression))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7301,7 +7301,7 @@ public void Cast_to_non_implemented_interface_is_not_removed_from_expression_tre
() => queryBase.Cast<IDummyEntity>().FirstOrDefault(x => x.Id == id)).Message;

Assert.Equal(
CoreStrings.TranslationFailed(@"DbSet<MockEntity> .Cast() .Where(e => e.Id == __id_0)"),
CoreStrings.TranslationFailed(@"DbSet<MockEntity> .Cast<IDummyEntity>() .Where(e => e.Id == __id_0)"),
message.Replace("\r", "").Replace("\n", ""));
}

Expand Down

0 comments on commit 37f0135

Please sign in to comment.