From e11d956328ff9fc4b6c069e92c5969c4c27ae6d7 Mon Sep 17 00:00:00 2001 From: Joel Hendrix Date: Tue, 6 Mar 2018 09:38:13 -0800 Subject: [PATCH] Generate func returning all values for an enum type. For each enum const type, generate a function that returns an array of all the possible values for that type. --- src/Templates/EnumTemplate.cshtml | 10 +++++++++- test/src/tests/generated/body-complex/models.go | 10 ++++++++++ test/src/tests/generated/body-string/models.go | 5 +++++ test/src/tests/generated/header/models.go | 5 +++++ test/src/tests/generated/lro/models.go | 15 +++++++++++++++ .../tests/generated/model-flattening/models.go | 5 +++++ test/src/tests/generated/paging/models.go | 5 +++++ test/src/tests/generated/url/models.go | 5 +++++ test/src/tests/generated/validation/models.go | 5 +++++ 9 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/Templates/EnumTemplate.cshtml b/src/Templates/EnumTemplate.cshtml index 63b569f8c..b109a971b 100644 --- a/src/Templates/EnumTemplate.cshtml +++ b/src/Templates/EnumTemplate.cshtml @@ -31,11 +31,15 @@ type @Model.Name string @EmptyLine @if (Model.Values.Any()) { + var possibleFuncName = $"Possible{Model.Name}Values"; + var orderedValues = new List(); const ( @foreach (var v in Model.Values.OrderBy(v => v.Name)) { var comment = string.Empty; + var memName = CodeNamerGo.Instance.GetEnumMemberName(v.Name); + orderedValues.Add(memName); if (string.IsNullOrWhiteSpace(v.Description)) { @@ -53,9 +57,13 @@ type @Model.Name string } - @(CodeNamerGo.Instance.GetEnumMemberName(v.Name)) @(Model.Name) = "@(v.SerializedName)" + @(memName) @(Model.Name) = "@(v.SerializedName)" } ) + // @possibleFuncName returns an array of possible values for the @Model.Name const type. + func @(possibleFuncName)() [@(Model.Values.Count)]@Model.Name { + return [@(Model.Values.Count)]@(Model.Name){@(string.Join(',', orderedValues))} + } } diff --git a/test/src/tests/generated/body-complex/models.go b/test/src/tests/generated/body-complex/models.go index 5104a81af..25b148e72 100644 --- a/test/src/tests/generated/body-complex/models.go +++ b/test/src/tests/generated/body-complex/models.go @@ -26,6 +26,11 @@ const ( YELLOW CMYKColors = "YELLOW" ) +// PossibleCMYKColorsValues returns an array of possible values for the CMYKColors const type. +func PossibleCMYKColorsValues() [4]CMYKColors { + return [4]CMYKColors{BlacK, Cyan, Magenta, YELLOW} +} + // Fishtype enumerates the values for fishtype. type Fishtype string @@ -46,6 +51,11 @@ const ( FishtypeSmartSalmon Fishtype = "smart_salmon" ) +// PossibleFishtypeValues returns an array of possible values for the Fishtype const type. +func PossibleFishtypeValues() [7]Fishtype { + return [7]Fishtype{FishtypeCookiecuttershark, FishtypeFish, FishtypeGoblin, FishtypeSalmon, FishtypeSawshark, FishtypeShark, FishtypeSmartSalmon} +} + // ArrayWrapper ... type ArrayWrapper struct { autorest.Response `json:"-"` diff --git a/test/src/tests/generated/body-string/models.go b/test/src/tests/generated/body-string/models.go index 485044d17..df8c56b9b 100644 --- a/test/src/tests/generated/body-string/models.go +++ b/test/src/tests/generated/body-string/models.go @@ -22,6 +22,11 @@ const ( Redcolor Colors = "red color" ) +// PossibleColorsValues returns an array of possible values for the Colors const type. +func PossibleColorsValues() [3]Colors { + return [3]Colors{BlueColor, GreenColor, Redcolor} +} + // Base64URL ... type Base64URL struct { autorest.Response `json:"-"` diff --git a/test/src/tests/generated/header/models.go b/test/src/tests/generated/header/models.go index 4f18f0427..603f2056c 100644 --- a/test/src/tests/generated/header/models.go +++ b/test/src/tests/generated/header/models.go @@ -18,6 +18,11 @@ const ( White GreyscaleColors = "White" ) +// PossibleGreyscaleColorsValues returns an array of possible values for the GreyscaleColors const type. +func PossibleGreyscaleColorsValues() [3]GreyscaleColors { + return [3]GreyscaleColors{Black, GREY, White} +} + // Error ... type Error struct { Status *int32 `json:"status,omitempty"` diff --git a/test/src/tests/generated/lro/models.go b/test/src/tests/generated/lro/models.go index 6cd2496cf..662009d23 100644 --- a/test/src/tests/generated/lro/models.go +++ b/test/src/tests/generated/lro/models.go @@ -41,6 +41,11 @@ const ( Updating ProvisioningStateValues = "Updating" ) +// PossibleProvisioningStateValuesValues returns an array of possible values for the ProvisioningStateValues const type. +func PossibleProvisioningStateValuesValues() [11]ProvisioningStateValues { + return [11]ProvisioningStateValues{Accepted, Canceled, Created, Creating, Deleted, Deleting, Failed, OK, Succeeded, Updated, Updating} +} + // ProvisioningStateValues1 enumerates the values for provisioning state values 1. type ProvisioningStateValues1 string @@ -69,6 +74,11 @@ const ( ProvisioningStateValues1Updating ProvisioningStateValues1 = "Updating" ) +// PossibleProvisioningStateValues1Values returns an array of possible values for the ProvisioningStateValues1 const type. +func PossibleProvisioningStateValues1Values() [11]ProvisioningStateValues1 { + return [11]ProvisioningStateValues1{ProvisioningStateValues1Accepted, ProvisioningStateValues1Canceled, ProvisioningStateValues1Created, ProvisioningStateValues1Creating, ProvisioningStateValues1Deleted, ProvisioningStateValues1Deleting, ProvisioningStateValues1Failed, ProvisioningStateValues1OK, ProvisioningStateValues1Succeeded, ProvisioningStateValues1Updated, ProvisioningStateValues1Updating} +} + // Status enumerates the values for status. type Status string @@ -97,6 +107,11 @@ const ( StatusUpdating Status = "Updating" ) +// PossibleStatusValues returns an array of possible values for the Status const type. +func PossibleStatusValues() [11]Status { + return [11]Status{StatusAccepted, StatusCanceled, StatusCreated, StatusCreating, StatusDeleted, StatusDeleting, StatusFailed, StatusOK, StatusSucceeded, StatusUpdated, StatusUpdating} +} + // CloudError ... type CloudError struct { Status *int32 `json:"status,omitempty"` diff --git a/test/src/tests/generated/model-flattening/models.go b/test/src/tests/generated/model-flattening/models.go index bc9edd70a..84f66481c 100644 --- a/test/src/tests/generated/model-flattening/models.go +++ b/test/src/tests/generated/model-flattening/models.go @@ -39,6 +39,11 @@ const ( Updating ProvisioningStateValues = "Updating" ) +// PossibleProvisioningStateValuesValues returns an array of possible values for the ProvisioningStateValues const type. +func PossibleProvisioningStateValuesValues() [11]ProvisioningStateValues { + return [11]ProvisioningStateValues{Accepted, Canceled, Created, Creating, Deleted, Deleting, Failed, OK, Succeeded, Updated, Updating} +} + // BaseProduct the product documentation. type BaseProduct struct { // ProductID - Unique identifier representing a specific product for a given latitude & longitude. For example, uberX in San Francisco will have a different product_id than uberX in Los Angeles. diff --git a/test/src/tests/generated/paging/models.go b/test/src/tests/generated/paging/models.go index ff8d72a22..3236203ba 100644 --- a/test/src/tests/generated/paging/models.go +++ b/test/src/tests/generated/paging/models.go @@ -40,6 +40,11 @@ const ( Updating Status = "Updating" ) +// PossibleStatusValues returns an array of possible values for the Status const type. +func PossibleStatusValues() [11]Status { + return [11]Status{Accepted, Canceled, Created, Creating, Deleted, Deleting, Failed, OK, Succeeded, Updated, Updating} +} + // OdataProductResult ... type OdataProductResult struct { autorest.Response `json:"-"` diff --git a/test/src/tests/generated/url/models.go b/test/src/tests/generated/url/models.go index d6d2cf9f7..074cc6dfe 100644 --- a/test/src/tests/generated/url/models.go +++ b/test/src/tests/generated/url/models.go @@ -18,6 +18,11 @@ const ( Redcolor URIColor = "red color" ) +// PossibleURIColorValues returns an array of possible values for the URIColor const type. +func PossibleURIColorValues() [3]URIColor { + return [3]URIColor{Bluecolor, Greencolor, Redcolor} +} + // Error ... type Error struct { Status *int32 `json:"status,omitempty"` diff --git a/test/src/tests/generated/validation/models.go b/test/src/tests/generated/validation/models.go index 1a55b353e..bd9cf6fe3 100644 --- a/test/src/tests/generated/validation/models.go +++ b/test/src/tests/generated/validation/models.go @@ -18,6 +18,11 @@ const ( ConstantStringAsEnum EnumConst = "constant_string_as_enum" ) +// PossibleEnumConstValues returns an array of possible values for the EnumConst const type. +func PossibleEnumConstValues() [1]EnumConst { + return [1]EnumConst{ConstantStringAsEnum} +} + // ChildProduct the product documentation. type ChildProduct struct { // ConstProperty - Constant string