Skip to content

[FEAT] AST and Types Parsing of Enums#6

Merged
bbengfort merged 1 commit intomainfrom
gengengen
Apr 7, 2026
Merged

[FEAT] AST and Types Parsing of Enums#6
bbengfort merged 1 commit intomainfrom
gengengen

Conversation

@bbengfort
Copy link
Copy Markdown
Contributor

@bbengfort bbengfort commented Apr 7, 2026

Scope of changes

Uses AST to discover Enum types, their constants, and name values.

Estimated PR Size:

  • Tiny
  • Small
  • Medium
  • Large
  • Huge

Acceptance criteria

This PR will be merged without review.

Author checklist

  • I have manually tested the change and/or added automation in the form of unit tests or integration tests
  • I have updated the dependencies list
  • I have added new test fixtures as needed to support added tests
  • I have added or updated the documentation
  • I have run go generate to update generated code

Note

Medium Risk
Medium risk because it changes core code-generation discovery logic (package loading, type/const detection, names-variable resolution), which can affect what gets generated and where. Failures will surface at build/generate time but could be subtle across different package/file layouts.

Overview
enumify.Generate/GenerateTests now create *_gen.go and *_gen_test.go via github.com/dave/jennifer/jen, and introduce file naming helpers based on the triggering $GOFILE.

The generator adds a go/packages/go/types discovery phase (Options.discover) that finds enum types by underlying uint8, collects their typed const values, auto-detects [Type]Names variables (1D/2D string slices/arrays), supports an explicit -names override, and validates required invariants (names var present, zero const present, etc.).

Examples are adjusted/expanded (new calendar.go with array-backed names and an additional Month enum; status.go adds a non-enum type to ensure it’s ignored), and generated example *_gen_test.go files are now gitignored; go.mod/go.sum add jennifer and golang.org/x/tools dependencies.

Reviewed by Cursor Bugbot for commit fba82e8. Configure here.

Comment on lines +63 to +68
// TODO: use the packages.NeedsSyntax mode to get the ast and find the
// CompositeLit that represents the names variable. You can then use its Elts
// property to get the number of elements and compare that to the number of
// constants. Unfortunately, there is no mapping from a types.Object to an
// ast.Node to do checking if it is an ast.CompositeLit so ast traversal is
// required, which is a bit too far for me to implement right now.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chris-okuda if you ever have the urge to do AST traversal …

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bbengfort I've been reading about code gen with Go, so I'm ready for whatever task you have for me!

@bbengfort bbengfort merged commit 7daa6ab into main Apr 7, 2026
6 checks passed
@bbengfort bbengfort deleted the gengengen branch April 7, 2026 01:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants