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

Support customizing model factory accessibility #4643

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,16 @@ private async Task<Project> RemoveMethodsFromModelFactoryAsync(Project project,
// find the GENERATED document of model factory (we may have the customized document of this for overloads)
Document? modelFactoryGeneratedDocument = null;
// the nodes corresponding to the model factory symbol has never been changed therefore the nodes inside the cache are still usable
foreach (var declarationNode in definitions.DeclaredNodesCache[modelFactorySymbol])
if (definitions.DeclaredNodesCache.TryGetValue(modelFactorySymbol, out var nodes))
{
var document = project.GetDocument(declarationNode.SyntaxTree);
if (document != null && GeneratedCodeWorkspace.IsGeneratedDocument(document))
foreach (var declarationNode in nodes)
{
modelFactoryGeneratedDocument = document;
break;
var document = project.GetDocument(declarationNode.SyntaxTree);
if (document != null && GeneratedCodeWorkspace.IsGeneratedDocument(document))
{
modelFactoryGeneratedDocument = document;
break;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ protected override string BuildName()
protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", $"{Name}.cs");

protected override TypeSignatureModifiers GetDeclarationModifiers()
=> TypeSignatureModifiers.Static | TypeSignatureModifiers.Public | TypeSignatureModifiers.Class | TypeSignatureModifiers.Partial;
{
var customModifiers = GetCustomCodeModifiers();
return customModifiers != TypeSignatureModifiers.None
? customModifiers
: TypeSignatureModifiers.Public | TypeSignatureModifiers.Static | TypeSignatureModifiers.Class | TypeSignatureModifiers.Partial;
}
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved

protected override string GetNamespace() => CodeModelPlugin.Instance.Configuration.ModelNamespace;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using Microsoft.Generator.CSharp.Tests.Common;
using NUnit.Framework;
Expand Down Expand Up @@ -37,6 +39,10 @@ public async Task CanReplaceModelMethod()
var modelFactory = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ModelFactoryProvider);
Assert.IsNotNull(modelFactory);

// The model factory should be public
Assert.IsTrue(modelFactory!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public));
ValidateModelFactoryCommon(modelFactory);

// The model factory method should be replaced
var modelFactoryMethods = modelFactory!.Methods;
Assert.AreEqual(1, modelFactoryMethods.Count);
Expand Down Expand Up @@ -71,9 +77,50 @@ public async Task DoesNotReplaceMethodIfNotCustomized()
var modelFactory = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ModelFactoryProvider);
Assert.IsNotNull(modelFactory);

// The model factory should be public
Assert.IsTrue(modelFactory!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public));
ValidateModelFactoryCommon(modelFactory);

// The model factory method should not be replaced
var modelFactoryMethods = modelFactory!.Methods;
Assert.AreEqual(1, modelFactoryMethods.Count);
}

private static void ValidateModelFactoryCommon(TypeProvider modelFactory)
{
Assert.IsTrue(modelFactory!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Static));
Assert.IsTrue(modelFactory!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Partial));
Assert.IsTrue(modelFactory!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Class));
}

[Test]
public async Task CanChangeAccessibilityOfModelFactory()
{
var plugin = await MockHelpers.LoadMockPluginAsync(
inputModelTypes: [
InputFactory.Model(
"mockInputModel",
properties:
[
InputFactory.Property("Prop1", InputPrimitiveType.String),
InputFactory.Property("OptionalBool", InputPrimitiveType.Boolean, isRequired: false)
]),
InputFactory.Model(
"otherModel",
properties: [InputFactory.Property("Prop2", InputPrimitiveType.String)]),
],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
var csharpGen = new CSharpGen();

await csharpGen.ExecuteAsync();

// Find the model factory provider
var modelFactory = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ModelFactoryProvider);
Assert.IsNotNull(modelFactory);

// The model factory should be internal
Assert.IsTrue(modelFactory!.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal));
ValidateModelFactoryCommon(modelFactory);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Generator.CSharp.Customization;
using System;

namespace Sample.Models
{
internal static partial class SampleNamespaceModelFactory
{
}
}
Loading