feat: add --initialism flag with token-aware const identifier casing#309
Open
leodido wants to merge 5 commits intoabice:masterfrom
Open
feat: add --initialism flag with token-aware const identifier casing#309leodido wants to merge 5 commits intoabice:masterfrom
--initialism flag with token-aware const identifier casing#309leodido wants to merge 5 commits intoabice:masterfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds support for preserving configured initialisms in generated const identifiers.
Example:
http_url->EndpointHttpUrl--initialism HTTP,URL:http_url->EndpointHTTPURLThe implementation is token-aware to avoid partial-word rewrites (for example,
apiaryis not rewritten toAPIary).Motivation
go-enumcurrently 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
--initialism(comma-separated values supported).ParseInitialisms([]string)with validation and deduplication.GeneratorConfig.Initialisms.WithInitialisms(...).apiary -> APIaryandideology -> IDEology.shouldSplitToken/splitIdentifierTokens.--initialismto command options.Behavior and Interactions
--initialismaffects generated const identifiers only.--forcelowerand--forceupperremain value-focused and independent.--nocamel, underscore-separated initialisms may not be rewritten because CamelCase conversion is skipped.Example of full-identifier behavior:
With
--initialism ID, generated const isUserIDCreated.Validation
Commands run:
Coverage snapshots on this branch:
92.2%generatorpackage:95.7%ParseInitialisms:100%splitIdentifierTokens:100%shouldSplitToken:100%