Skip to content

proposal: x/tools/cmd/stringer: add -fromstring method #72959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
quasilyte opened this issue Mar 19, 2025 · 2 comments
Closed

proposal: x/tools/cmd/stringer: add -fromstring method #72959

quasilyte opened this issue Mar 19, 2025 · 2 comments
Labels
Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program.
Milestone

Comments

@quasilyte
Copy link
Contributor

quasilyte commented Mar 19, 2025

Proposal Details

stringer solves int->string case, making it easier to define enum-like values in Go.
It does not, however, generate a reversal index which is necessary for some (but not all) enums.
A string->int conversion method could be generated when a special flag -fromstring is provided.

With FromString(s string) T method, it would be possible to serialize enums to files and back without having to write that code yourself. The whole idea is to avoid the need to update two entities, int->string and string->int mappings. There are many ways to implement that mapping manually, but it also leads to a decision problem and inconsistency where a single large project may have different string->int conversion tactics (a map, a switch statement, a linear search along the constants, etc).

This would make my life easier, given that I already use stringer everywhere I can. I'm interested to know if anyone else is interested in this feature as well.

If this is not rejected right away, we can figure out the details. E.g. how FromString would behave with an invalid input (I would assume that it would not return an error, perhaps it would set the value to 0 as it's usually a convention for unknown/invalid/zero values in enum-like declarations)

@gopherbot gopherbot added this to the Proposal milestone Mar 19, 2025
@seankhliao
Copy link
Member

see all the previous issues linked above.

@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Mar 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal ToolProposal Issues describing a requested change to a Go tool or command-line program.
Projects
None yet
Development

No branches or pull requests

4 participants