Skip to content

Commit 3bed27a

Browse files
authored
Add NameVisitor (#50869)
1 parent 76fe086 commit 3bed27a

File tree

10 files changed

+6465
-6710
lines changed

10 files changed

+6465
-6710
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,6 @@
451451
<PropertyGroup>
452452
<TestProxyVersion>1.0.0-dev.20250501.1</TestProxyVersion>
453453
<UnbrandedGeneratorVersion>1.0.0-alpha.20250627.2</UnbrandedGeneratorVersion>
454-
<AzureGeneratorVersion>1.0.0-alpha.20250625.3</AzureGeneratorVersion>
454+
<AzureGeneratorVersion>1.0.0-alpha.20250629.1</AzureGeneratorVersion>
455455
</PropertyGroup>
456456
</Project>

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/ManagementClientGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ protected override void Configure()
5252
AddVisitor(new RestClientVisitor());
5353
AddVisitor(new ResourceVisitor());
5454
AddVisitor(new InheritableSystemObjectModelVisitor());
55+
AddVisitor(new NameVisitor());
5556
}
5657
}
5758
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.TypeSpec.Generator.ClientModel;
5+
using Microsoft.TypeSpec.Generator.Input;
6+
using Microsoft.TypeSpec.Generator.Providers;
7+
using System;
8+
using System.Diagnostics.CodeAnalysis;
9+
10+
namespace Azure.Generator.Management
11+
{
12+
internal class NameVisitor : ScmLibraryVisitor
13+
{
14+
protected override ModelProvider? PreVisitModel(InputModelType model, ModelProvider? type)
15+
{
16+
if (type is not null && TryTransformUrlToUri(model.Name, out var newName))
17+
{
18+
type.Update(name: newName);
19+
}
20+
21+
foreach (var property in model.Properties)
22+
{
23+
if (property is InputModelProperty modelProperty && TryTransformUrlToUri(property.Name, out var newPropertyName))
24+
{
25+
modelProperty.Update(name: newPropertyName);
26+
}
27+
}
28+
return base.PreVisitModel(model, type);
29+
}
30+
31+
private bool TryTransformUrlToUri(string name, [MaybeNullWhen(false)] out string newName)
32+
{
33+
const char i = 'i';
34+
const string UrlSuffix = "Url";
35+
newName = null;
36+
if (name.Length < UrlSuffix.Length)
37+
{
38+
return false;
39+
}
40+
41+
var span = name.AsSpan();
42+
// check if this ends with `Url`
43+
if (span.EndsWith(UrlSuffix.AsSpan(), StringComparison.Ordinal))
44+
{
45+
Span<char> newSpan = span.ToArray();
46+
newSpan[^1] = i;
47+
48+
newName = new string(newSpan);
49+
return true;
50+
}
51+
52+
return false;
53+
}
54+
}
55+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Azure.Generator.Management;
5+
using Azure.Generator.Management.Tests.TestHelpers;
6+
using Azure.Generator.Tests.Common;
7+
using Microsoft.TypeSpec.Generator.Input;
8+
using Microsoft.TypeSpec.Generator.Providers;
9+
using NUnit.Framework;
10+
11+
namespace Azure.Generator.Mgmt.Tests
12+
{
13+
internal class NameVisitorTests
14+
{
15+
private const string TestClientName = "TestClient";
16+
private const string TestModelName = "TestModelUrl";
17+
private const string TestProtyName = "TestPropertyUrl";
18+
19+
[Test]
20+
public void TestTransformUrlToUri()
21+
{
22+
var modelProperty = InputFactory.Property(TestProtyName, InputPrimitiveType.String, serializedName: "testName", isRequired: true);
23+
var model = InputFactory.Model(TestModelName, properties: [modelProperty]);
24+
var responseType = InputFactory.OperationResponse(statusCodes: [200], bodytype: model);
25+
var testNameParameter = InputFactory.Parameter("testName", InputPrimitiveType.String, location: InputRequestLocation.Path);
26+
var operation = InputFactory.Operation(name: "get", responses: [responseType], parameters: [testNameParameter], path: "/providers/a/test/{testName}", decorators: []);
27+
28+
var client = InputFactory.Client(
29+
TestClientName,
30+
methods: [InputFactory.BasicServiceMethod("Get", operation, parameters: [testNameParameter])],
31+
crossLanguageDefinitionId: $"Test.{TestClientName}",
32+
decorators: []);
33+
34+
var plugin = ManagementMockHelpers.LoadMockPlugin(inputModels: () => [model], clients: () => [client]);
35+
var visitor = new TestVisitor();
36+
var type = plugin.Object.TypeFactory.CreateModel(model);
37+
var transformedModel = visitor.InvokeVisit(model, type);
38+
Assert.That(transformedModel?.Name, Is.EqualTo(TestModelName.Replace("Url", "Uri")));
39+
Assert.That(transformedModel?.Properties[0].Name, Is.EqualTo(TestProtyName.Replace("Url", "Uri")));
40+
}
41+
42+
private class TestVisitor : NameVisitor
43+
{
44+
public ModelProvider? InvokeVisit(InputModelType model, ModelProvider? type)
45+
{
46+
return base.PreVisitModel(model, type);
47+
}
48+
}
49+
}
50+
}

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/MgmtTypeSpecModelFactory.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Models/FooProperties.Serialization.cs

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Models/FooProperties.cs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)