package jsonschemaimport (gopath)// ExtractGoComments will read all the go files contained in the provided path,// including sub-directories, in order to generate a dictionary of comments// associated with Types and Fields. The results will be added to the `commentsMap`// provided in the parameters and expected to be used for Schema "description" fields.//// The `go/parser` library is used to extract all the comments and unfortunately doesn't// have a built-in way to determine the fully qualified name of a package. The `base` paremeter,// the URL used to import that package, is thus required to be able to match reflected types.//// When parsing type comments, we use the `go/doc`'s Synopsis method to extract the first phrase// only. Field comments, which tend to be much shorter, will include everything.func (, string, map[string]string) error { := token.NewFileSet() := make(map[string][]*ast.Package) := filepath.Walk(, func( string, fs.FileInfo, error) error {if != nil {return }if .IsDir() { , := parser.ParseDir(, , nil, parser.ParseComments)if != nil {return }for , := range {// paths may have multiple packages, like for tests := gopath.Join(, ) [] = append([], ) } }returnnil })if != nil {return }for , := range {for , := range { := "" := ""ast.Inspect(, func( ast.Node) bool {switch x := .(type) {case *ast.TypeSpec: = .Name.String()if !ast.IsExported() { = "" } else { := .Doc.Text()if == "" && != "" { = = "" } = doc.Synopsis() [fmt.Sprintf("%s.%s", , )] = strings.TrimSpace() }case *ast.Field: := .Doc.Text()if == "" { = .Comment.Text() }if != "" && != "" {for , := range .Names {ifast.IsExported(.String()) { := fmt.Sprintf("%s.%s.%s", , , ) [] = strings.TrimSpace() } } }case *ast.GenDecl:// remember for the next type = .Doc.Text() }returntrue }) } }returnnil}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.