Skip to content

Commit dd364f2

Browse files
committed
Add ordinal function to enums
1 parent cc34619 commit dd364f2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

templates/main/singleton/boil_types.go.tpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ It only titlecases the EnumValue portion if it's snake-cased.
138138
func (e {{$enumName}}) String() string {
139139
return string(e)
140140
}
141+
142+
func (e {{$enumName}}) Ordinal() int {
143+
switch e {
144+
{{range $idx, $val := $vals -}}
145+
{{- $enumValue := titleCase $val -}}
146+
case {{$enumName}}{{$enumValue}}:
147+
return {{$idx}}
148+
{{end}}
149+
default:
150+
panic(errors.New("enum is not valid"))
151+
}
152+
}
141153
{{- end -}}
142154

143155
{{ if and

0 commit comments

Comments
 (0)