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
x/tools/cmd/stringer: add a -trimprefix flag #16539
Comments
This seems reasonable to me, though I don't think you can assume a value of -trimprefix without breaking existing code. I think it has to be opt-in. |
ok, I'll keep that in mind! |
I like this proposal. I think -trimprefix should default to the longest
common prefix of all the names (instead of the type name).
|
That would change the behavior of stringer, as @quentinmit pointed out. Maybe a value that's not a valid identifier could trigger it without requiring spelling the prefix out? Something like Unrelated, but I assume that if you did
The strings would be
Is that correct? |
@minux that was actually my first idea but I thought using the type name by default was simpler, I'm not opposed to going back to that idea though if thats the consensus. @jimmyfrasche if we implemented @minux's idea of trimming the longest common prefix when you pass just a |
@adrianduke Assuming -trimprefix is a stdlib flag package flag.String
You could have an -autotrimprefix and a -trimprefix but that seems messy. To me, having a magic name seems the best way to go, but it can't be something that either
Of those left, |
@jimmyfrasche ah I understand now! There appears to be many valid special characters when placed as flag values (at least for BASH), heres a short list of some that make some amount of sense to me:
Although having 2 separate flags doesn't seem that horrendous of an idea, having 1 flag with a special meaning character will most certainly be harder to get across to the user rather than your example of 2 flags:
flags are cheap and explicit, special characters are exceptions and harder to communicate. |
This would be useful for #15462, and I see the issue has been accepted for a while with little activity, so I have given it a go. I have left out the automatic detection of prefixes for now. That can always be added later, with whatever extra flag or special trim value that we agree on. |
@gopherbot is being slow: https://go-review.googlesource.com/c/tools/+/76650 |
great job! This completely fell off my radar. |
This has been merged - not sure when gopherbot will catch up. |
I have moved the discussion about automatic detection of prefixes to #22649. |
Whoops, I forgot to use golang/go#N when closing this issue. Not gopherbot's fault at all. |
@mvdan Are you using the latest version of |
Context
When writing constants its common to add a prefix to aid IDE auto-complete and also to explicitly group together common constants (see: net/http, net/http, crypto/tls, net/http/fcgi & net/interface).
Problem
x/tools/cmd/stringer
generates aString()
method with only the exact constant name:Would result in:
Whilst this is desirable for certain situations there are some in which its not:
"type": "TypePayPal"
vs"type": "PayPal"
Types
all prefixed with the same 4 characters is just a waste of space given it would be in context in the column:Proposal
Add an additional flag
-trimprefix=<prefix>
tox/tools/cmd/stringer
, this will take a user defined prefix and strip it from the generated string, if no argument is passed it assumes the value of-type
, heres an example with out a value for-trimprefix
:Would generate:
And a contrived example with a value
-trimprefix=Ty
:Would generate:
I'd be happy to create a PR with the following feature if its accepted.
The text was updated successfully, but these errors were encountered: