Documentation
¶
Overview ¶
Package commentparsing recovers a config tree's doc comments and feeds them to code generators.
Run closes that gap from one root value: it parses the comments of types this module declares, reads the compiled DocComments method of types from a dependency, and writes those methods for the local packages so the next module downstream can do the same. See the examples below, the examples directory for whole working commands, and the README for the wider arrangement.
The generated methods are a build artifact, so they can fall behind the comments they were read from. A package that ships them should regenerate in CI and fail on a diff.
Example ¶
A generator names one root and nothing else. commentparsing.Files returns the output rather than writing it, so it can be printed here; a real //go:generate command calls commentparsing.Run and lets the files reach disk.
package main
import (
"fmt"
"path/filepath"
"github.com/smartcontractkit/chainlink-common/x/config/commentparsing"
"github.com/smartcontractkit/chainlink-common/x/config/commentparsing/examples/simple"
"github.com/smartcontractkit/chainlink-common/x/config/markup/tomlmarkup"
)
func main() {
files, err := commentparsing.Files(commentparsing.RunArgs{
Dir: filepath.Join("examples", "simple"),
Roots: []any{&simple.Config{}},
Markup: tomlmarkup.New(),
Tool: "github.com/smartcontractkit/chainlink-common/x/config/commentparsing/examples/simple/gen",
})
if err != nil {
panic(err)
}
fmt.Print(files[filepath.Join(".", commentparsing.GeneratedFileName)])
}
Output: // Code generated by github.com/smartcontractkit/chainlink-common/x/config/commentparsing/examples/simple/gen, DO NOT EDIT. package simple import "github.com/smartcontractkit/chainlink-common/x/config/commentparsing" func (Config) DocComments() map[string]commentparsing.FieldDoc { return map[string]commentparsing.FieldDoc{ "Endpoint": {Comment: "Endpoint is the upstream URL to dial.\nIt must include a scheme, and a trailing path is preserved."}, "Retries": {Comment: "Retries is the attempt count after the first."}, "Timeout": {Comment: "Timeout bounds a single request. Zero means no timeout."}, } }
Index ¶
Examples ¶
Constants ¶
const GeneratedFileName = "doccomments_gen.go"
GeneratedFileName is where the DocComments methods are written. It is skipped when parsing, so a regeneration reads the same sources as the first run.
Variables ¶
var DerefType = decode.DerefType
DerefType strips every level of pointer from t. A nil t stays nil.
Functions ¶
func Files ¶
Files discovers the config tree under args and returns what every generator produced from it, keyed by path relative to RunArgs.Dir, cleaned. A path that leaves that directory is an error: nothing outside it is a run's to write, and nothing outside it is covered by the staleness handling Run applies.
The content is what Run would write, header and gofmt included, so a caller that must inspect the output before it lands - a check that committed files are up to date, say - compares the bytes it is given against the bytes on disk.
func Lookup ¶
Lookup returns the documentation recorded for t, following pointers to the struct type.
A failure means the generator has not run over the package that declares t.
One gap is not reported: a type that embeds a documented type promotes its method, so a struct whose own package never generated answers with the embedded type's fields. Those comments are right for the fields the embed promoted, and the embedder's own fields are simply absent - undocumented, as any field without a comment is. Only a field that shadows an embedded one by name takes the wrong comment; two embeds promoting one name are refused at generation instead.
Example ¶
A consumer reads the generated methods back at run time, with no access to the source they came from.
package main
import (
"fmt"
"reflect"
"github.com/smartcontractkit/chainlink-common/x/config/commentparsing"
"github.com/smartcontractkit/chainlink-common/x/config/commentparsing/examples/simple"
)
func main() {
fields, err := commentparsing.Lookup(reflect.TypeFor[simple.Config]())
if err != nil {
panic(err)
}
fmt.Println(fields["Timeout"].Comment)
}
Output: Timeout bounds a single request. Zero means no timeout.
func Run ¶
Run generates as Files does and writes the result.
The reserved GeneratedFileName is managed rather than only written: a file under that name that RunArgs.Tool wrote and this run did not produce is removed, so a package that stops documenting anything does not keep methods for types it no longer has. A file another tool generated is neither removed nor written over, which is what lets two runs share a module; renaming a tool therefore orphans what its old name wrote. A file with no generated-by header at all is left alone too. Either collision fails the run and names the file, before anything is written. Nested modules are left to their own runs.
Types ¶
type DocCommenter ¶
DocCommenter is implemented by the generated code, returning the documentation of the type it was generated for, keyed by Go field name.
type FieldDoc ¶
type FieldDoc struct {
// Comment is the field's whole doc comment, not just the first sentence.
Comment string
}
FieldDoc is what a field's declaration says about itself that compilation discards.
type Generator ¶
Generator turns the discovered packages of a config tree into files to write, keyed by path relative to RunArgs.Dir.
The packages arrive already resolved, whether their comments came from local source or from a dependency's compiled methods.
type Package ¶
type Package struct {
// ImportPath matches what reflection reports as a type's package. Empty for a package parsed
// without one.
ImportPath string
// Name is the package clause.
Name string
// Dir is where the package's generated files belong. [ParseDir] reports the directory it
// read; the walk rewrites it relative to the run directory.
Dir string
// Types are sorted by name, so regenerating an unchanged package produces an unchanged file.
Types []Type
// contains filtered or unexported fields
}
Package is one package of a config tree, as a Generator receives it.
func ParseDir ¶
ParseDir reads the doc comments of every struct type declared in the package rooted at dir. Test files are excluded.
Only the files the active build selects are read, as go/build.Context.MatchFile decides it from GOOS, GOARCH and the constraints in each file, so a package declaring one type across config_linux.go and config_windows.go yields the one the build in hand compiles rather than both. Since what generation writes is untagged, a config type that exists on one platform only is not supported: generating on another misses it, and generating on its own leaves a receiver the others cannot compile. A type behind a -tags build tag is not visible here either.
type RunArgs ¶
type RunArgs struct {
// Roots are the config values whose type trees are walked. List every type a consumer may
// reach on its own, not only the outermost config.
Roots []any
// Markup is the required config language, such as tomlmarkup.New(). The walk stops at a type
// it reads whole, whose fields aren't configured one at a time and so need no docs.
Markup markup.Markup
// Dir anchors the run: the module enclosing it decides which types are local, and every
// generated file path is relative to it. Empty means the working directory.
Dir string
// LocalPrefix groups this repository's imports after third-party ones, as goimports' -local
// does.
LocalPrefix string
// Tool names the generator in each file's "Code generated by" header.
Tool string
}
RunArgs are the inputs that cannot be derived from the config tree itself.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
nested
Package nested is a config package whose tree reaches into a second package of this module.
|
Package nested is a config package whose tree reaches into a second package of this module. |
|
nested/gen
command
Command gen generates the DocComments for this example.
|
Command gen generates the DocComments for this example. |
|
nested/upstream
Package upstream is a second package of this module that the consumer's config tree reaches into.
|
Package upstream is a second package of this module that the consumer's config tree reaches into. |
|
simple
Package simple is a config package that documents itself.
|
Package simple is a config package that documents itself. |
|
simple/gen
command
Command gen generates the DocComments for this example.
|
Command gen generates the DocComments for this example. |