diff --git a/example/user_template.tmpl b/example/user_template.tmpl index 7aab62b..b34e9f5 100644 --- a/example/user_template.tmpl +++ b/example/user_template.tmpl @@ -2,3 +2,6 @@ func Parse{{.enum.Name}}Example() bool { return true } +func Parse{{.enum.Name}}Description() string { + return `{{.enum.Comment}}` +} diff --git a/example/user_template_enum.go b/example/user_template_enum.go index 2a7479e..6c4ee42 100644 --- a/example/user_template_enum.go +++ b/example/user_template_enum.go @@ -73,3 +73,6 @@ func ParseOceanColorGlobbedExample2() bool { func ParseOceanColorExample() bool { return true } +func ParseOceanColorDescription() string { + return `OceanColor is an enumeration of ocean colors that are allowed.` +} diff --git a/example/user_template_test.go b/example/user_template_test.go index 127ec07..14d363e 100644 --- a/example/user_template_test.go +++ b/example/user_template_test.go @@ -11,6 +11,7 @@ import ( func TestUserTemplateColor(t *testing.T) { assert.Equal(t, OceanColor(0), OceanColorCerulean) + assert.Equal(t, "OceanColor is an enumeration of ocean colors that are allowed.", ParseOceanColorDescription()) assert.Equal(t, true, ParseOceanColorExample()) assert.Equal(t, true, ParseOceanColorGlobbedExample()) assert.Equal(t, true, ParseOceanColorGlobbedExample2()) diff --git a/generator/.snapshots/Test118ExampleFileMoreOptions-1.18 b/generator/.snapshots/Test118ExampleFileMoreOptions-1.18 index 97b1200..91e2661 100644 --- a/generator/.snapshots/Test118ExampleFileMoreOptions-1.18 +++ b/generator/.snapshots/Test118ExampleFileMoreOptions-1.18 @@ -1,4 +1,4 @@ -([]string) (len=186) { +([]string) (len=189) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -184,5 +184,8 @@ (string) (len=36) "func ParseChangeTypeExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=42) "func ParseChangeTypeDescription() string {", + (string) (len=50) "\treturn `ChangeType is a type of change detected.`", + (string) (len=1) "}", (string) "" } diff --git a/generator/.snapshots/Test118ExampleFileMoreOptions-og b/generator/.snapshots/Test118ExampleFileMoreOptions-og index 8dfd956..598607d 100644 --- a/generator/.snapshots/Test118ExampleFileMoreOptions-og +++ b/generator/.snapshots/Test118ExampleFileMoreOptions-og @@ -1,4 +1,4 @@ -([]string) (len=2616) { +([]string) (len=2658) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -184,6 +184,9 @@ (string) (len=32) "func ParseAnimalExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=38) "func ParseAnimalDescription() string {", + (string) (len=18) "\treturn `Animal x`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=50) "\t// CasesTest_lower is a Cases of type Test_lower.", @@ -355,6 +358,9 @@ (string) (len=31) "func ParseCasesExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=37) "func ParseCasesDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=40) "\t// ColorBlack is a Color of type Black.", @@ -550,6 +556,9 @@ (string) (len=31) "func ParseColorExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=37) "func ParseColorDescription() string {", + (string) (len=61) "\treturn `Color is an enumeration of colors that are allowed.`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=62) "\t// ColorWithCommentBlack is a ColorWithComment of type Black.", @@ -746,6 +755,9 @@ (string) (len=42) "func ParseColorWithCommentExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=48) "func ParseColorWithCommentDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=64) "\t// ColorWithComment2Black is a ColorWithComment2 of type Black.", @@ -942,6 +954,9 @@ (string) (len=43) "func ParseColorWithComment2Example() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=49) "func ParseColorWithComment2Description() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=64) "\t// ColorWithComment3Black is a ColorWithComment3 of type Black.", @@ -1157,6 +1172,9 @@ (string) (len=43) "func ParseColorWithComment3Example() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=49) "func ParseColorWithComment3Description() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=18) "\t// Skipped value.", @@ -1371,6 +1389,9 @@ (string) (len=43) "func ParseColorWithComment4Example() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=49) "func ParseColorWithComment4Description() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=52) "\t// Enum64bitUnknown is a Enum64bit of type Unknown.", @@ -1614,6 +1635,9 @@ (string) (len=35) "func ParseEnum64bitExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=41) "func ParseEnum64bitDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=42) "\t// ModelToyota is a Model of type Toyota.", @@ -1789,6 +1813,9 @@ (string) (len=31) "func ParseModelExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=37) "func ParseModelDescription() string {", + (string) (len=17) "\treturn `Model x`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=60) "\t// NonASCIIПродам is a NonASCII of type Продам.", @@ -1960,6 +1987,9 @@ (string) (len=34) "func ParseNonASCIIExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=40) "func ParseNonASCIIDescription() string {", + (string) (len=18) "\treturn `NonASCII`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=61) "\t// SanitizingTestHyphen is a Sanitizing of type Test-Hyphen.", @@ -2155,6 +2185,9 @@ (string) (len=36) "func ParseSanitizingExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=42) "func ParseSanitizingDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=36) "\t// SodaCoke is a Soda of type Coke.", @@ -2326,6 +2359,9 @@ (string) (len=30) "func ParseSodaExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=36) "func ParseSodaDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=68) "\t// StartNotZeroStartWithNum is a StartNotZero of type StartWithNum.", @@ -2491,6 +2527,9 @@ (string) (len=38) "func ParseStartNotZeroExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=44) "func ParseStartNotZeroDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=52) "\t// StringEnumRandom is a StringEnum of type random.", @@ -2614,5 +2653,8 @@ (string) (len=36) "func ParseStringEnumExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=42) "func ParseStringEnumDescription() string {", + (string) (len=21) "\treturn `StringEnum.`", + (string) (len=1) "}", (string) "" } diff --git a/generator/.snapshots/TestExampleFileMoreOptions b/generator/.snapshots/TestExampleFileMoreOptions index 8dfd956..598607d 100644 --- a/generator/.snapshots/TestExampleFileMoreOptions +++ b/generator/.snapshots/TestExampleFileMoreOptions @@ -1,4 +1,4 @@ -([]string) (len=2616) { +([]string) (len=2658) { (string) (len=41) "// Code generated by go-enum DO NOT EDIT.", (string) (len=13) "// Version: -", (string) (len=14) "// Revision: -", @@ -184,6 +184,9 @@ (string) (len=32) "func ParseAnimalExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=38) "func ParseAnimalDescription() string {", + (string) (len=18) "\treturn `Animal x`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=50) "\t// CasesTest_lower is a Cases of type Test_lower.", @@ -355,6 +358,9 @@ (string) (len=31) "func ParseCasesExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=37) "func ParseCasesDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=40) "\t// ColorBlack is a Color of type Black.", @@ -550,6 +556,9 @@ (string) (len=31) "func ParseColorExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=37) "func ParseColorDescription() string {", + (string) (len=61) "\treturn `Color is an enumeration of colors that are allowed.`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=62) "\t// ColorWithCommentBlack is a ColorWithComment of type Black.", @@ -746,6 +755,9 @@ (string) (len=42) "func ParseColorWithCommentExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=48) "func ParseColorWithCommentDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=64) "\t// ColorWithComment2Black is a ColorWithComment2 of type Black.", @@ -942,6 +954,9 @@ (string) (len=43) "func ParseColorWithComment2Example() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=49) "func ParseColorWithComment2Description() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=64) "\t// ColorWithComment3Black is a ColorWithComment3 of type Black.", @@ -1157,6 +1172,9 @@ (string) (len=43) "func ParseColorWithComment3Example() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=49) "func ParseColorWithComment3Description() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=18) "\t// Skipped value.", @@ -1371,6 +1389,9 @@ (string) (len=43) "func ParseColorWithComment4Example() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=49) "func ParseColorWithComment4Description() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=52) "\t// Enum64bitUnknown is a Enum64bit of type Unknown.", @@ -1614,6 +1635,9 @@ (string) (len=35) "func ParseEnum64bitExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=41) "func ParseEnum64bitDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=42) "\t// ModelToyota is a Model of type Toyota.", @@ -1789,6 +1813,9 @@ (string) (len=31) "func ParseModelExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=37) "func ParseModelDescription() string {", + (string) (len=17) "\treturn `Model x`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=60) "\t// NonASCIIПродам is a NonASCII of type Продам.", @@ -1960,6 +1987,9 @@ (string) (len=34) "func ParseNonASCIIExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=40) "func ParseNonASCIIDescription() string {", + (string) (len=18) "\treturn `NonASCII`", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=61) "\t// SanitizingTestHyphen is a Sanitizing of type Test-Hyphen.", @@ -2155,6 +2185,9 @@ (string) (len=36) "func ParseSanitizingExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=42) "func ParseSanitizingDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=36) "\t// SodaCoke is a Soda of type Coke.", @@ -2326,6 +2359,9 @@ (string) (len=30) "func ParseSodaExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=36) "func ParseSodaDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=68) "\t// StartNotZeroStartWithNum is a StartNotZero of type StartWithNum.", @@ -2491,6 +2527,9 @@ (string) (len=38) "func ParseStartNotZeroExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=44) "func ParseStartNotZeroDescription() string {", + (string) (len=10) "\treturn ``", + (string) (len=1) "}", (string) "", (string) (len=7) "const (", (string) (len=52) "\t// StringEnumRandom is a StringEnum of type random.", @@ -2614,5 +2653,8 @@ (string) (len=36) "func ParseStringEnumExample() bool {", (string) (len=12) "\treturn true", (string) (len=1) "}", + (string) (len=42) "func ParseStringEnumDescription() string {", + (string) (len=21) "\treturn `StringEnum.`", + (string) (len=1) "}", (string) "" } diff --git a/generator/generator.go b/generator/generator.go index 8466875..e51000e 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -59,10 +59,11 @@ type Generator struct { // Enum holds data for a discovered enum in the parsed source type Enum struct { - Name string - Prefix string - Type string - Values []EnumValue + Name string + Prefix string + Type string + Values []EnumValue + Comment string } // EnumValue holds the individual data for each enum value within the found enum. @@ -385,6 +386,9 @@ func (g *Generator) parseEnum(ts *ast.TypeSpec) (*Enum, error) { enum.Prefix = g.prefix + enum.Prefix } + commentPreEnumDecl, _, _ := strings.Cut(ts.Doc.Text(), `ENUM(`) + enum.Comment = strings.TrimSpace(commentPreEnumDecl) + enumDecl := getEnumDeclFromComments(ts.Doc.List) if enumDecl == "" { return nil, errors.New("failed parsing enum")