Skip to content

Commit 59e63d5

Browse files
authored
Merge pull request #1305 from EmiPhil/add_ordinal_to_enum
Add ordinal function to enums
2 parents cc34619 + dd364f2 commit 59e63d5

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

templates/main/singleton/boil_types.go.tpl

+12
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)