diff --git a/main.go b/main.go index 2bf773d..5f35086 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,15 @@ func main() { panic("could not determine underlying type for enum") } + visited := make(map[string]bool, 0) + for i := range enumValues{ + if visited[enumValues[i].Value] == true{ + panic("no duplicate values allowed in enum") + } else { + visited[enumValues[i].Value] = true + } + } + templates, err := template.New(""). Funcs(TemplateFunctions). // Custom functions ParseFS(templates, "templates/*.tmpl") diff --git a/templates/enum.tmpl b/templates/enum.tmpl index 0680994..ace707a 100644 --- a/templates/enum.tmpl +++ b/templates/enum.tmpl @@ -47,8 +47,12 @@ func ({{ $lt }} {{ $t }}) String() string { switch {{ $lt }} { {{- range $index, $enum := $.EnumValues }} case {{ $enum.Name }}: + {{- if eq $.BaseType "string" }} + return {{ $enum.Value }} + {{- else }} return "{{ stringer $enum.Name }}" {{- end }} + {{- end }} default: {{- if $default := $.EnumDefaultValue }} return {{ $default }}.String()