Skip to content

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

Closed as duplicate of#13744
@quasilyte

Description

@quasilyte

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ProposalToolProposalIssues describing a requested change to a Go tool or command-line program.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      proposal: x/tools/cmd/stringer: add -fromstring method · Issue #72959 · golang/go