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

Handle private properties in the compiled model #33409

Merged
merged 1 commit into from
Apr 4, 2024
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
123 changes: 53 additions & 70 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,50 @@ protected virtual bool ShouldUseFullName(string shortTypeName)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Identifier(string name, ICollection<string>? scope = null, bool? capitalize = null)
{
var identifier = Identifier(name, capitalize);
if (scope == null)
{
return Keywords.Contains(identifier) ? "@" + identifier : identifier;
}

var uniqueIdentifier = Keywords.Contains(identifier) ? "@" + identifier : identifier;
var qualifier = 0;
while (scope.Contains(uniqueIdentifier))
{
uniqueIdentifier = identifier + qualifier++;
}

scope.Add(uniqueIdentifier);
identifier = uniqueIdentifier;

return identifier;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// 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 string Identifier<T>(string name, T value, IDictionary<string, T> scope, bool? capitalize = null)
{
var identifier = Identifier(name, capitalize);

var uniqueIdentifier = Keywords.Contains(identifier) ? "@" + identifier : identifier;
var qualifier = 0;
while (scope.ContainsKey(uniqueIdentifier))
{
uniqueIdentifier = identifier + qualifier++;
}

scope.Add(uniqueIdentifier, value);
identifier = uniqueIdentifier;

return identifier;
}

private static string Identifier(string name, bool? capitalize)
{
var builder = new StringBuilder();
var partStart = 0;
Expand Down Expand Up @@ -268,20 +312,7 @@ public virtual string Identifier(string name, ICollection<string>? scope = null,
}

var identifier = builder.ToString();
if (scope != null)
{
var uniqueIdentifier = identifier;
var qualifier = 0;
while (scope.Contains(uniqueIdentifier))
{
uniqueIdentifier = identifier + qualifier++;
}

scope.Add(uniqueIdentifier);
identifier = uniqueIdentifier;
}

return Keywords.Contains(identifier) ? "@" + identifier : identifier;
return identifier;
}

private static void ChangeFirstLetterCase(StringBuilder builder, bool capitalize)
Expand Down Expand Up @@ -1557,36 +1588,12 @@ public virtual string Statement(
Expression node,
ISet<string> collectedNamespaces,
IReadOnlyDictionary<object, string>? constantReplacements,
IReadOnlyDictionary<MemberAccess, string>? memberAccessReplacements)
{
Dictionary<object, ExpressionSyntax>? constantReplacementExpressions = null;
if (constantReplacements != null)
{
constantReplacementExpressions = [];

foreach (var instancePair in constantReplacements)
{
constantReplacementExpressions[instancePair.Key] = SyntaxFactory.IdentifierName(instancePair.Value);
}
}

Dictionary<MemberAccess, ExpressionSyntax>? memberAccessReplacementExpressions = null;
if (memberAccessReplacements != null)
{
memberAccessReplacementExpressions = [];

foreach (var methodPair in memberAccessReplacements)
{
memberAccessReplacementExpressions[methodPair.Key] = SyntaxFactory.IdentifierName(methodPair.Value);
}
}

return ToSourceCode(_translator.TranslateStatement(
IReadOnlyDictionary<MemberInfo, QualifiedName>? memberAccessReplacements)
=> ToSourceCode(_translator.TranslateStatement(
node,
constantReplacementExpressions,
memberAccessReplacementExpressions,
constantReplacements,
memberAccessReplacements,
collectedNamespaces));
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -1598,36 +1605,12 @@ public virtual string Expression(
Expression node,
ISet<string> collectedNamespaces,
IReadOnlyDictionary<object, string>? constantReplacements,
IReadOnlyDictionary<MemberAccess, string>? memberAccessReplacements)
{
Dictionary<object, ExpressionSyntax>? constantReplacementExpressions = null;
if (constantReplacements != null)
{
constantReplacementExpressions = [];

foreach (var instancePair in constantReplacements)
{
constantReplacementExpressions[instancePair.Key] = SyntaxFactory.IdentifierName(instancePair.Value);
}
}

Dictionary<MemberAccess, ExpressionSyntax>? memberAccessReplacementExpressions = null;
if (memberAccessReplacements != null)
{
memberAccessReplacementExpressions = [];

foreach (var methodPair in memberAccessReplacements)
{
memberAccessReplacementExpressions[methodPair.Key] = SyntaxFactory.IdentifierName(methodPair.Value);
}
}

return ToSourceCode(_translator.TranslateExpression(
IReadOnlyDictionary<MemberInfo, QualifiedName>? memberAccessReplacements)
=> ToSourceCode(_translator.TranslateExpression(
node,
constantReplacementExpressions,
memberAccessReplacementExpressions,
constantReplacements,
memberAccessReplacements,
collectedNamespaces));
}

private static bool IsIdentifierStartCharacter(char ch)
{
Expand Down
Loading
Loading