Skip to content

Commit

Permalink
chore(internal/protoveneer): add removeOtherDoc config (#9438)
Browse files Browse the repository at this point in the history
Add a config option to remove all documentation from a symbol except for the first line.
  • Loading branch information
jba committed Feb 20, 2024
1 parent 07f4c9f commit 7421626
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/protoveneer/cmd/protoveneer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ type typeConfig struct {
// Custom conversion functions: "tofunc, fromfunc"
ConvertToFrom string `yaml:"convertToFrom"`
// Doc string for the type, omitting the initial type name.
// This replaces the first line of the doc.
Doc string
// Remove all but the first line of the doc.
RemoveOtherDoc bool `yaml:"removeOtherDoc"`
// Verb to place after type name in doc. Default: "is".
// Ignored if Doc is non-empty.
DocVerb string `yaml:"docVerb"`
Expand Down
11 changes: 8 additions & 3 deletions internal/protoveneer/cmd/protoveneer/protoveneer.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func processEnumValues(d *ast.GenDecl, tc *typeConfig) []string {
if tc != nil {
vs.Type.(*ast.Ident).Name = tc.Name
}
modifyCommentGroup(vs.Doc, protoName, veneerName, "means", "")
modifyCommentGroup(vs.Doc, protoName, veneerName, "means", "", false)
}
return valueNames
}
Expand All @@ -568,9 +568,11 @@ func veneerValueName(protoValueName string, tc *typeConfig) string {
func processDoc(gd *ast.GenDecl, protoName string, tc *typeConfig) {
doc := ""
verb := ""
remOther := false
if tc != nil {
doc = tc.Doc
verb = tc.DocVerb
remOther = tc.RemoveOtherDoc
}

spec := gd.Specs[0]
Expand All @@ -586,10 +588,10 @@ func processDoc(gd *ast.GenDecl, protoName string, tc *typeConfig) {
if tc != nil && name != tc.Name {
panic(fmt.Errorf("GenDecl name is %q, config name is %q", name, tc.Name))
}
modifyCommentGroup(gd.Doc, protoName, name, verb, doc)
modifyCommentGroup(gd.Doc, protoName, name, verb, doc, remOther)
}

func modifyCommentGroup(cg *ast.CommentGroup, protoName, veneerName, verb, doc string) {
func modifyCommentGroup(cg *ast.CommentGroup, protoName, veneerName, verb, doc string, removeOther bool) {
if cg == nil {
return
}
Expand All @@ -598,6 +600,9 @@ func modifyCommentGroup(cg *ast.CommentGroup, protoName, veneerName, verb, doc s
}
c := cg.List[0]
c.Text = "// " + adjustDoc(strings.TrimPrefix(c.Text, "// "), protoName, veneerName, verb, doc)
if removeOther {
cg.List = cg.List[:1]
}
}

// adjustDoc takes a doc string with initial comment characters and whitespace removed, and returns
Expand Down

0 comments on commit 7421626

Please sign in to comment.