From 755a2399e4d05ae320565d1d0f8f2cf132e97afa Mon Sep 17 00:00:00 2001 From: MattMo Date: Mon, 22 May 2023 08:59:56 -0700 Subject: [PATCH] Bumped package version to 1.5.6 added a helper function to get the name from a enum value. --- Rest.Net/Rest.Net.csproj | 2 +- Rest.Net/RestJavascriptClientGenerator.cs | 27 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 6b5f2dd..a3ada43 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.5.5 + 1.5.6 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/RestJavascriptClientGenerator.cs b/Rest.Net/RestJavascriptClientGenerator.cs index 59a623e..b49c9e8 100644 --- a/Rest.Net/RestJavascriptClientGenerator.cs +++ b/Rest.Net/RestJavascriptClientGenerator.cs @@ -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)},");