2026-01-21 22:00:31 +01:00
|
|
|
// Package generate produces Go source code that registers documentation for use with the docreflect package.
|
2025-08-08 21:44:31 +02:00
|
|
|
package generate
|
|
|
|
|
|
2026-01-21 22:00:31 +01:00
|
|
|
import "io"
|
2025-08-26 21:02:10 +02:00
|
|
|
|
2026-01-13 23:28:46 +01:00
|
|
|
// Options contains options for the generator.
|
|
|
|
|
type Options struct {
|
|
|
|
|
|
|
|
|
|
// Main indicates that the docs for the symbols will be lookded up as part of the main package of an
|
2026-01-21 22:00:31 +01:00
|
|
|
// executable. This is necessary, because the fully qualified Go path of symbols in a main package differs
|
|
|
|
|
// from the path pointing to that package in a module.
|
2026-01-13 23:28:46 +01:00
|
|
|
Main bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-21 22:00:31 +01:00
|
|
|
// GenerateRegistry generates a Go source file containing an init function that registers the documentation for
|
|
|
|
|
// the declarations specified by their fully qualified Go path.
|
|
|
|
|
//
|
|
|
|
|
// The paths argument accepts arbitrary packages, package-level symbols, or struct fields. Usage of package
|
|
|
|
|
// paths is recommended over specific symbols in most cases.
|
|
|
|
|
//
|
|
|
|
|
// Limitations:
|
2025-08-17 15:59:01 +02:00
|
|
|
//
|
2026-01-21 22:00:31 +01:00
|
|
|
// - Type references (such as type aliases or definitions based on named types) are not resolved.
|
2025-08-17 15:59:01 +02:00
|
|
|
//
|
2026-01-21 22:00:31 +01:00
|
|
|
// - Import paths are not followed.
|
|
|
|
|
func GenerateRegistry(o Options, w io.Writer, outputPackageName string, path ...string) error {
|
2026-01-13 23:28:46 +01:00
|
|
|
oo := initOptions()
|
|
|
|
|
oo.isMain = o.Main
|
2026-01-21 22:00:31 +01:00
|
|
|
d, err := generate(oo, path...)
|
2025-08-08 21:44:31 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := format(w, outputPackageName, d); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|