Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var (

The tool generates the following for your type:

- `func (t Type) String() string`: Returns the enum name (e.g., "credit_card") or the value of the field specified by `-name-field`.
- `func (t Type) String() string`: Returns the enum name (e.g., "credit_card") or the value of the field specified by `-marshal-field`.
- `func (t Type) MarshalText() ([]byte, error)`: Implements `encoding.TextMarshaler`.
- `func (t *Type) UnmarshalText([]byte) error`: Implements `encoding.TextUnmarshaler`. Matches the string representation exactly.
- `func TypeValues() []Type`: Returns a slice of all enum instances.
Expand All @@ -133,8 +133,8 @@ The tool generates the following for your type:
## CLI Options

- `-type`: (Required) Comma-separated list of type names to generate code for.
- `-name-field`: (Optional) The name of the struct field to use for the string representation (e.g., `Code`). If not provided, the enum instance name is used (transformed by `-format`).
- `-format`: (Optional) Casing format for the string representation (ignored if `-name-field` is used).
- `-marshal-field`: (Optional) The name of the struct field to use for marshaling (e.g., `Code`). If not provided, the enum instance name is used (transformed by `-format`).
- `-format`: (Optional) Casing format for the string representation (ignored if `-marshal-field` is used).
- (empty) (default): Preserves the original casing.
- `snake_case`
- `camelCase`
Expand Down
4 changes: 2 additions & 2 deletions cmd/enumr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
"",
"output file name or directory (default: dir/<type>_enum.go)",
)
nameField := flag.String("name-field", "", "field to use for string representation")
marshalField := flag.String("marshal-field", "", "field to use for marshaling (String/MarshalText)")

flag.Parse()

Expand Down Expand Up @@ -78,7 +78,7 @@ func main() {

// Process the loaded package and files
generator := enumr.NewGenerator(logger)
err = generator.Generate(ctx, pkg, targetTypes, nameFormat, outputName, *nameField)
err = generator.Generate(ctx, pkg, targetTypes, nameFormat, outputName, *marshalField)
if err != nil {
logger.ErrorContext(ctx, "Error processing file", "error", err)
os.Exit(1)
Expand Down
10 changes: 5 additions & 5 deletions pkg/enumr/enum.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)
{{range .Enums}}
{{- $typeName := .TypeName}}
{{- $nameField := .NameField}}
{{- $marshalField := .MarshalField}}
{{- $structFields := .StructFields}}
{{- if .GenerateVars}}
var ( {{- range .Instances -}}
Expand All @@ -16,12 +16,12 @@ var ( {{- range .Instances -}}
)
{{end}}
{{- $format := .CaseFormat}}
// String converts the enum value to its corresponding name field.
// String converts the enum value to its corresponding marshal field.
func (t {{.TypeName}}) String() string {
switch t { {{- range .Instances -}}
{{printf "\n\t"}}case {{.Name}}:
{{- if $nameField }}
return {{ index .Fields $nameField }}
{{- if $marshalField }}
return {{ index .Fields $marshalField }}
{{- else }}
return "{{transformName .Name $format}}"
{{- end }}
Expand All @@ -39,7 +39,7 @@ func (t {{.TypeName}}) MarshalText() (text []byte, err error) {
func (t *{{.TypeName}}) UnmarshalText(text []byte) error {
value := string(text)
switch value { {{- range .Instances -}}
{{printf "\n\t"}}case {{ if $nameField }}{{ index .Fields $nameField }}{{ else }}"{{transformName .Name $format}}"{{ end }}:
{{printf "\n\t"}}case {{ if $marshalField }}{{ index .Fields $marshalField }}{{ else }}"{{transformName .Name $format}}"{{ end }}:
*t = {{.Name}}
{{- end}}
default:
Expand Down
12 changes: 6 additions & 6 deletions pkg/enumr/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (g *Generator) Generate(
typeNames []string,
nameFormat string,
output string,
nameField string,
marshalField string,
) error {
var enums []enumInfo

Expand All @@ -45,14 +45,14 @@ func (g *Generator) Generate(
return err
}

// Validate that if nameField is specified, all instances have it
if nameField != "" {
// Validate that if marshalField is specified, all instances have it
if marshalField != "" {
for _, instance := range resolution.Instances {
if _, ok := instance.Fields[nameField]; !ok {
if _, ok := instance.Fields[marshalField]; !ok {
return fmt.Errorf(
"instance %s does not have field %q",
instance.Name,
nameField,
marshalField,
)
}
}
Expand All @@ -63,7 +63,7 @@ func (g *Generator) Generate(
Instances: resolution.Instances,
CaseFormat: nameFormat,
GenerateVars: resolution.GenerateVars,
NameField: nameField,
MarshalField: marshalField,
StructFields: typeSpec.Fields,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/enumr/testdata/myenum_enum.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
)

// String converts the enum value to its corresponding name field.
// String converts the enum value to its corresponding marshal field.
func (t MyEnum) String() string {
switch t {
case ValueOne:
Expand Down
2 changes: 1 addition & 1 deletion pkg/enumr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type enumInfo struct {
Instances []instanceData
CaseFormat string
GenerateVars bool
NameField string
MarshalField string
StructFields []fieldInfo
}

Expand Down