Skip to content
Open
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
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,22 @@ var (
```

**Syntax Rules:**
* `Key:Value` sets a field (e.g., `Code:CC`).
* `Key:"Value with spaces"` sets a string field with spaces.
* `Key:true` sets a boolean field (e.g., `IsCredit:true`).
* `Key:"[]string{\"a\", \"b\"}"` sets complex types (slices, structs) by quoting the Go syntax.
* Fields not specified default to their zero value.

- `Key:Value` sets a field (e.g., `Code:CC`).
- `Key:"Value with spaces"` sets a string field with spaces.
- `Key:true` sets a boolean field (e.g., `IsCredit:true`).
- `Key:"[]string{\"a\", \"b\"}"` sets complex types (slices, structs) by quoting the Go syntax.
- Fields not specified default to their zero value.

### 2. Manual Mode

If you need complex initialization (e.g., function calls, external imports) or want to document individual instances, you can define the variables yourself.

**When to use Manual Mode:**
* You need to use functions like `time.Now()`.
* You need to import other packages.
* You want to add godoc comments to specific enum instances.

- You need to use functions like `time.Now()`.
- You need to import other packages.
- You want to add godoc comments to specific enum instances.

```go
package payment
Expand Down Expand Up @@ -143,6 +145,7 @@ The tool generates the following for your type:
- `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.
- `-dry-run`: (Optional) Perform a trial run without writing any files. The generated source code will be printed to stdout.

## Best Practices

Expand Down
21 changes: 19 additions & 2 deletions cmd/enumr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ func main() {
"",
"output file name or directory (default: dir/<type>_enum.go)",
)
marshalField := flag.String("marshal-field", "", "field to use for marshaling (String/MarshalText)")
marshalField := flag.String(
"marshal-field",
"",
"field to use for marshaling (String/MarshalText)",
)
zero := flag.Bool("zero", false, "allow zero value (empty string) during parsing")
dryRun := flag.Bool("dry-run", false, "perform a trial run with no changes made")

flag.Parse()

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
ctx := context.Background()

// Ensure the -type argument is provided
Expand Down Expand Up @@ -93,6 +98,18 @@ func main() {
// Determine output filename
outFileName := enumr.GetOutputFilename(pkg.Dir, targetTypes[0], outputName)

if *dryRun {
logger.LogAttrs(
ctx,
slog.LevelInfo,
"Dry run enabled, no files written",
slog.String("file", outFileName),
)
//nolint:forbidigo // print the result to the screen instead of using a log for dry runs
fmt.Print(string(source))
return
}

// Write the generated source to a file
if err = os.WriteFile(outFileName, source, 0o644); err != nil {
logger.ErrorContext(ctx, "Error writing file", "file", outFileName, "error", err)
Expand Down