cmd/stringer: add more control over generated String output through comment #255
+72
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The change suggested is to address the use case, where constants have to be public (capitalized), and the output is expected to be camelCase.
The idea is that the flags may be forgotten, and the output is expected to be the same over time.
Without the -linecomment flag, regular line comment will be searched for a sequence, mocking struct tag, at any part of the comment, in the following form (notice required `):
// `stringer:"[<name>],[fn]"`
where
<name>
is a name to generate and fn is one of the following: "title", "de(un)title", "lower", "upper".Absent
<name>
means to use default constant name and pass it through the function, if any. If<name>
is provided, it will still be passed through the function for consistency. To completely suppress any output, provide`stringer:""`
.golang/go#38229 mentioned someone's idea to add support for flags for styling the output of stringer, however it seems to be more logical to have it as a comment close to the code itself.
Downside: mocking struct tag is still just a comment and becomes a part of regular godoc.