Per the current implementation & documentation for stringer, we have a way to rename/remap constant names when we return them as string via -linecomment flag. But I can see cases, where comments are used for other explanatory purposes, and I'm not sure how I see you can combine a comment to both be used to rename constants with stringer, as well as preserve the other uses of the comment. I don't see how stringer currently handles that in the code:
This is what I propose with 2 options to enhance the constant naming feature:
enhance the line comment logic to support optional delimiting of comment text into sections, with the text before (first) delimiter treated as the value to rename constant name to, and text after that (first) delimiter treated as comments and ignored for the purposes of stringer. Perhaps the delimiter value can also be specified here, with default being either a comma or semicolon. If enabled this delimiting feature, comments can then be both for renaming and other uses. If not enabling this enhanced feature, then stringer's line comment feature behaves as it does currently.
offer option of another flag to specify alternative naming than comments. Could we not use something like struct tags to label constants with alternative name for stringer? And if enabled with this flag, stringer parses the struct tags to get the alternate name, otherwise ignores the tags.
If there is already such functionality that I overlooked, please enlighten me.
Proposal Details
Per the current implementation & documentation for stringer, we have a way to rename/remap constant names when we return them as string via
-linecomment
flag. But I can see cases, where comments are used for other explanatory purposes, and I'm not sure how I see you can combine a comment to both be used to rename constants with stringer, as well as preserve the other uses of the comment. I don't see how stringer currently handles that in the code:https://cs.opensource.google/go/x/tools/+/refs/tags/v0.17.0:cmd/stringer/stringer.go;l=465
This is what I propose with 2 options to enhance the constant naming feature:
enhance the line comment logic to support optional delimiting of comment text into sections, with the text before (first) delimiter treated as the value to rename constant name to, and text after that (first) delimiter treated as comments and ignored for the purposes of stringer. Perhaps the delimiter value can also be specified here, with default being either a comma or semicolon. If enabled this delimiting feature, comments can then be both for renaming and other uses. If not enabling this enhanced feature, then stringer's line comment feature behaves as it does currently.
offer option of another flag to specify alternative naming than comments. Could we not use something like struct tags to label constants with alternative name for stringer? And if enabled with this flag, stringer parses the struct tags to get the alternate name, otherwise ignores the tags.
If there is already such functionality that I overlooked, please enlighten me.