Skip to content

Commit

Permalink
feat: make enum comment available in templates (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvelabs committed Jul 25, 2023
1 parent eaf68b4 commit bf63e10
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 7 deletions.
3 changes: 3 additions & 0 deletions example/user_template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
func Parse{{.enum.Name}}Example() bool {
return true
}
func Parse{{.enum.Name}}Description() string {
return `{{.enum.Comment}}`
}
3 changes: 3 additions & 0 deletions example/user_template_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/user_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
5 changes: 4 additions & 1 deletion generator/.snapshots/Test118ExampleFileMoreOptions-1.18
Original file line number Diff line number Diff line change
@@ -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: -",
Expand Down Expand Up @@ -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) ""
}
44 changes: 43 additions & 1 deletion generator/.snapshots/Test118ExampleFileMoreOptions-og
Original file line number Diff line number Diff line change
@@ -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: -",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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 Продам.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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) ""
}
44 changes: 43 additions & 1 deletion generator/.snapshots/TestExampleFileMoreOptions
Original file line number Diff line number Diff line change
@@ -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: -",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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 Продам.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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) ""
}
12 changes: 8 additions & 4 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit bf63e10

Please sign in to comment.