tools icon indicating copy to clipboard operation
tools copied to clipboard

cmd/stringer: add more control over generated String output through comment

Open zzwx opened this issue 3 years ago • 3 comments

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:""`.

https://github.com/golang/go/issues/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.

zzwx avatar Oct 23 '20 23:10 zzwx