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

Replace ResourceDataProvdier with ResoruceVisitor #48362

Merged
merged 11 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion eng/Packages.Data.props
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@
</ItemGroup>

<ItemGroup Condition="'$(IsGeneratorLibrary)' == 'true'">
<PackageReference Update="Microsoft.TypeSpec.Generator.ClientModel" Version="1.0.0-alpha.20250211.5" />
<PackageReference Update="Microsoft.TypeSpec.Generator.ClientModel" Version="1.0.0-alpha.20250220.3" />
</ItemGroup>

<!--
Expand Down
4 changes: 2 additions & 2 deletions eng/packages/http-client-csharp/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { EmitContext } from "@typespec/compiler";

import {
$onEmit as $OnMGCEmit,
NetEmitterOptions,
CSharpEmitterOptions,
setSDKContextOptions
} from "@typespec/http-client-csharp";
import { azureSDKContextOptions } from "./sdk-context-options.js";

export async function $onEmit(context: EmitContext<NetEmitterOptions>) {
export async function $onEmit(context: EmitContext<CSharpEmitterOptions>) {
context.options["plugin-name"] ??= "AzureClientPlugin";
context.options["emitter-extension-path"] = import.meta.url;
setSDKContextOptions(azureSDKContextOptions);
Expand Down
1 change: 1 addition & 0 deletions eng/packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ $failingSpecs = @(
Join-Path 'http' 'encode' 'datetime'
Join-Path 'http' 'encode' 'duration'
Join-Path 'http' 'parameters' 'collection-format'
Join-Path 'http' 'response' 'status-code-range' # Response namespace conflicts with Azure.Response
Join-Path 'http' 'routes'
Join-Path 'http' 'type' 'array'
Join-Path 'http' 'type' 'dictionary'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Azure.Generator;
/// </summary>
[Export(typeof(CodeModelPlugin))]
[ExportMetadata("PluginName", nameof(AzureClientPlugin))]
public class AzureClientPlugin : ClientModelPlugin
public class AzureClientPlugin : ScmCodeModelPlugin
{
private static AzureClientPlugin? _instance;
internal static AzureClientPlugin Instance => _instance ?? throw new InvalidOperationException("AzureClientPlugin is not loaded.");
Expand Down Expand Up @@ -56,6 +56,7 @@ public override void Configure()
if (IsAzureArm.Value)
{
AddVisitor(new RestClientVisitor());
AddVisitor(new ResourceVisitor());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,6 @@ public override MethodBodyStatement SerializeJsonValue(Type valueType, ValueExpr
return transformedClient is null ? null : base.CreateClientCore(transformedClient);
}

/// <inheritdoc/>
protected override IReadOnlyList<TypeProvider> CreateSerializationsCore(InputType inputType, TypeProvider typeProvider)
{
if (inputType is InputModelType inputModel
&& typeProvider is ModelProvider modelProvider
&& AzureClientPlugin.Instance.OutputLibrary.IsResource(inputType.Name)
&& inputModel.Usage.HasFlag(InputModelTypeUsage.Json))
{
return [new ResourceDataSerializationProvider(inputModel, modelProvider)];
}

return base.CreateSerializationsCore(inputType, typeProvider);
}

/// <inheritdoc/>
protected override ModelProvider? CreateModelCore(InputModelType model)
{
if (AzureClientPlugin.Instance.OutputLibrary.IsResource(model.Name))
{
return new ResourceDataProvider(model);
}
return base.CreateModelCore(model);
}

/// <inheritdoc/>
public override NewProjectScaffolding CreateNewProjectScaffolding()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class NamespaceVisitor : ScmLibraryVisitor
}
else
{
type.Type.Namespace = AzureClientPlugin.Instance.TypeFactory.RootNamespace;
type.Type.Update(@namespace: AzureClientPlugin.Instance.TypeFactory.PackageName);
}
return type;
}
Expand All @@ -57,7 +57,7 @@ private static void UpdateModelsNamespace(TypeProvider type)
// TODO: need to take consideration of model-namespace configuration
// if model-namespace is false, set namespace to $"{AzureClientPlugin.Instance.TypeFactory.RootNamespace}"
// if model-namespace is true, set namespace to $"{AzureClientPlugin.Instance.TypeFactory.RootNamespace}.Models"
type.Type.Namespace = AzureClientPlugin.Instance.TypeFactory.GetCleanNameSpace($"{AzureClientPlugin.Instance.TypeFactory.RootNamespace}.Models");
type.Type.Update(@namespace: AzureClientPlugin.Instance.TypeFactory.GetCleanNameSpace($"{AzureClientPlugin.Instance.TypeFactory.PackageName}.Models"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ protected override string GetSourceProjectFileContent()
{
var builder = new CSharpProjectWriter()
{
Description = $"This is the {AzureClientPlugin.Instance.TypeFactory.RootNamespace} client library for developing .NET applications with rich experience.",
AssemblyTitle = $"SDK Code Generation {AzureClientPlugin.Instance.TypeFactory.RootNamespace}",
Description = $"This is the {AzureClientPlugin.Instance.TypeFactory.PackageName} client library for developing .NET applications with rich experience.",
AssemblyTitle = $"SDK Code Generation {AzureClientPlugin.Instance.TypeFactory.PackageName}",
Version = "1.0.0-beta.1",
PackageTags = AzureClientPlugin.Instance.TypeFactory.RootNamespace,
PackageTags = AzureClientPlugin.Instance.TypeFactory.PackageName,
GenerateDocumentationFile = true,
};

Expand All @@ -36,7 +36,7 @@ protected override string GetSourceProjectFileContent()
}

int pathSegmentCount = GetPathSegmentCount();
if (AzureClientPlugin.Instance.InputLibrary.InputNamespace.Auth.ApiKey is not null)
if (AzureClientPlugin.Instance.InputLibrary.InputNamespace.Auth?.ApiKey is not null)
{
builder.CompileIncludes.Add(new CSharpProjectWriter.CSProjCompileInclude(GetCompileInclude("AzureKeyCredentialPolicy.cs", pathSegmentCount), "Shared/Core"));
}
Expand Down Expand Up @@ -133,36 +133,5 @@ private string GetCompileInclude(string fileName, int pathSegmentCount)
new("System.ClientModel"),
new("System.Text.Json")
];

/// <inheritdoc/>
protected override string GetSolutionFileContent()
{
string slnContent = @"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.35201.131
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}"") = ""{0}"", ""src\{0}.csproj"", ""{{28FF4005-4467-4E36-92E7-DEA27DEB1519}}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{{28FF4005-4467-4E36-92E7-DEA27DEB1519}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{{28FF4005-4467-4E36-92E7-DEA27DEB1519}}.Debug|Any CPU.Build.0 = Debug|Any CPU
{{28FF4005-4467-4E36-92E7-DEA27DEB1519}}.Release|Any CPU.ActiveCfg = Release|Any CPU
{{28FF4005-4467-4E36-92E7-DEA27DEB1519}}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {{A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}}
EndGlobalSection
EndGlobal
";
return string.Format(slnContent, AzureClientPlugin.Instance.TypeFactory.RootNamespace);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.TypeSpec.Generator.ClientModel;
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Providers;
using System.IO;

namespace Azure.Generator
{
internal class ResourceVisitor : ScmLibraryVisitor
{
protected override ModelProvider? Visit(InputModelType model, ModelProvider? type)
{
if (type is not null)
{
TransformResource(type);
}
return type;
}

protected override TypeProvider? Visit(TypeProvider type)
{
TransformResource(type);
return type;
}

private static void TransformResource(TypeProvider type)
{
if (type is ModelProvider && AzureClientPlugin.Instance.OutputLibrary.IsResource(type.Name))
{
type.Update(relativeFilePath: TransformRelativeFilePath(type));
type.Type.Update(TransformName(type));
foreach (var serialization in type.SerializationProviders)
{
serialization.Update(relativeFilePath: TransformRelativeFilePathForSerialization(serialization));
serialization.Type.Update(TransformName(serialization));
}
}
}

private static string TransformName(TypeProvider model) => $"{model.Name}Data";

private static string TransformRelativeFilePath(TypeProvider model)
=> Path.Combine("src", "Generated", $"{TransformName(model)}.cs");

private static string TransformRelativeFilePathForSerialization(TypeProvider model)
=> Path.Combine("src", "Generated", $"{TransformName(model)}.Serialization.cs");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ internal class RestClientVisitor : ScmLibraryVisitor
/// <inheritdoc/>
protected override TypeProvider? Visit(TypeProvider type)
{
base.Visit(type);

if (type is ClientProvider)
if (type is not null && type is ClientProvider)
{
type.Update(modifiers: TransfromPublicModifiersToInternal(type), relativeFilePath: TransformRelativeFilePathForClient(type));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public static OperationResponse OperationResponse(IEnumerable<int>? statusCodes
["application/json"]);
}

public static InputClient Client(string name, string clientNamespace = "Sample", string? doc = null, IEnumerable<InputOperation>? operations = null, IEnumerable<InputParameter>? parameters = null, string? parent = null)
public static InputClient Client(string name, string clientNamespace = "Samples", string? doc = null, IEnumerable<InputOperation>? operations = null, IEnumerable<InputParameter>? parameters = null, string? parent = null)
{
return new InputClient(
name,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"output-folder": "./outputFolder",
"project-folder": "./projectFolder",
"unknown-bool-property": false,
"library-name": "sample-library",
"package-name": "sample-library",
"unknown-string-property": "unknownPropertyValue"
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static Mock<AzureClientPlugin> LoadMockPlugin(

// initialize the mock singleton instance of the plugin
var codeModelInstance = typeof(CodeModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
var clientModelInstance = typeof(ClientModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
var clientModelInstance = typeof(ScmCodeModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
var azureInstance = typeof(AzureClientPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic);
// invoke the load method with the config file path
var loadMethod = typeof(Configuration).GetMethod("Load", BindingFlags.Static | BindingFlags.NonPublic);
Expand Down
Loading
Loading