-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Comments
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. |
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. |
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. |
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 |
Please link to such a generator tool here in this conversation thread. |
By the way, generating a |
Extend
stringer
with the capability to go from string to value.Inspired by https://pkg.go.dev/github.com/diegommm/tools/cmd/stringerParser
Usage:
Besides being useful when accepting input from a user this feature would also be useful when implementing default value struct tags for "enum" types.
The text was updated successfully, but these errors were encountered: