Skip to content

Commit

Permalink
Generate func returning all values for an enum type. (#102)
Browse files Browse the repository at this point in the history
For each enum const type, generate a function that returns an array of
all the possible values for that type.
  • Loading branch information
jhendrixMSFT authored Mar 6, 2018
1 parent 3e7cc3f commit ac48644
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Templates/EnumTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ type @Model.Name string
@EmptyLine
@if (Model.Values.Any())
{
var possibleFuncName = $"Possible{Model.Name}Values";
var orderedValues = new List<string>();
<text>
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))
{
Expand All @@ -53,9 +57,13 @@ type @Model.Name string
</text>
}
<text>
@(CodeNamerGo.Instance.GetEnumMemberName(v.Name)) @(Model.Name) = "@(v.SerializedName)"
@(memName) @(Model.Name) = "@(v.SerializedName)"
</text>
}
)
// @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))}
}
</text>
}
10 changes: 10 additions & 0 deletions test/src/tests/generated/body-complex/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/src/tests/generated/body-string/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/src/tests/generated/header/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/src/tests/generated/lro/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/src/tests/generated/model-flattening/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/src/tests/generated/paging/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/src/tests/generated/url/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/src/tests/generated/validation/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac48644

Please sign in to comment.