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 1 commit
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.20250218.1" />
</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 @@ -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.Namespace = AzureClientPlugin.Instance.TypeFactory.GetCleanNameSpace(AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name);
}
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.Namespace = AzureClientPlugin.Instance.TypeFactory.GetCleanNameSpace($"{AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name}.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.InputLibrary.InputNamespace.Name} client library for developing .NET applications with rich experience.",
AssemblyTitle = $"SDK Code Generation {AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name}",
Version = "1.0.0-beta.1",
PackageTags = AzureClientPlugin.Instance.TypeFactory.RootNamespace,
PackageTags = AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name,
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 @@ -162,7 +162,7 @@ protected override string GetSolutionFileContent()
EndGlobalSection
EndGlobal
";
return string.Format(slnContent, AzureClientPlugin.Instance.TypeFactory.RootNamespace);
return string.Format(slnContent, AzureClientPlugin.Instance.InputLibrary.InputNamespace.Name);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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)
{
base.Visit(model, type);
if (type is not null)
{
TransformResource(type);
}
return type;
}

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

private static void TransformResource(TypeProvider type)
{
if (type is ModelProvider && AzureClientPlugin.Instance.OutputLibrary.IsResource(type.Name))
{
type.RelativeFilePath = TransformRelativeFilePath(type);
type.Name = TransformName(type);
type.Type.Name = type.Name;
foreach (var serialization in type.SerializationProviders)
{
serialization.RelativeFilePath = TransformRelativeFilePathForSerialization(serialization);
serialization.Name = 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 @@ -19,7 +19,8 @@ internal class RestClientVisitor : ScmLibraryVisitor

if (type is ClientProvider)
{
type.Update(modifiers: TransfromPublicModifiersToInternal(type), relativeFilePath: TransformRelativeFilePathForClient(type));
type.DeclarationModifiers = TransfromPublicModifiersToInternal(type);
type.RelativeFilePath = TransformRelativeFilePathForClient(type);
}
// TODO: uncomment this once resources are generated
//if (type is RestClientProvider)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"output-folder": ".",
"namespace": "BasicTypeSpec",
"library-name": "BasicTypeSpec"
"package-name": "BasicTypeSpec"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"output-folder": ".",
"namespace": "MgmtTypeSpec",
"library-name": "MgmtTypeSpec"
"package-name": "MgmtTypeSpec"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading