In the readme/documentation add the variables avilable inside the .tmpl file.
|
data := map[string]any{ |
|
"enum": enum, |
|
"name": name, |
|
"lowercase": g.LowercaseLookup, |
|
"nocase": g.CaseInsensitive, |
|
"nocomments": g.NoComments, |
|
"noIota": g.NoIota, |
|
"marshal": g.Marshal, |
|
"sql": g.SQL, |
|
"sqlint": g.SQLInt, |
|
"flag": g.Flag, |
|
"names": g.Names, |
|
"ptr": g.Ptr, |
|
"values": g.Values, |
|
"anySQLEnabled": g.anySQLEnabled(), |
|
"sqlnullint": g.SQLNullInt, |
|
"sqlnullstr": g.SQLNullStr, |
|
"mustparse": g.MustParse, |
|
"forcelower": g.ForceLower, |
|
"forceupper": g.ForceUpper, |
|
"noparse": g.NoParse, |
|
// Computed values for cleaner templates |
|
"generateParse": generateParse, |
|
"parseIsPublic": parseIsPublic, |
|
"parseName": parseName, |
|
"generateError": generateError, |
|
} |
|
type Enum struct { |
|
Name string |
|
Prefix string |
|
Type string |
|
Values []EnumValue |
|
Comment string |
|
} |
Would also be nice to include a small example that uses variables, if/else, importing an external library... In my case, I had to do this, maybe it's a good real-world example:
/enums/templates/huma.tmpl
// Schema returns the Huma schema for {{.enum.Name}}.
func (s {{.enum.Name}}) Schema(_ huma.Registry) *huma.Schema {
values := {{.enum.Name}}Values()
enumInterfaces := make([]any, len(values))
for i, v := range values {
enumInterfaces[i] = string(v)
}
return &huma.Schema{
Type: huma.TypeString,
Enum: enumInterfaces,{{- if .enum.Comment }}
Description: {{ printf "%q" .enum.Comment }},{{- end }}
Examples: []any{string(values[0])},
}
}
/enums/my_status.go
package enums
//go:generate go tool go-enum --marshal --template templates/*.tmpl --values
// MyStatus represents the status of the fetch request
// ENUM(pending, completed, failed)
type MyStatus string
In the readme/documentation add the variables avilable inside the .tmpl file.
go-enum/generator/generator.go
Lines 209 to 235 in 9d73c76
go-enum/generator/generator.go
Lines 42 to 48 in 9d73c76
Would also be nice to include a small example that uses variables, if/else, importing an external library... In my case, I had to do this, maybe it's a good real-world example:
/enums/templates/huma.tmpl// Schema returns the Huma schema for {{.enum.Name}}. func (s {{.enum.Name}}) Schema(_ huma.Registry) *huma.Schema { values := {{.enum.Name}}Values() enumInterfaces := make([]any, len(values)) for i, v := range values { enumInterfaces[i] = string(v) } return &huma.Schema{ Type: huma.TypeString, Enum: enumInterfaces,{{- if .enum.Comment }} Description: {{ printf "%q" .enum.Comment }},{{- end }} Examples: []any{string(values[0])}, } }/enums/my_status.go