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

Fixes around namespaces and Fluent API calls #25435

Merged
merged 5 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
102 changes: 85 additions & 17 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Text;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down Expand Up @@ -773,7 +777,7 @@ internal static IReadOnlyCollection<Enum> GetFlags(Enum flags)
/// </summary>
public virtual string UnknownLiteral(object? value)
{
if (value == null)
if (value is null)
{
return "null";
}
Expand Down Expand Up @@ -967,15 +971,69 @@ private bool HandleList(IEnumerable<Expression> argumentExpressions, StringBuild
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Fragment(MethodCallCodeFragment fragment)
=> Fragment(fragment, indent: 0);
public virtual string Fragment(MethodCallCodeFragment fragment, string? instanceIdentifier = null, bool typeQualified = false)
=> Fragment(fragment, typeQualified, instanceIdentifier, indent: 0);

private string Fragment(MethodCallCodeFragment fragment, int indent)
private string Fragment(MethodCallCodeFragment fragment, bool typeQualified, string? instanceIdentifier, int indent)
{
var builder = new IndentedStringBuilder();
IndentedStringBuilder builder = new();
roji marked this conversation as resolved.
Show resolved Hide resolved

var current = fragment;
while (current != null)

if (typeQualified)
{
if (instanceIdentifier is null || fragment.MethodInfo is null)
{
throw new ArgumentException(DesignStrings.CannotGenerateTypeQualifiedMethodCal);
}

builder.Append(fragment.DeclaringType!);

if (current.ChainedCall is not null)
{
builder
.AppendLine()
.IncrementIndent();
}

builder
.Append('.')
.Append(fragment.Method)
.Append('(')
.Append(instanceIdentifier);

for (var i = 0; i < fragment.Arguments.Count; i++)
{
builder.Append(", ");
Arg(fragment.Arguments[i]);
}

builder.Append(')');

if (current.ChainedCall is null)
{
return builder.ToString();
}

builder.AppendLine();
current = current.ChainedCall;
}
else
{
if (instanceIdentifier is not null)
{
builder.Append(instanceIdentifier);

if (current.ChainedCall is not null)
{
builder
.AppendLine()
.IncrementIndent();
}
roji marked this conversation as resolved.
Show resolved Hide resolved
}
}

while (true)
{
builder
.Append('.')
Expand All @@ -989,30 +1047,40 @@ private string Fragment(MethodCallCodeFragment fragment, int indent)
builder.Append(", ");
}

var argument = current.Arguments[i];
if (argument is NestedClosureCodeFragment nestedFragment)
{
builder.Append(Fragment(nestedFragment, indent));
}
else
{
builder.Append(UnknownLiteral(argument));
}
Arg(current.Arguments[i]);
}

builder.Append(')');

if (current.ChainedCall is null)
{
break;
}

builder.AppendLine();
current = current.ChainedCall;
}

return builder.ToString();

void Arg(object? argument)
roji marked this conversation as resolved.
Show resolved Hide resolved
{
if (argument is NestedClosureCodeFragment nestedFragment)
{
builder.Append(Fragment(nestedFragment, indent));
}
else
{
builder.Append(UnknownLiteral(argument));
}
}
}

private string Fragment(NestedClosureCodeFragment fragment, int indent)
{
if (fragment.MethodCalls.Count == 1)
{
return fragment.Parameter + " => " + fragment.Parameter + Fragment(fragment.MethodCalls[0], indent);
return fragment.Parameter + " => " + Fragment(fragment.MethodCalls[0], typeQualified: false, fragment.Parameter, indent);
}

var builder = new IndentedStringBuilder();
Expand All @@ -1026,7 +1094,7 @@ private string Fragment(NestedClosureCodeFragment fragment, int indent)
{
foreach (var methodCall in fragment.MethodCalls)
{
builder.Append(fragment.Parameter + Fragment(methodCall, indent + 1));
builder.AppendLines(Fragment(methodCall, typeQualified: false, fragment.Parameter, indent + 1), skipFinalNewline: true);
builder.AppendLine(";");
}
}
Expand Down
Loading