Skip to content

Commit

Permalink
Expose public setter of namespace (#5928)
Browse files Browse the repository at this point in the history
After the tsp namespace feature implementation in
#5443, the namespace of model,
enum and modelFactory have been changed from previous `XXX.Models` to
`XXX`.
For Azure plugin, we would like the namespace for model and enum to be
`XXX.Models`.

- Expose public setter of namespace, use visitor to update namespace of
TypeProvider in Azure plugin.
https://github.com/Azure/azure-sdk-for-net/pull/48197/files#diff-a6219e0492ffcf33fd4bb03be26f1e6bc04a84ee1e2723b4d58d5adbcdb031ef
- Expose provider types to let sub-plugin identify.
- Remove duplicated instance of ModelFactoryProvider
- Remove Namespace property from CSharp
  • Loading branch information
live1206 authored Feb 12, 2025
1 parent 2642f4a commit ffef215
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
/// <summary>
/// This defines a class with extension methods for enums to convert an enum to its underlying value, or from its underlying value to an instance of the enum
/// </summary>
internal partial class ExtensibleEnumSerializationProvider : TypeProvider
public partial class ExtensibleEnumSerializationProvider : TypeProvider
{
private readonly InputEnumType _enumType;
private TypeProvider _enumProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
/// <summary>
/// This defines a class with extension methods for enums to convert an enum to its underlying value, or from its underlying value to an instance of the enum
/// </summary>
internal class FixedEnumSerializationProvider : TypeProvider
public class FixedEnumSerializationProvider : TypeProvider
{
private readonly InputEnumType _enumType;
private TypeProvider _enumProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public RestClientProvider(InputClient inputClient, ClientProvider clientProvider

protected override string BuildName() => _inputClient.Name.ToCleanName();

protected override string BuildNamespace() => ClientProvider.Namespace;
protected override string BuildNamespace() => ClientProvider.Type.Namespace;

protected override PropertyProvider[] BuildProperties()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ private static class Options
/// </summary>
internal bool ClearOutputFolder { get; private set; }

/// <summary>
/// Whether we will generate model factory for this library.
/// If true (default), the model factory will be generated. If false, the model factory will not be generated.
/// </summary>
internal bool GenerateModelFactory { get; private set; }

/// <summary>
/// True if a sample project should be generated.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.TypeSpec.Generator.Providers;
Expand All @@ -16,6 +17,8 @@ public IReadOnlyList<TypeProvider> TypeProviders
internal set => _typeProviders = value;
}

internal Lazy<ModelFactoryProvider> ModelFactory { get; } = new(() => new ModelFactoryProvider(CodeModelPlugin.Instance.InputLibrary.InputNamespace.Models));

private static TypeProvider[] BuildEnums()
{
var input = CodeModelPlugin.Instance.InputLibrary.InputNamespace;
Expand Down Expand Up @@ -79,9 +82,9 @@ .. BuildModelFactory()
];
}

private static TypeProvider[] BuildModelFactory()
private TypeProvider[] BuildModelFactory()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = ModelFactory.Value;
return modelFactory.Methods.Count > 0 ? [modelFactory] : [];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ internal static Project AddDirectory(Project project, string directory, Func<str
/// </summary>
public async Task PostProcessAsync()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = CodeModelPlugin.Instance.OutputLibrary.ModelFactory.Value;
var postProcessor = new PostProcessor(
[.. CodeModelPlugin.Instance.TypeFactory.UnionTypes, .. CodeModelPlugin.Instance.TypesToKeep],
modelFactoryFullName: $"{modelFactory.Namespace}.{modelFactory.Name}");
modelFactoryFullName: $"{modelFactory.Type.Namespace}.{modelFactory.Name}");
switch (Configuration.UnreferencedTypesHandling)
{
case Configuration.UnreferencedTypesHandlingOption.KeepAll:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ internal CSharpType(
_underlyingType = underlyingEnumType;
}

public string Namespace { get; private init; }
public string Namespace { get; set; }
public string Name { get; private init; }
public CSharpType? DeclaringType { get; private init; }
public bool IsValueType { get; private init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public CanonicalTypeProvider(TypeProvider generatedTypeProvider, InputType? inpu

protected override string BuildName() => _generatedTypeProvider.Name;

protected override string BuildNamespace() => _generatedTypeProvider.Namespace;
protected override string BuildNamespace() => _generatedTypeProvider.Type.Namespace;

protected override TypeSignatureModifiers BuildDeclarationModifiers() => _generatedTypeProvider.DeclarationModifiers;

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

namespace Microsoft.TypeSpec.Generator.Providers
{
internal abstract class EnumProvider : TypeProvider
public abstract class EnumProvider : TypeProvider
{
private readonly InputEnumType _inputType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@

namespace Microsoft.TypeSpec.Generator.Providers
{
internal class ModelFactoryProvider : TypeProvider
public class ModelFactoryProvider : TypeProvider
{
private const string ModelFactorySuffix = "ModelFactory";
private const string AdditionalBinaryDataParameterName = "additionalBinaryDataProperties";

private readonly IEnumerable<InputModelType> _models;

public static ModelFactoryProvider FromInputLibrary() => new ModelFactoryProvider(CodeModelPlugin.Instance.InputLibrary.InputNamespace.Models);

private ModelFactoryProvider(IEnumerable<InputModelType> models)
internal ModelFactoryProvider(IEnumerable<InputModelType> models)
{
_models = models;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ private IReadOnlyList<FieldProvider> BuildAllCustomFields()

private string? _name;

public string Namespace => _namespace ??= BuildNamespace();
private string? _namespace;

protected virtual FormattableString Description { get; } = FormattableStringHelpers.Empty;

private XmlDocProvider? _xmlDocs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void ValidateName()
[Test]
public void ValidateNamespace()
{
Assert.AreEqual(_typeProvider.Namespace, _typeProvider.CanonicalView.Namespace);
Assert.AreEqual(_typeProvider.Type.Namespace, _typeProvider.CanonicalView.Type.Namespace);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@ namespace Microsoft.TypeSpec.Generator.Tests.Providers.ModelFactories
public class ModelFactoryProviderTests
{
private static readonly InputModelType[] ModelList = GetTestModels();
private CodeModelPlugin _instance;

public ModelFactoryProviderTests()
{
MockHelpers.LoadMockPlugin(inputModelTypes: ModelList);
_instance = MockHelpers.LoadMockPlugin(inputModelTypes: ModelList).Object;
}

[Test]
public void SkipInternalModels()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = _instance.OutputLibrary.ModelFactory.Value;
Assert.AreEqual(ModelList.Length - ModelList.Where(m => m.Access == "internal").Count(), modelFactory.Methods.Count);
}

[Test]
public void ListParamShape()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = _instance.OutputLibrary.ModelFactory.Value;
var models = ModelList.Select(CodeModelPlugin.Instance.TypeFactory.CreateModel);
foreach (var model in models)
{
Expand All @@ -54,7 +55,7 @@ public void ListParamShape()
[Test]
public void DictionaryParamShape()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = _instance.OutputLibrary.ModelFactory.Value;
var models = ModelList.Select(CodeModelPlugin.Instance.TypeFactory.CreateModel);
foreach (var model in models)
{
Expand All @@ -77,7 +78,7 @@ public void DictionaryParamShape()
[Test]
public void DiscriminatorEnumParamShape()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = _instance.OutputLibrary.ModelFactory.Value;
var models = ModelList.Select(CodeModelPlugin.Instance.TypeFactory.CreateModel);
foreach (var model in models)
{
Expand All @@ -99,7 +100,7 @@ public void DiscriminatorEnumParamShape()
[Test]
public void AdditionalPropertiesParamShape()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = _instance.OutputLibrary.ModelFactory.Value;
var models = ModelList.Select(CodeModelPlugin.Instance.TypeFactory.CreateModel);
foreach (var model in models)
{
Expand All @@ -123,7 +124,7 @@ public void AdditionalPropertiesParamShape()
[Test]
public void ModelFactoryName()
{
var modelFactory = ModelFactoryProvider.FromInputLibrary();
var modelFactory = _instance.OutputLibrary.ModelFactory.Value;
Assert.AreEqual("SampleNamespaceModelFactory", modelFactory.Name);
}

Expand Down

0 comments on commit ffef215

Please sign in to comment.