-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support customizing enum member name (#4485)
This PR adds support for customizing enum member names, including in the serialization code for an enum. Fixes: #4372
- Loading branch information
1 parent
48099f0
commit d62adb1
Showing
40 changed files
with
278 additions
and
90 deletions.
There are no files selected for viewing
26 changes: 0 additions & 26 deletions
26
packages/http-client-csharp/generator/Microsoft.Generator.CSharp.ClientModel/test/Helpers.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...nerator.CSharp.ClientModel/test/Providers/EnumProvider/SerializationCustomizationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Generator.CSharp.ClientModel.Providers; | ||
using Microsoft.Generator.CSharp.Input; | ||
using Microsoft.Generator.CSharp.Primitives; | ||
using Microsoft.Generator.CSharp.Tests.Common; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Generator.CSharp.ClientModel.Tests.Providers.EnumProvider | ||
{ | ||
public class SerializationCustomizationTests | ||
{ | ||
[Test] | ||
public async Task CanChangeEnumMemberName() | ||
{ | ||
var enumValues = new[] | ||
{ | ||
InputFactory.EnumMember.Int32("Red", 1), | ||
InputFactory.EnumMember.Int32("Green", 2), | ||
InputFactory.EnumMember.Int32("Blue", 3) | ||
}; | ||
var inputEnum = InputFactory.Enum("mockInputModel", underlyingType: InputPrimitiveType.String, values: enumValues); | ||
var plugin = await MockHelpers.LoadMockPluginAsync( | ||
inputEnums: () => [inputEnum], | ||
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync()); | ||
|
||
Assert.IsNull(plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t.IsEnum)); | ||
|
||
var serializationProvider = plugin.Object.OutputLibrary.TypeProviders.Single(t => t is FixedEnumSerializationProvider); | ||
Assert.IsNotNull(serializationProvider); | ||
Assert.AreEqual(0, serializationProvider!.Fields.Count); | ||
|
||
// validate the methods use the custom member name | ||
var writer = new TypeProviderWriter(serializationProvider); | ||
var file = writer.Write(); | ||
Assert.AreEqual(Helpers.GetExpectedFromFile(), file.Content); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...roviders/EnumProvider/TestData/SerializationCustomizationTests/CanChangeEnumMemberName.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// <auto-generated/> | ||
|
||
#nullable disable | ||
|
||
using System; | ||
|
||
namespace Sample.Models | ||
{ | ||
internal static partial class MockInputModelExtensions | ||
{ | ||
public static string ToSerialString(this global::Sample.Models.MockInputModel value) => value switch | ||
{ | ||
global::Sample.Models.MockInputModel.Red => 1, | ||
global::Sample.Models.MockInputModel.Green => 2, | ||
global::Sample.Models.MockInputModel.SkyBlue => 3, | ||
_ => throw new global::System.ArgumentOutOfRangeException(nameof(value), value, "Unknown MockInputModel value.") | ||
}; | ||
|
||
public static global::Sample.Models.MockInputModel ToMockInputModel(this string value) | ||
{ | ||
if (string.Equals(value, 1, global::System.StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
return global::Sample.Models.MockInputModel.Red; | ||
} | ||
if (string.Equals(value, 2, global::System.StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
return global::Sample.Models.MockInputModel.Green; | ||
} | ||
if (string.Equals(value, 3, global::System.StringComparison.InvariantCultureIgnoreCase)) | ||
{ | ||
return global::Sample.Models.MockInputModel.SkyBlue; | ||
} | ||
throw new global::System.ArgumentOutOfRangeException(nameof(value), value, "Unknown MockInputModel value."); | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...ovider/TestData/SerializationCustomizationTests/CanChangeEnumMemberName/MockInputModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#nullable disable | ||
|
||
using Microsoft.Generator.CSharp.Customization; | ||
|
||
namespace Sample.Models | ||
{ | ||
public enum MockInputModel | ||
{ | ||
Red, | ||
Green, | ||
[CodeGenMember("Blue")] | ||
SkyBlue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.