Skip to content

feat: add --initialism flag with token-aware const identifier casing#309

Open
leodido wants to merge 5 commits intoabice:masterfrom
leodido:feat/initialisms
Open

feat: add --initialism flag with token-aware const identifier casing#309
leodido wants to merge 5 commits intoabice:masterfrom
leodido:feat/initialisms

Conversation

@leodido
Copy link

@leodido leodido commented Feb 21, 2026

This PR adds support for preserving configured initialisms in generated const identifiers.

Example:

  • Without flag: http_url -> EndpointHttpUrl
  • With --initialism HTTP,URL: http_url -> EndpointHTTPURL

The implementation is token-aware to avoid partial-word rewrites (for example, apiary is not rewritten to APIary).

Motivation

go-enum currently generates idiomatic CamelCase, but projects with common initialisms (HTTP, URL, ID, API, JSON, etc.) need stable Go-style identifier casing without changing enum string values.

This also aligns with Go naming conventions, where initialisms are typically kept in a consistent form in identifiers (for example, HTTP, URL, ID).

What Changed

  1. CLI/API naming
  • Add new repeatable flag: --initialism (comma-separated values supported).
  • Add parser: ParseInitialisms([]string) with validation and deduplication.
  • Add config field: GeneratorConfig.Initialisms.
  • Add option: WithInitialisms(...).
  1. Naming pipeline
  • Apply initialism rewriting during const identifier generation.
  • Rewriting is token-aware (not substring/global replace).
  • Prevents false positives such as apiary -> APIary and ideology -> IDEology.
  1. Tests
  • Added/updated parser tests, integration tests, ordering tests, and regression tests for substring false positives.
  • Added focused tokenizer tests for boundary rules used by shouldSplitToken/splitIdentifierTokens.
  1. Docs
  • Added --initialism to command options.
  • Added a dedicated "Initialism notes" section documenting behavior and interactions.

Behavior and Interactions

  • --initialism affects generated const identifiers only.
  • It does not modify enum string values.
  • --forcelower and --forceupper remain value-focused and independent.
  • Rewriting runs after alias sanitization and after snake_case -> CamelCase conversion.
  • With --nocamel, underscore-separated initialisms may not be rewritten because CamelCase conversion is skipped.
  • Rewriting currently applies to the full identifier (including prefix/type-derived segments), not just the value-derived segment.

Example of full-identifier behavior:

// ENUM(created)
type UserId int

With --initialism ID, generated const is UserIDCreated.

Validation

Commands run:

go test ./...
go test ./generator -run 'TestShouldSplitToken|TestSplitIdentifierTokens|TestInitialism|TestInitialisms' -v

Coverage snapshots on this branch:

  • Total statements: 92.2%
  • generator package: 95.7%
  • ParseInitialisms: 100%
  • splitIdentifierTokens: 100%
  • shouldSplitToken: 100%

Add Acronyms field to GeneratorConfig, WithAcronyms option function,
ParseAcronyms validator, and applyAcronyms method that replaces
title-cased acronym substrings with their fully uppercased forms
in generated const names.

Replacement pairs are precomputed at init time in NewGeneratorWithConfig
and sorted longest-first to handle overlapping acronyms correctly
(e.g., IDE is matched before ID).
Wire the repeatable --acronym StringSlice flag into the CLI, parse
and validate entries via ParseAcronyms, and pass them through to
GeneratorConfig.
Add tests for ParseAcronyms validation, WithAcronyms option, and
integration tests covering int/string enums, kfeatures-style usage,
nocamel interaction, noprefix, and overlapping acronym ordering.
Update TestNewGeneratorWithConfig and TestAllOptionsIntegration.
@leodido leodido requested a review from abice as a code owner February 21, 2026 12:03
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