diff --git a/README.md b/README.md index a0170bf..12da0b9 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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` diff --git a/cmd/enumr/main.go b/cmd/enumr/main.go index f67c0b1..1a155e1 100644 --- a/cmd/enumr/main.go +++ b/cmd/enumr/main.go @@ -26,7 +26,7 @@ func main() { "", "output file name or directory (default: dir/_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() @@ -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) diff --git a/pkg/enumr/enum.tmpl b/pkg/enumr/enum.tmpl index 68fd94d..dbe05ac 100644 --- a/pkg/enumr/enum.tmpl +++ b/pkg/enumr/enum.tmpl @@ -7,7 +7,7 @@ import ( ) {{range .Enums}} {{- $typeName := .TypeName}} -{{- $nameField := .NameField}} +{{- $marshalField := .MarshalField}} {{- $structFields := .StructFields}} {{- if .GenerateVars}} var ( {{- range .Instances -}} @@ -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 }} @@ -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: diff --git a/pkg/enumr/generate.go b/pkg/enumr/generate.go index a0ab724..dedf730 100644 --- a/pkg/enumr/generate.go +++ b/pkg/enumr/generate.go @@ -28,7 +28,7 @@ func (g *Generator) Generate( typeNames []string, nameFormat string, output string, - nameField string, + marshalField string, ) error { var enums []enumInfo @@ -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, ) } } @@ -63,7 +63,7 @@ func (g *Generator) Generate( Instances: resolution.Instances, CaseFormat: nameFormat, GenerateVars: resolution.GenerateVars, - NameField: nameField, + MarshalField: marshalField, StructFields: typeSpec.Fields, }) } diff --git a/pkg/enumr/testdata/myenum_enum.go.golden b/pkg/enumr/testdata/myenum_enum.go.golden index 7221a0f..99e11f4 100644 --- a/pkg/enumr/testdata/myenum_enum.go.golden +++ b/pkg/enumr/testdata/myenum_enum.go.golden @@ -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: diff --git a/pkg/enumr/types.go b/pkg/enumr/types.go index 5fb7485..19f00fa 100644 --- a/pkg/enumr/types.go +++ b/pkg/enumr/types.go @@ -16,7 +16,7 @@ type enumInfo struct { Instances []instanceData CaseFormat string GenerateVars bool - NameField string + MarshalField string StructFields []fieldInfo }