Skip to content

Commit

Permalink
Nullability annotation for scaffolding
Browse files Browse the repository at this point in the history
Resubmitting after reverted by cb686e849b448ebbafe9dfee5288d4610bc9823c

(cherry picked from commit eab93af)
  • Loading branch information
roji committed Jan 3, 2020
1 parent 2e8ef35 commit d1cd9c7
Show file tree
Hide file tree
Showing 57 changed files with 583 additions and 561 deletions.
2 changes: 2 additions & 0 deletions src/EFCore.Design/Scaffolding/IModelCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding
{
/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/EFCore.Design/Scaffolding/IModelCodeGeneratorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding
{
/// <summary>
Expand All @@ -15,6 +17,6 @@ public interface IModelCodeGeneratorSelector
/// </summary>
/// <param name="language"> The programming language. </param>
/// <returns> The <see cref="IModelCodeGenerator" />. </returns>
IModelCodeGenerator Select([CanBeNull] string language);
IModelCodeGenerator Select([CanBeNull] string? language);
}
}
4 changes: 3 additions & 1 deletion src/EFCore.Design/Scaffolding/IReverseEngineerScaffolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding
{
/// <summary>
Expand Down Expand Up @@ -33,7 +35,7 @@ ScaffoldedModel ScaffoldModel(
/// <returns> The model files. </returns>
SavedModelFiles Save(
[NotNull] ScaffoldedModel scaffoldedModel,
[CanBeNull] string outputDir,
[NotNull] string outputDir,
bool overwriteFiles);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -31,7 +33,7 @@ public class CSharpDbContextGenerator : ICSharpDbContextGenerator
private readonly ICSharpHelper _code;
private readonly IProviderConfigurationCodeGenerator _providerConfigurationCodeGenerator;
private readonly IAnnotationCodeGenerator _annotationCodeGenerator;
private IndentedStringBuilder _sb;
private IndentedStringBuilder _sb = null!;
private bool _entityTypeBuilderInitialized;

/// <summary>
Expand Down Expand Up @@ -64,7 +66,7 @@ public virtual string WriteCode(
IModel model,
string contextName,
string connectionString,
string contextNamespace,
string? contextNamespace,
string modelNamespace,
bool useDataAnnotations,
bool suppressConnectionStringWarning)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -26,7 +28,7 @@ public class CSharpEntityTypeGenerator : ICSharpEntityTypeGenerator
{
private readonly ICSharpHelper _code;

private IndentedStringBuilder _sb;
private IndentedStringBuilder _sb = null!;
private bool _useDataAnnotations;

/// <summary>
Expand Down
33 changes: 24 additions & 9 deletions src/EFCore.Design/Scaffolding/Internal/CSharpModelGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.IO;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand Down Expand Up @@ -74,7 +77,20 @@ public override ScaffoldedModel GenerateModel(
Check.NotNull(model, nameof(model));
Check.NotNull(options, nameof(options));

var resultingFiles = new ScaffoldedModel();
if (options.ContextName == null)
{
throw new ArgumentException($"{nameof(options.ContextName)} cannot be null", nameof(options));
}

if (options.ConnectionString == null)
{
throw new ArgumentException($"{nameof(options.ConnectionString)} cannot be null", nameof(options));
}

if (options.ModelNamespace == null)
{
throw new ArgumentException($"{nameof(options.ModelNamespace)} cannot be null", nameof(options));
}

var generatedCode = CSharpDbContextGenerator.WriteCode(
model,
Expand All @@ -87,13 +103,12 @@ public override ScaffoldedModel GenerateModel(

// output DbContext .cs file
var dbContextFileName = options.ContextName + FileExtension;
resultingFiles.ContextFile = new ScaffoldedFile
{
Path = options.ContextDir != null
? Path.Combine(options.ContextDir, dbContextFileName)
: dbContextFileName,
Code = generatedCode
};
var resultingFiles = new ScaffoldedModel(
new ScaffoldedFile(
options.ContextDir != null
? Path.Combine(options.ContextDir, dbContextFileName)
: dbContextFileName,
generatedCode));

foreach (var entityType in model.GetEntityTypes())
{
Expand All @@ -102,7 +117,7 @@ public override ScaffoldedModel GenerateModel(
// output EntityType poco .cs file
var entityTypeFileName = entityType.DisplayName() + FileExtension;
resultingFiles.AdditionalFiles.Add(
new ScaffoldedFile { Path = entityTypeFileName, Code = generatedCode });
new ScaffoldedFile(entityTypeFileName, generatedCode));
}

return resultingFiles;
Expand Down
6 changes: 4 additions & 2 deletions src/EFCore.Design/Scaffolding/Internal/CSharpNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -18,7 +20,7 @@ public class CSharpNamer<T>
{
private readonly Func<T, string> _nameGetter;
private readonly ICSharpUtilities _cSharpUtilities;
private readonly Func<string, string> _singularizePluralizer;
private readonly Func<string, string>? _singularizePluralizer;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -37,7 +39,7 @@ public class CSharpNamer<T>
public CSharpNamer(
[NotNull] Func<T, string> nameGetter,
[NotNull] ICSharpUtilities cSharpUtilities,
[CanBeNull] Func<string, string> singularizePluralizer)
[CanBeNull] Func<string, string>? singularizePluralizer)
{
Check.NotNull(nameGetter, nameof(nameGetter));
Check.NotNull(cSharpUtilities, nameof(cSharpUtilities));
Expand Down
8 changes: 5 additions & 3 deletions src/EFCore.Design/Scaffolding/Internal/CSharpUniqueNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -26,7 +28,7 @@ public class CSharpUniqueNamer<T> : CSharpNamer<T>
public CSharpUniqueNamer(
[NotNull] Func<T, string> nameGetter,
[NotNull] ICSharpUtilities cSharpUtilities,
[CanBeNull] Func<string, string> singularizePluralizer)
[CanBeNull] Func<string, string>? singularizePluralizer)
: this(nameGetter, null, cSharpUtilities, singularizePluralizer)
{
}
Expand All @@ -39,9 +41,9 @@ public CSharpUniqueNamer(
/// </summary>
public CSharpUniqueNamer(
[NotNull] Func<T, string> nameGetter,
[CanBeNull] IEnumerable<string> usedNames,
[CanBeNull] IEnumerable<string>? usedNames,
[NotNull] ICSharpUtilities cSharpUtilities,
[CanBeNull] Func<string, string> singularizePluralizer)
[CanBeNull] Func<string, string>? singularizePluralizer)
: base(nameGetter, cSharpUtilities, singularizePluralizer)
{
if (usedNames != null)
Expand Down
16 changes: 9 additions & 7 deletions src/EFCore.Design/Scaffolding/Internal/CSharpUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand Down Expand Up @@ -122,8 +124,8 @@ public virtual bool IsCSharpKeyword(string identifier)
/// </summary>
public virtual string GenerateCSharpIdentifier(
string identifier,
ICollection<string> existingIdentifiers,
Func<string, string> singularizePluralizer)
ICollection<string>? existingIdentifiers,
Func<string, string>? singularizePluralizer)
=> GenerateCSharpIdentifier(identifier, existingIdentifiers, singularizePluralizer, Uniquifier);

/// <summary>
Expand All @@ -134,9 +136,9 @@ public virtual string GenerateCSharpIdentifier(
/// </summary>
public virtual string GenerateCSharpIdentifier(
string identifier,
ICollection<string> existingIdentifiers,
Func<string, string> singularizePluralizer,
Func<string, ICollection<string>, string> uniquifier)
ICollection<string>? existingIdentifiers,
Func<string, string>? singularizePluralizer,
Func<string, ICollection<string>?, string> uniquifier)
{
Check.NotNull(identifier, nameof(identifier));
Check.NotNull(uniquifier, nameof(uniquifier));
Expand Down Expand Up @@ -177,7 +179,7 @@ public virtual string GenerateCSharpIdentifier(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string Uniquifier(
[NotNull] string proposedIdentifier, [CanBeNull] ICollection<string> existingIdentifiers)
[NotNull] string proposedIdentifier, [CanBeNull] ICollection<string>? existingIdentifiers)
{
Check.NotEmpty(proposedIdentifier, nameof(proposedIdentifier));

Expand All @@ -203,7 +205,7 @@ public virtual string Uniquifier(
/// 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 bool IsValidIdentifier(string name)
public virtual bool IsValidIdentifier(string? name)
{
if (string.IsNullOrEmpty(name))
{
Expand Down
10 changes: 4 additions & 6 deletions src/EFCore.Design/Scaffolding/Internal/CandidateNamingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -26,9 +28,7 @@ public class CandidateNamingService : ICandidateNamingService
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string GenerateCandidateIdentifier(DatabaseTable originalTable)
{
return GenerateCandidateIdentifier(originalTable.Name);
}
=> GenerateCandidateIdentifier(originalTable.Name);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -37,9 +37,7 @@ public virtual string GenerateCandidateIdentifier(DatabaseTable originalTable)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string GenerateCandidateIdentifier(DatabaseColumn originalColumn)
{
return GenerateCandidateIdentifier(originalColumn.Name);
}
=> GenerateCandidateIdentifier(originalColumn.Name);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -24,7 +26,7 @@ string WriteCode(
[NotNull] IModel model,
[NotNull] string contextName,
[NotNull] string connectionString,
[NotNull] string contextNamespace,
[CanBeNull] string? contextNamespace,
[NotNull] string modelNamespace,
bool useDataAnnotations,
bool suppressConnectionStringWarning);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand Down
12 changes: 7 additions & 5 deletions src/EFCore.Design/Scaffolding/Internal/ICSharpUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -22,8 +24,8 @@ public interface ICSharpUtilities
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
string GenerateCSharpIdentifier(
[NotNull] string identifier, [CanBeNull] ICollection<string> existingIdentifiers,
[CanBeNull] Func<string, string> singularizePluralizer);
[NotNull] string identifier, [CanBeNull] ICollection<string>? existingIdentifiers,
[CanBeNull] Func<string, string>? singularizePluralizer);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -32,8 +34,8 @@ string GenerateCSharpIdentifier(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
string GenerateCSharpIdentifier(
[NotNull] string identifier, [CanBeNull] ICollection<string> existingIdentifiers,
[CanBeNull] Func<string, string> singularizePluralizer, [NotNull] Func<string, ICollection<string>, string> uniquifier);
[NotNull] string identifier, [CanBeNull] ICollection<string>? existingIdentifiers,
[CanBeNull] Func<string, string>? singularizePluralizer, [NotNull] Func<string, ICollection<string>?, string> uniquifier);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -49,6 +51,6 @@ string GenerateCSharpIdentifier(
/// 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>
bool IsValidIdentifier([CanBeNull] string name);
bool IsValidIdentifier([CanBeNull] string? name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
/// <summary>
Expand All @@ -19,6 +21,6 @@ public interface IScaffoldingTypeMapper
/// 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>
TypeScaffoldingInfo FindMapping([NotNull] string storeType, bool keyOrIndex, bool rowVersion);
TypeScaffoldingInfo? FindMapping([NotNull] string storeType, bool keyOrIndex, bool rowVersion);
}
}
Loading

0 comments on commit d1cd9c7

Please sign in to comment.