Bumped package version to 1.5.6 added a helper function to get the name from a enum value.

This commit is contained in:
MattMo 2023-05-22 08:59:56 -07:00
parent 5e814e81c6
commit 755a2399e4
2 changed files with 25 additions and 4 deletions

View File

@ -17,7 +17,7 @@
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Version>1.5.5</Version>
<Version>1.5.6</Version>
<PackageReleaseNotes></PackageReleaseNotes>
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
</PropertyGroup>

View File

@ -291,9 +291,31 @@ namespace MontoyaTech.Rest.Net
writer.Outdent().WriteLine("}");
}
//If this is an enum, generate a GetNames/GetValues function.
//If this is an enum, generate a GetNames/GetValues/GetName function.
if (type.IsEnum)
{
var names = Enum.GetNames(type);
var values = Enum.GetValues(type);
//GetName function
writer.WriteBreak().WriteLine("/**").Indent();
writer.WriteLine("Returns the name of a value in this enum. Returns null if the value is invalid.");
writer.WriteLine("@function");
writer.WriteLine("@param {number} value");
writer.WriteLine("@returns {string}");
writer.Outdent().WriteLine("*/");
writer.WriteLine("static GetName(value) {").Indent();
writer.WriteLine("switch (value) {").Indent();
for (int i = 0; i < values.Length; i++)
writer.WriteLine($"case {Convert.ToInt32(values.GetValue(i))}: return `{names.GetValue(i)}`;");
writer.Outdent().WriteLine("}");
writer.WriteLine("return null;");
writer.Outdent().WriteLine("}");
//GetNames function
writer.WriteBreak().WriteLine("/**").Indent();
writer.WriteLine("Returns the names of this enums values in an array.");
writer.WriteLine("@function");
@ -302,13 +324,13 @@ namespace MontoyaTech.Rest.Net
writer.WriteLine("static GetNames() {").Indent();
writer.WriteLine("return [").Indent();
var names = Enum.GetNames(type);
foreach (var name in names)
writer.WriteLine($"'{name}',");
writer.Outdent().WriteLine("];");
writer.Outdent().WriteLine("}");
//GetValues function
writer.WriteBreak().WriteLine("/**").Indent();
writer.WriteLine("Returns the values of this enum in an arrray.");
writer.WriteLine("@function");
@ -317,7 +339,6 @@ namespace MontoyaTech.Rest.Net
writer.WriteLine("static GetValues() {").Indent();
writer.WriteLine("return [").Indent();
var values = Enum.GetValues(type);
foreach (var value in values)
writer.WriteLine($"{Convert.ToInt32(value)},");