Skip to content

proposal: x/tools/stringer: add a Parse function to go from string to value #55860

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
fredrikelinder opened this issue Sep 25, 2022 · 7 comments

Comments

@fredrikelinder
Copy link

Extend stringer with the capability to go from string to value.

Inspired by https://pkg.go.dev/github.com/diegommm/tools/cmd/stringerParser

Usage:

//go:generate stringer -type Day -trimprefix Day
type Day uint8

const (
  DayMonday Day = iota
  DayTuesday
  DayWednesday
  DayThursday
  DayFriday
  DaySaturday
  DaySunday
)

func ParseDay(s string) (Day, error) {
  var d Day
  err := d.Parse(s)
  if err != nil {
    return d, err
  }
  return d, nil
}

Besides being useful when accepting input from a user this feature would also be useful when implementing default value struct tags for "enum" types.

@gopherbot gopherbot added this to the Proposal milestone Sep 25, 2022
@fredrikelinder
Copy link
Author

@robpike
Copy link
Contributor

robpike commented Sep 25, 2022

I'm pretty sure this has come up before, but I can't find the discussion. Anyway, there's already an external package to do this, as you cite, so there isn't a compelling need to compete with it in the tools repo. It even has the same interface. You can just use the external package.

Moreover, the full set of options and controls needed to solve this problem in general, the way people would want it with issues of casing and suffixes and prefixes and plurals etc., argues it should stay external.

Marking the CL as hold for proposal acceptance.

@mvdan
Copy link
Member

mvdan commented Sep 25, 2022

Previously #23535. If you need to convert to and from string, I would argue you want a separate tool that generates MarshalText and UnmarshalText, since they are both standard interfaces. Then exposing a String method manually would be trivial.

@ianlancetaylor ianlancetaylor changed the title proposal: affected/package: tools/stringer proposal: x/tools/stringer: add a Parse function to go from string to value Sep 25, 2022
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Sep 25, 2022
@fredrikelinder
Copy link
Author

The main reason I thought of stringer for this implementation is that stringer already implements the "enum"-type reading capability needed for this functionality.

MarshalText and UnmarshalText are good suggestions, as they more closely match what I'm looking for, so thank you.

The current implementation of stringer keeps only one of the same-valued constant names, as stringer has to make a decision of which constant name to string an underlying value as. This information is needed by a Parse or MarshalText implementation to go from any of its constant names to the a constant.

When reading the stringer implementation it appears that care has been taken to reduce the memory footprint needed for the String implementation (or else the map variant would always be used), and adding additional memory (the currently discarded strings) would be counter to that effort.

Alright, a separate tool it is. Just need to copy the type/const-reading code from stringer.

@mvdan
Copy link
Member

mvdan commented Sep 25, 2022

Indeed, the code that stringer produces is only optimized in one direction and avoids overhead. A tool that also wants to do UnmarshalText efficiently might want something like a map[string]int too, for example.

@mcandre
Copy link

mcandre commented May 10, 2023

Please link to such a generator tool here in this conversation thread.

@mcandre
Copy link

mcandre commented May 10, 2023

By the way, generating a Validate() error method to reject values not covered by the enum declaration, would also be useful for making Go programs safer.

@golang golang locked and limited conversation to collaborators May 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants