-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
704 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#! /usr/bin/make | ||
# | ||
# Makefile for the Goa Model plugin | ||
|
||
# include common Makefile content for plugins | ||
GOPATH=$(shell go env GOPATH) | ||
include ../plugins.mk | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Model Plugin | ||
|
||
The `model` plugin is a [Goa](https://github.com/goadesign/goa/tree/v3) plugin | ||
that validates a [Model](https://github.com/goadesign/model) diagram DSL against | ||
a Goa design. The plugin generates errors if there are services present in the | ||
Goa design with no corresponding container in the Model diagram. The plugin can | ||
also validate that all the containers in the Model diagram have a corresponding | ||
service in the Goa design. | ||
|
||
## Enabling the Plugin | ||
|
||
To enable the plugin simply import the model plugin `dsl` package in the Goa | ||
design: | ||
|
||
```go | ||
import ( | ||
. "goa.design/goa/v3/dsl" | ||
. "goa.design/plugins/v3/model/dsl" | ||
) | ||
``` | ||
|
||
Running `goa gen` will now execute the plugin and validate that all services | ||
have a corresponding container in the model (unless configured otherwise, see | ||
below). | ||
|
||
## Usage | ||
|
||
The plugin extends the Goa DSL as follows: | ||
|
||
* The `Model` function sets the `model` Go package and the name of the software | ||
system to validate against, this is the only required DSL. | ||
* The `ModelContainerNameFormat` function sets the format used by the plugin to | ||
map service to container names. | ||
* The `ModelExcludedTags` function sets a list of case-insensitive tags that | ||
exclude containers equipped with them from the validation process. | ||
* The `ModelComplete` function enables the validation that all the containers in | ||
the Model diagram have a corresponding service in the Goa design. | ||
|
||
All these functions must appear inside the `API` function: | ||
|
||
```go | ||
var _ = API("calc", func() { | ||
// Set the model package and the name of the software system | ||
Model("goa.design/model/examples/basic/model", "calc") | ||
|
||
// Set the container name format, default is "%s Service" | ||
ModelContainerFormat("%s Service") | ||
|
||
// Exclude containers with the "database", "external" and "thirdparty" tags (default) | ||
ModelExcludedTags("database", "external", "thirdparty") | ||
|
||
// Enable the validation that all the containers in the Model diagram have a | ||
// corresponding service. Not enabled by default. | ||
ModelComplete() | ||
}) | ||
``` | ||
|
||
The plugin also defines the following DSL functions that must appear under a | ||
`Service` definition: | ||
|
||
* The `ModelContainer` function sets the name of the corresponding diagram | ||
container. This overrides the name computed from the `ContainerNameFormat` | ||
format. | ||
* The `ModelNone` function disables the validation for the service. | ||
|
||
```go | ||
var _ = Service("add", func() { | ||
// Set the container name | ||
ModelContainer("Add Service") | ||
// ... | ||
}) | ||
|
||
var _ = Service("no diagram", func() { | ||
// Disable the validation for this service | ||
ModelNone() | ||
// ... | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package dsl | ||
|
||
import ( | ||
"goa.design/goa/v3/eval" | ||
goaexpr "goa.design/goa/v3/expr" | ||
"goa.design/plugins/v3/model/expr" | ||
|
||
// Register code generators for the Model plugin | ||
_ "goa.design/plugins/v3/model" | ||
) | ||
|
||
// Model specifies the import path of the Go package that contains the model as | ||
// well as the name of the system to be validated. | ||
// | ||
// Model must appear in the API expression. | ||
// | ||
// Example: | ||
// | ||
// import . "goa.design/plugins/v3/model" | ||
// | ||
// var _ = API("calc", func() { | ||
// Model("goa.design/model/examples/basic/model", "calc") | ||
// }) | ||
func Model(path, systemName string) { | ||
expr.Root.ModelPkgPath = path | ||
expr.Root.SystemName = systemName | ||
} | ||
|
||
// ModelContainerFormat specifies a fmt.Sprintf format string that is used | ||
// to compute the name of the model container from the service name. | ||
// The default value is "%s Service". | ||
// | ||
// ModelContainerFormat must appear in the API expression. | ||
// | ||
// Example: | ||
// | ||
// import . "goa.design/plugins/v3/model" | ||
// | ||
// var _ = API("calc", func() { | ||
// Model("goa.design/model/examples/basic/model", "calc") | ||
// ModelContainerFormat("%s Service") | ||
// }) | ||
func ModelContainerFormat(format string) { | ||
expr.Root.ContainerNameFormat = format | ||
} | ||
|
||
// ModelExcludedTags specifies a list of tags. Containers that have any of these | ||
// tags will be excluded from the check. | ||
// By default, the tags "external", "thirdparty" and "database" are excluded. | ||
// | ||
// ModelExcludedTags must appear in the API expression. | ||
// | ||
// Example: | ||
// | ||
// import . "goa.design/plugins/v3/model" | ||
// | ||
// var _ = API("calc", func() { | ||
// Model("goa.design/model/examples/basic/model", "calc") | ||
// ModelExcludedTags("external", "thirdparty", "database") | ||
// }) | ||
func ModelExcludedTags(tags ...string) { | ||
expr.Root.ExcludedTags = tags | ||
} | ||
|
||
// ModelComplete marks the model as complete, meaning that the model plugin | ||
// should validate that all containers have a corresponding service in addition | ||
// to validating that each service has a corresponding container. | ||
// | ||
// ModelComplete must appear in the API expression. | ||
// | ||
// Example: | ||
// | ||
// import . "goa.design/plugins/v3/model" | ||
// | ||
// var _ = API("calc", func() { | ||
// Model("goa.design/model/examples/basic/model", "calc") | ||
// ModelComplete() | ||
// }) | ||
func ModelComplete() { | ||
expr.Root.Complete = true | ||
} | ||
|
||
// ModelContainer sets the name of the container for the enclosing service. | ||
// | ||
// ModelContainer must appear in a Service expression. | ||
// | ||
// Example: | ||
// | ||
// import . "goa.design/plugins/v3/model" | ||
// | ||
// var _ = API("calc", func() { | ||
// Service("adder", func() { | ||
// ModelContainer("Adder") | ||
// }) | ||
// }) | ||
func ModelContainer(name string) { | ||
svc, ok := eval.Current().(*goaexpr.ServiceExpr) | ||
if !ok { | ||
eval.IncompatibleDSL() | ||
return | ||
} | ||
expr.Root.ServiceContainer[svc.Name] = name | ||
} | ||
|
||
// ModelNone disables the model validation for the enclosing service. | ||
// | ||
// ModelNone must appear in a Service expression. | ||
// | ||
// Example: | ||
// | ||
// import . "goa.design/plugins/v3/model" | ||
// | ||
// var _ = API("calc", func() { | ||
// Service("adder", func() { | ||
// ModelNone() | ||
// }) | ||
// }) | ||
func ModelNone() { | ||
svc, ok := eval.Current().(*goaexpr.ServiceExpr) | ||
if !ok { | ||
eval.IncompatibleDSL() | ||
return | ||
} | ||
expr.Root.ServiceContainer[svc.Name] = "" | ||
} |
Oops, something went wrong.