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

ExpressionPrinter fixes/cleanup #18763

Merged
merged 1 commit into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
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
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; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any specific reason these are private properties rather than fields?


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