Skip to content

Commit

Permalink
Add ordinal function to enums
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiPhil committed Sep 12, 2023
1 parent cc34619 commit dd364f2
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 dd364f2

Please sign in to comment.