Documentation
¶
Overview ¶
Packages in the Plugin directory contain plugins for [goa v3](https://pkg.go.dev/goa.design/goa/v3). Plugins can extend the goa DSL, generate new artifacts and modify the output of existing generators.
Examples include:
The [goakit](https://pkg.go.dev/goa.design/plugins/v3/goakit) plugin generates code that integrates with the [go-kit](https://github.com/go-kit/kit) library.
The [cors](https://pkg.go.dev/goa.design/plugins/v3/cors) plugin adds new DSL to define CORS policies. The plugin generates HTTP server code that responds to CORS requests in compliance with the policies defined in the design.
Writing a Plugin ¶
Writing a plugin consists of two steps:
1. Writing the functions that gets called by the goa tool during code generation.
2. Registering the functions with the goa tool.
A plugin may implement the "gen" goa command, the "example" goa command or both. In each case a plugin may register one or two functions: the first function called "Prepare" gets called prior to any code generation actually happening. The function can modify the design data structures before goa uses them to generate code. The second function called "Generate" does the actual code generation.
The signature of the Generate function is:
func (genpkg string, roots []eval.Root, files []*codegen.File) ([]*codegen.File, error)
where:
"genpkg" is the Go import path to the top level generated package ("gen")
"roots" is the set of design roots created by the DSL.
"files" is the current set of generated files.
The function must return the entire set of generated files (even the files that the plugin does not modify).
The functions must then be registered with the goa code generator tool using the "RegisterPlugin" function of the "codegen" package. This is typically done in a package "init" function, for example:
// Register the plugin.
func init() {
codegen.RegisterPlugin("my-plugin", "gen", Prepare, Generate)
codegen.RegisterPlugin("my-plugin", "example", Prepare, Example)
}
The first argument names the plugin. The second argument must be either "gen" or "example" and identifies the goa command that runs the plugin. The third argument is the Prepare function, or nil when the plugin does not change the design. The last argument is the required Generate function.
Extending The DSL ¶
A plugin may introduce new DSL "keywords" (typically Go package functions). The DSL functions initialize the content of a design root object (an object that implements the "eval.Root" interface), for example:
func MyDSLMethod(arg string) {
// validate where DSL function is called, in this example it must be called
// outside of all other expression
if _, ok := eval.Current().(eval.TopExpr); !ok {
eval.IncompatibleDSL()
return nil
}
// create corresponding design object
expr := &design.MyExpr{ Name: arg }
// store design object in root
design.Root.Exprs = append(design.Root.Exprs, expr)
}
The [eval](https://godoc.org/goa.design/goa/v3/codegen/eval) package contains a number of functions that can be leveraged to implement the DSL such as error reporting functions.
The plugin design root object is then given to the plugin Generate function (if there's one) which may take advantage of it to generate the code.
Directories
¶
| Path | Synopsis |
|---|---|
|
example/cmd/example
command
|
|
|
examples/calc/cmd/calc
command
|
|
|
examples/calc/cmd/calc-cli
command
|
|
|
examples/calc/cmd/calc
command
|
|
|
examples/calc/cmd/calc-cli
command
|
|
|
This file writes go-kit request and response wrappers using the names chosen while Goa planned the generated client and server packages.
|
This file writes go-kit request and response wrappers using the names chosen while Goa planned the generated client and server packages. |
|
examples/calc/cmd/calc
command
|
|
|
examples/calc/cmd/calc-cli
command
|
|
|
examples/calc/cmd/calc
command
|
|
|
examples/calc/cmd/calc-cli
command
|
|
|
Package mains defines a Goa plugin that customizes the example server main layout to follow the recommended conventions.
|
Package mains defines a Goa plugin that customizes the example server main layout to follow the recommended conventions. |
|
Package otel is a Goa plugin that sets the http.route OpenTelemetry span attribute on generated HTTP handlers.
|
Package otel is a Goa plugin that sets the http.route OpenTelemetry span attribute on generated HTTP handlers. |
|
This file registers the testing plugin and supports both current per-run Goa plans and the released callback API used by existing applications.
|
This file registers the testing plugin and supports both current per-run Goa plans and the released callback API used by existing applications. |
|
codegen
This file renders a typed test client with one method branch for each transport selected by the Goa design.
|
This file renders a typed test client with one method branch for each transport selected by the Goa design. |
|
example/cmd/example
command
|
|
|
This file plans and writes standalone Go types for every named type in a Goa design.
|
This file plans and writes standalone Go types for every named type in a Goa design. |
|
This file changes Goa's starter service and server examples to create and pass a Zap logger.
|
This file changes Goa's starter service and server examples to create and pass a Zap logger. |
|
examples/calc/cmd/calc
command
|
|
|
examples/calc/cmd/calc-cli
command
|
|