descriptions

package
v1.41.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package descriptions reads the doc comments of Go types and of their fields from source, so the route registry, which builds its documents by reflection where comments do not exist, can describe what it publishes.

A build step runs Read (through `compose descriptions`) and writes the result with Marshal to a file the app embeds, and api.Descriptions loads that file. The keys are Key(t) for a type, "github.com/x/app/runs.Run", and FieldKey(t, name) for a field, "github.com/x/app/runs.Run#Status". An import path can hold a dot but never a #, so no field key is also a type key.

A type declared more than once in its package, such as in a cgo and a !cgo file, is not described, nor are its fields: with one declaration there is one truth. A defined type over a struct of the same package, type A B, gets the field entries of B under A, as reflection reports B's fields with A as their struct, also through a chain such as type C A, when A, every type along the chain and the struct are each declared once in the package. A defined type over a struct of another package gets none.

A directory given is keyed by where it is, after symbolic links are followed: given as a link to another module's package, it is keyed by that module's import path, though a binary built from the linking module reports the linking module's. A link to a directory below a directory given is not followed. A type declared inside a function shares its key with a package-level type of the same name, so it gets that type's description. The descriptions are the same for the same files, whatever the order of the directories given and whether they are reached through relative paths, absolute paths or symbolic links. A directory must be given in the letter case it has on disk, as its import path is built from the argument.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommentText

func CommentText(group *ast.CommentGroup) string

CommentText is a doc comment as a description: its text without the "Deprecated:" paragraph (the schema says deprecated) and without TODO, FIXME and nolint lines, which are notes for maintainers.

func FieldKey

func FieldKey(t reflect.Type, field string) string

FieldKey returns the key of the field named field of the struct t: Key(t), a # and the Go field name. It returns "" when Key(t) does.

func FieldText

func FieldText(field *ast.Field) string

FieldText describes a struct field: its doc comment, then its line comment.

func Key

func Key(t reflect.Type) string

Key returns the key of a named type: its package path, a dot and its name without type arguments, so every instantiation of a generic type shares the comment of its declaration. Key returns "" for an unnamed type and for a predeclared one. A type of a package main has the package path "main" in a binary built with go build or go run, and its import path in a test binary. Read writes its entries under its import path, and also under main when it is the only main package read that declares types.

func Marshal

func Marshal(descriptions map[string]string) ([]byte, error)

Marshal returns descriptions as the file api.Descriptions loads: one JSON object with sorted keys, indented, ending in a newline, the same bytes for the same map.

func TypeComment

func TypeComment(gen *ast.GenDecl, spec *ast.TypeSpec) *ast.CommentGroup

TypeComment returns the doc comment of the type spec declares: the one on spec, or the one on gen when gen declares that type alone.

Types

type Duplicate

type Duplicate struct {
	Key    string // the key of the type
	First  string // the first file that declares it, in walk order
	Second string // the second file that declares it
}

Duplicate is a type declared more than once in its package. Read describes neither it nor its fields.

type Result

type Result struct {
	// Descriptions maps each key to its text. It is what Marshal writes.
	Descriptions map[string]string
	// Duplicates are the types declared more than once in their package,
	// left out of Descriptions, sorted by key.
	Duplicates []Duplicate
	// MainPackages are the import paths of the main packages that declare
	// types, sorted, when there are two or more of them and one has an
	// entry. No key is then written under main, so those entries are not
	// found in a binary built with go build or go run. It is empty otherwise.
	MainPackages []string
}

Result is what Read found.

func Read

func Read(dirs ...string) (Result, error)

Read walks each directory and returns the descriptions of the types declared under it, keyed as Key and FieldKey do. A type of a main package written under main has the keys main.Run and main.Run#Status.

Each directory given is made absolute and its symbolic links are resolved, so a file is read once whether the arguments reach it through relative paths, absolute paths or symbolic links. A directory must be given in the letter case it has on disk, as its import path is built from the argument. Below it, Read skips the directories named vendor or testdata, those whose name starts with "." or "_", and symbolic links to directories, as ./... does. It skips the files whose name starts with "." or "_", the _test.go files, the files with a //go:build ignore line and the files of package documentation, which the go command ignores. It reads every other .go file, a symbolic link to a file too, whatever its build constraints, so the result is the same on every platform. A file that does not parse fails Read, a skipped name aside. Every package-level named type with a doc comment has an entry, unexported ones too, and so does every exported field of a package-level struct type with a comment, an embedded one under the name of its type. Aliases have none.

The import path of a package is the module path of the nearest go.mod above it joined with its directory below that go.mod, or what follows the last vendor element of that directory. A main package is an import path whose files read all say package main. When exactly one main package read declares a type, an alias counting, its entries are also written under "main". When two or more do, none is, and Result.MainPackages names them. A package whose import path is exactly main, in a module named main, has entries only when it is the one main package, as its keys are then its main keys. Otherwise its keys would be those of another command's types, and a library there is in no binary, as main cannot be imported. A package below it, such as main/sub, is keyed as usual.

A type declared more than once in its package, of any kind and whatever its comments, has no entry and its fields have none, as which declaration a binary holds depends on build constraints Read does not evaluate. It is reported as a Duplicate. No description is better than a wrong one.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL