From dd364f267c29fab421dd5708409dd9cfebd181c7 Mon Sep 17 00:00:00 2001 From: Phil Date: Tue, 12 Sep 2023 16:19:18 -0700 Subject: [PATCH] Add ordinal function to enums --- templates/main/singleton/boil_types.go.tpl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/templates/main/singleton/boil_types.go.tpl b/templates/main/singleton/boil_types.go.tpl index ba27ac012..495b806fa 100644 --- a/templates/main/singleton/boil_types.go.tpl +++ b/templates/main/singleton/boil_types.go.tpl @@ -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