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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ The tool generates the following for your type:
- `SNAKE_CASE`
- `Title Case`
- `-output`: (Optional) Output file name or directory. Defaults to `<type>_enum.go` in the package directory.
- `-zero`: (Optional) Allow the zero value (empty string) to be parsed as a valid enum value. Useful for optional fields that default to the zero value.

## Best Practices

Expand Down
3 changes: 2 additions & 1 deletion cmd/enumr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func main() {
"output file name or directory (default: dir/<type>_enum.go)",
)
marshalField := flag.String("marshal-field", "", "field to use for marshaling (String/MarshalText)")
zero := flag.Bool("zero", false, "allow zero value (empty string) during parsing")

flag.Parse()

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

// Process the loaded package and files
generator := enumr.NewGenerator(logger)
source, err := generator.Generate(ctx, pkg, targetTypes, nameFormat, *marshalField)
source, err := generator.Generate(ctx, pkg, targetTypes, nameFormat, *marshalField, *zero)
if err != nil {
logger.ErrorContext(ctx, "Error processing file", "error", err)
os.Exit(1)
Expand Down
4 changes: 4 additions & 0 deletions pkg/enumr/enum.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func Parse{{.TypeName}}(text string) ({{.TypeName}}, error) {
{{printf "\n\t"}}case {{ if $marshalField }}{{ index .Fields $marshalField }}{{ else }}"{{transformName .Name $format}}"{{ end }}:
return {{.Name}}, nil
{{- end}}
{{- if .IncludeZero }}
case "":
return {{.TypeName}}{}, nil
{{- end }}
default:
return {{.TypeName}}{}, fmt.Errorf("unknown enum value: %q", text)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/enumr/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (g *Generator) Generate(
typeNames []string,
nameFormat string,
marshalField string,
includeZero bool,
) ([]byte, error) {
var enums []enumInfo

Expand Down Expand Up @@ -63,6 +64,7 @@ func (g *Generator) Generate(
Instances: resolution.Instances,
CaseFormat: nameFormat,
GenerateVars: resolution.GenerateVars,
IncludeZero: includeZero,
MarshalField: marshalField,
StructFields: typeSpec.Fields,
})
Expand Down
26 changes: 26 additions & 0 deletions pkg/enumr/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,29 @@ func TestGenerateEnumSourceWithVars(t *testing.T) {
t.Errorf("generated source does not contain expected var block.\nGot:\n%s", source)
}
}

func TestGenerateEnumSourceWithZero(t *testing.T) {
packageName := "testpkg"
enums := []enumInfo{
{
TypeName: "MyEnum",
CaseFormat: "snake_case",
IncludeZero: true,
Instances: []instanceData{
{Name: "ValueOne"},
},
},
}

source, err := generateEnumSource(packageName, enums)
if err != nil {
t.Fatalf("generateEnumSource failed: %v", err)
}

expectedSnippet := `case "":
return MyEnum{}, nil`

if !strings.Contains(string(source), expectedSnippet) {
t.Errorf("generated source does not contain zero value case.\nGot:\n%s", source)
}
}
1 change: 1 addition & 0 deletions pkg/enumr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type enumInfo struct {
Instances []instanceData
CaseFormat string
GenerateVars bool
IncludeZero bool
MarshalField string
StructFields []fieldInfo
}
Expand Down