Skip to content

feat: namespaces - #1

Merged
jcmfernandes merged 9 commits into
mainfrom
namespace-generators
Dec 19, 2025
Merged

feat: namespaces#1
jcmfernandes merged 9 commits into
mainfrom
namespace-generators

Conversation

@jcmfernandes

@jcmfernandes jcmfernandes commented Dec 19, 2025

Copy link
Copy Markdown
Member

What does this PR do?

Replaces the prefix/suffix-based option generation with namespace-based generation. Instead of generating top-level functions with prefixes (e.g., WithMyOption()), it generates methods on a namespace struct (e.g., OptionNs.MyOption()).

Key Changes:

  • Removed -prefix and -suffix command-line flags
  • Added -namespace flag (defaults to ${option}Namespace)
  • Changed generated API from top-level functions to namespace methods
  • Updated template to generate namespace struct and methods
  • Fixed implementation type naming to avoid collisions across different option types in the same package
  • Added ARGV to generated files showing the command used for generation

Example:

Before:

//go:generate go-options -prefix=With -option=Option config
cfg := newConfig(WithMyInt(123))

After:

//go:generate go-options -namespace=OptionNs -option=Option config
cfg := newConfig(OptionNs.MyInt(123))

Motivation

The prefix/suffix approach polluted the global namespace with many top-level functions. The namespace approach:

  1. Better organization: Groups all related options under a single namespace
  2. Cleaner global namespace: Only one namespace variable instead of dozens of functions
  3. Modern API design: Similar to how other Go libraries organize their APIs
  4. Better discoverability: IDE autocomplete shows all options when typing OptionNs.

Additional notes

Breaking Change

This is a breaking change that removes the -prefix and -suffix flags entirely. Users must:

  1. Update their go:generate directives to use -namespace instead of -prefix/-suffix
  2. Update their code to use the namespace-based API (OptionNs.MyField() instead of OptionMyField())

Implementation Details

  • Template changes (render.gotmpl): Added namespace struct generation and changed functions to methods
  • Implementation type naming: Fixed to include option type name prefix to avoid collisions (e.g., optionMyIntImpl vs myOptMyIntImpl)
  • Generated file headers: Now include the command-line arguments used for generation

How to test the change?

make test

@jcmfernandes jcmfernandes self-assigned this Dec 19, 2025
@jcmfernandes
jcmfernandes marked this pull request as ready for review December 19, 2025 02:11
@jcmfernandes
jcmfernandes merged commit 67830b4 into main Dec 19, 2025
1 check passed
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.

1 participant