improving the autogenerated api docs

This commit is contained in:
Jeromy 2020-05-19 08:48:58 -07:00
parent cc44167f2c
commit cb88237305

View File

@ -190,7 +190,7 @@ func (v *Visitor) Visit(node ast.Node) ast.Visitor {
return v
}
func parseApiASTInfo() map[string]string {
func parseApiASTInfo() (map[string]string, map[string]string) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, "./api", nil, parser.AllErrors|parser.ParseComments)
@ -207,16 +207,26 @@ func parseApiASTInfo() map[string]string {
v := &Visitor{make(map[string]ast.Node)}
ast.Walk(v, pkgs["api"])
groupDocs := make(map[string]string)
out := make(map[string]string)
for mn, node := range v.Methods {
cs := cmap.Filter(node).Comments()
if len(cs) == 0 {
out[mn] = "NO COMMENTS"
out[mn] = "There are not yet any comments for this method."
} else {
for _, c := range cs {
if strings.HasPrefix(c.Text(), "MethodGroup:") {
parts := strings.Split(c.Text(), "\n")
groupName := strings.TrimSpace(parts[0][12:])
comment := strings.Join(parts[1:], "\n")
groupDocs[groupName] = comment
}
}
out[mn] = cs[len(cs)-1].Text()
}
}
return out
return out, groupDocs
}
type MethodGroup struct {
@ -244,7 +254,7 @@ func methodGroupFromName(mn string) string {
func main() {
comments := parseApiASTInfo()
comments, groupComments := parseApiASTInfo()
groups := make(map[string]*MethodGroup)
@ -258,7 +268,7 @@ func main() {
g, ok := groups[groupName]
if !ok {
g = new(MethodGroup)
g.Header = groupName
g.Header = groupComments[groupName]
g.GroupName = groupName
groups[groupName] = g
}