package main import ( "fmt" "go/ast" "go/parser" "go/token" "os" "path/filepath" "reflect" "strings" ) // modulePath is the module's import path, which every package path starts // with. const modulePath = "uberbringer" // comments maps "pkgpath.Type" and "pkgpath.Type.Field" to the first // paragraph of their doc comments, for the packages whose types the document // describes. Later paragraphs are for Go readers only. // jsonschema's own extractor builds its keys from file paths, which come out // with backslashes on Windows, so the generator reads the comments itself to // give the same document on every platform. type comments map[string]string // extractComments reads the doc comments of every type in the packages of // the given values. func extractComments(root string, of ...any) (comments, error) { out := make(comments) for _, v := range of { pkg := reflect.TypeOf(v).PkgPath() rel, ok := strings.CutPrefix(pkg, modulePath+"/") if !ok { return nil, fmt.Errorf("package %s is outside the module", pkg) } dir := filepath.Join(root, filepath.FromSlash(rel)) entries, err := os.ReadDir(dir) if err != nil { return nil, fmt.Errorf("reading %s: %w", rel, err) } fset := token.NewFileSet() for _, e := range entries { name := e.Name() if e.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { continue } f, err := parser.ParseFile(fset, filepath.Join(dir, name), nil, parser.ParseComments) if err != nil { return nil, err } out.collect(pkg, f) } } return out, nil } func (c comments) collect(pkg string, f *ast.File) { for _, decl := range f.Decls { gen, ok := decl.(*ast.GenDecl) if !ok || gen.Tok != token.TYPE { continue } for _, spec := range gen.Specs { ts := spec.(*ast.TypeSpec) doc := ts.Doc if doc == nil && len(gen.Specs) == 1 { doc = gen.Doc } key := pkg + "." + ts.Name.Name if text := firstParagraph(doc.Text()); text != "" { c[key] = text } st, ok := ts.Type.(*ast.StructType) if !ok { continue } for _, field := range st.Fields.List { text := field.Doc.Text() if text == "" { text = field.Comment.Text() } for _, n := range field.Names { if text != "" { c[key+"."+n.Name] = firstParagraph(text) } } } } } } // lookup is the jsonschema LookupComment hook: field is the Go field name, // or empty for the type itself. A comment starts, as Go doc comments do, // with the Go name; the document shows the schema's or the property's name // there instead. func (c comments) lookup(t reflect.Type, field string) string { key := t.PkgPath() + "." + t.Name() if field == "" { text := c[key] if rest, ok := strings.CutPrefix(text, t.Name()+" "); ok { text = schemaName(t) + " " + rest } return text } text := c[key+"."+field] if f, ok := t.FieldByName(field); ok { name, _, _ := strings.Cut(f.Tag.Get("json"), ",") if rest, ok := strings.CutPrefix(text, field+" "); ok && name != "" { text = name + " " + rest } } return text } // firstParagraph returns a doc comment's text up to its first blank line, on // one line. func firstParagraph(text string) string { para, _, _ := strings.Cut(strings.TrimSpace(text), "\n\n") return strings.Join(strings.Fields(para), " ") }