Skip to content

Commit

Permalink
Merge pull request #1305 from EmiPhil/add_ordinal_to_enum
Browse files Browse the repository at this point in the history
Add ordinal function to enums
  • Loading branch information
stephenafamo authored Sep 13, 2023
2 parents cc34619 + dd364f2 commit 59e63d5
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions templates/main/singleton/boil_types.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ It only titlecases the EnumValue portion if it's snake-cased.
func (e {{$enumName}}) String() string {
return string(e)
}

func (e {{$enumName}}) Ordinal() int {
switch e {
{{range $idx, $val := $vals -}}
{{- $enumValue := titleCase $val -}}
case {{$enumName}}{{$enumValue}}:
return {{$idx}}
{{end}}
default:
panic(errors.New("enum is not valid"))
}
}
{{- end -}}

{{ if and
Expand Down

0 comments on commit 59e63d5

Please sign in to comment.