Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Aug 12, 2021
1 parent a1d7b1a commit b91f109
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 145 deletions.
11 changes: 5 additions & 6 deletions src/EFCore.Design/Design/Internal/CSharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,14 @@ public virtual string Fragment(MethodCallCodeFragment fragment, string? instance

private string Fragment(MethodCallCodeFragment fragment, bool typeQualified, string? instanceIdentifier, int indent)
{
IndentedStringBuilder builder = new();

var builder = new IndentedStringBuilder();
var current = fragment;

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

builder.Append(fragment.DeclaringType!);
Expand All @@ -1005,7 +1004,7 @@ private string Fragment(MethodCallCodeFragment fragment, bool typeQualified, str
for (var i = 0; i < fragment.Arguments.Count; i++)
{
builder.Append(", ");
Arg(fragment.Arguments[i]);
Argument(fragment.Arguments[i]);
}

builder.Append(')');
Expand Down Expand Up @@ -1047,7 +1046,7 @@ private string Fragment(MethodCallCodeFragment fragment, bool typeQualified, str
builder.Append(", ");
}

Arg(current.Arguments[i]);
Argument(current.Arguments[i]);
}

builder.Append(')');
Expand All @@ -1063,7 +1062,7 @@ private string Fragment(MethodCallCodeFragment fragment, bool typeQualified, str

return builder.ToString();

void Arg(object? argument)
void Argument(object? argument)
{
if (argument is NestedClosureCodeFragment nestedFragment)
{
Expand Down
67 changes: 13 additions & 54 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public virtual void Generate(string modelBuilderName, IModel model, IndentedStri
annotations[CoreAnnotationNames.ProductVersion] = new Annotation(CoreAnnotationNames.ProductVersion, productVersion);
}

GenerateRemainingAnnotations(modelBuilderName, model, stringBuilder, annotations, inChainedCall: false, leadingNewline: false);
GenerateAnnotations(modelBuilderName, model, stringBuilder, annotations, inChainedCall: false, leadingNewline: false);

foreach (var sequence in model.GetSequences())
{
Expand Down Expand Up @@ -479,6 +479,7 @@ protected virtual void GenerateProperty(
.AppendLine()
.Append(propertyBuilderName);

// Note that GenerateAnnotations below does the corresponding decrement
stringBuilder.IncrementIndent();

if (property.IsConcurrencyToken)
Expand Down Expand Up @@ -550,7 +551,7 @@ protected virtual void GeneratePropertyAnnotations(
GenerateFluentApiForDefaultValue(property, stringBuilder);
annotations.Remove(RelationalAnnotationNames.DefaultValue);

GenerateRemainingAnnotations(propertyBuilderName, property, stringBuilder, annotations, inChainedCall: true);
GenerateAnnotations(propertyBuilderName, property, stringBuilder, annotations, inChainedCall: true);
}

private ValueConverter? FindValueConverter(IProperty property)
Expand Down Expand Up @@ -616,6 +617,7 @@ protected virtual void GenerateKey(
.Append(string.Join(", ", key.Properties.Select(p => Code.Literal(p.Name))))
.Append(")");

// Note that GenerateAnnotations below does the corresponding decrement
stringBuilder.IncrementIndent();

GenerateKeyAnnotations(entityTypeBuilderName, key, stringBuilder);
Expand All @@ -637,7 +639,7 @@ protected virtual void GenerateKeyAnnotations(string entityTypeBuilderName, IKey
.FilterIgnoredAnnotations(key.GetAnnotations())
.ToDictionary(a => a.Name, a => a);

GenerateRemainingAnnotations(entityTypeBuilderName, key, stringBuilder, annotations, inChainedCall: true);
GenerateAnnotations(entityTypeBuilderName, key, stringBuilder, annotations, inChainedCall: true);
}

/// <summary>
Expand Down Expand Up @@ -690,6 +692,7 @@ protected virtual void GenerateIndex(
.AppendLine()
.Append(indexBuilderName);

// Note that GenerateAnnotations below does the corresponding decrement
stringBuilder.IncrementIndent();

if (index.IsUnique)
Expand Down Expand Up @@ -721,7 +724,7 @@ protected virtual void GenerateIndexAnnotations(
.FilterIgnoredAnnotations(index.GetAnnotations())
.ToDictionary(a => a.Name, a => a);

GenerateRemainingAnnotations(indexBuilderName, index, stringBuilder, annotations, inChainedCall: true);
GenerateAnnotations(indexBuilderName, index, stringBuilder, annotations, inChainedCall: true);
}

/// <summary>
Expand Down Expand Up @@ -950,7 +953,7 @@ protected virtual void GenerateEntityTypeAnnotations(
stringBuilder.AppendLine(";");
}

GenerateRemainingAnnotations(entityTypeBuilderName, entityType, stringBuilder, annotations, inChainedCall: false);
GenerateAnnotations(entityTypeBuilderName, entityType, stringBuilder, annotations, inChainedCall: false);
}

/// <summary>
Expand Down Expand Up @@ -1078,6 +1081,7 @@ protected virtual void GenerateForeignKey(
.Append(")")
.AppendLine();

// Note that GenerateAnnotations below does the corresponding decrement
stringBuilder.IncrementIndent();

if (foreignKey.IsUnique
Expand Down Expand Up @@ -1184,7 +1188,7 @@ protected virtual void GenerateForeignKeyAnnotations(
.FilterIgnoredAnnotations(foreignKey.GetAnnotations())
.ToDictionary(a => a.Name, a => a);

GenerateRemainingAnnotations(entityTypeBuilderName, foreignKey, stringBuilder, annotations, inChainedCall: true);
GenerateAnnotations(entityTypeBuilderName, foreignKey, stringBuilder, annotations, inChainedCall: true);
}

/// <summary>
Expand Down Expand Up @@ -1267,6 +1271,7 @@ protected virtual void GenerateNavigation(
.Append(Code.Literal(navigation.Name))
.Append(")");

// Note that GenerateAnnotations below does the corresponding decrement
stringBuilder.IncrementIndent();

if (!navigation.IsOnDependent
Expand Down Expand Up @@ -1300,46 +1305,7 @@ protected virtual void GenerateNavigationAnnotations(
.FilterIgnoredAnnotations(navigation.GetAnnotations())
.ToDictionary(a => a.Name, a => a);

GenerateRemainingAnnotations(entityTypeBuilderName, navigation, stringBuilder, annotations, inChainedCall: true);
}

/// <summary>
/// Generates code for annotations.
/// </summary>
/// <param name="annotations"> The annotations. </param>
/// <param name="stringBuilder"> The builder code is added to. </param>
protected virtual void GenerateAnnotations(
IEnumerable<IAnnotation> annotations,
IndentedStringBuilder stringBuilder)
{
Check.NotNull(annotations, nameof(annotations));
Check.NotNull(stringBuilder, nameof(stringBuilder));

foreach (var annotation in annotations.OrderBy(a => a.Name))
{
stringBuilder.AppendLine();
GenerateAnnotation(annotation, stringBuilder);
}
}

/// <summary>
/// Generates code for an annotation which does not have a fluent API call.
/// </summary>
/// <param name="annotation"> The annotation. </param>
/// <param name="stringBuilder"> The builder code is added to. </param>
protected virtual void GenerateAnnotation(
IAnnotation annotation,
IndentedStringBuilder stringBuilder)
{
Check.NotNull(annotation, nameof(annotation));
Check.NotNull(stringBuilder, nameof(stringBuilder));

stringBuilder
.Append(".HasAnnotation(")
.Append(Code.Literal(annotation.Name))
.Append(", ")
.Append(Code.UnknownLiteral(annotation.Value))
.Append(")");
GenerateAnnotations(entityTypeBuilderName, navigation, stringBuilder, annotations, inChainedCall: true);
}

/// <summary>
Expand Down Expand Up @@ -1515,7 +1481,7 @@ private void GenerateFluentApiForDefaultValue(
.Append(")");
}

private void GenerateRemainingAnnotations(
private void GenerateAnnotations(
string builderName,
IAnnotatable annotatable,
IndentedStringBuilder stringBuilder,
Expand Down Expand Up @@ -1547,7 +1513,6 @@ private void GenerateRemainingAnnotations(
// Append remaining raw annotations which did not get generated as Fluent API calls
foreach (var annotation in annotations.Values.OrderBy(a => a.Name))
{
// var call = new MethodCallCodeFragment("HasAnnotation", annotation.Name, annotation.Value);
var call = new MethodCallCodeFragment(_hasAnnotationMethodInfo, annotation.Name, annotation.Value);
nonQualifiedCalls = nonQualifiedCalls is null ? call : nonQualifiedCalls.Chain(call);
}
Expand Down Expand Up @@ -1593,12 +1558,6 @@ private void GenerateRemainingAnnotations(
stringBuilder
.AppendLines(Code.Fragment(typeQualifiedCalls, builderName, typeQualified: true), skipFinalNewline: true)
.AppendLine(";");

// foreach (var call in typeQualifiedCalls)
// {
// stringBuilder.Append(Code.Fragment(call, builderName, typeQualified: true));
// stringBuilder.AppendLine(";");
// }
}
}
}
Expand Down
Loading

0 comments on commit b91f109

Please sign in to comment.