From bbd38496dad7f2174f8759e1fdcb394615d6510b Mon Sep 17 00:00:00 2001 From: MattMo Date: Fri, 14 Apr 2023 06:38:01 -0700 Subject: [PATCH] Bumped package version to 1.5.1, added GetNames/GetValues helper functions to Javascript Enum Type class. --- Rest.Net/Rest.Net.csproj | 2 +- Rest.Net/RestJavascriptClientGenerator.cs | 35 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index f190a4a..16512bb 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.0 + 1.5.1 Logo_Symbol_Black_Outline.png diff --git a/Rest.Net/RestJavascriptClientGenerator.cs b/Rest.Net/RestJavascriptClientGenerator.cs index bb18784..8edcedb 100644 --- a/Rest.Net/RestJavascriptClientGenerator.cs +++ b/Rest.Net/RestJavascriptClientGenerator.cs @@ -289,6 +289,41 @@ namespace MontoyaTech.Rest.Net writer.Outdent().WriteLine("}"); } + //If this is an enum, generate a GetNames/GetValues function. + if (type.IsEnum) + { + writer.WriteBreak().WriteLine("/**").Indent(); + writer.WriteLine("Returns the names of this enums values in an array."); + writer.WriteLine("@method"); + writer.WriteLine("@returns {Array}"); + writer.Outdent().WriteLine("*/"); + 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("}"); + + writer.WriteBreak().WriteLine("/**").Indent(); + writer.WriteLine("Returns the values of this enum in an arrray."); + writer.WriteLine("@method"); + writer.WriteLine("@returns {Array}"); + writer.Outdent().WriteLine("*/"); + writer.WriteLine("static GetValues() {").Indent(); + writer.WriteLine("return [").Indent(); + + var values = Enum.GetValues(type); + foreach (var value in values) + writer.WriteLine($"{Convert.ToInt32(value)},"); + + writer.Outdent().WriteLine("];"); + writer.Outdent().WriteLine("}"); + } + + //Close off the class writer.Outdent().WriteLine("}"); writer.WriteBreak().WriteLine($"{this.ClientName}.{(type.DeclaringType != null ? type.DeclaringType.Name : "")}{(newName != null ? newName.Name : type.Name)} = {(type.DeclaringType != null ? type.DeclaringType.Name : "")}{(newName != null ? newName.Name : type.Name)};");