Skip to content

Commit

Permalink
Add 'model' plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael committed Jun 15, 2024
1 parent 9303231 commit 524e522
Show file tree
Hide file tree
Showing 14 changed files with 704 additions and 64 deletions.
23 changes: 2 additions & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Collect Workflow Telemetry
uses: runforesight/foresight-workflow-kit-action@v1
if: success() || failure()

- name: Set up Go 1.21
- name: Set up Go 1.22
uses: actions/setup-go@v5.0.1
with:
go-version: '1.21'
go-version: '1.22'
check-latest: true
id: go

Expand All @@ -31,18 +27,3 @@ jobs:

- name: Build
run: make ci
env:
GOA_BRANCH: ${{ steps.extract_branch.outputs.branch }}

- name: Compute code coverage
run: go test -v -json -coverprofile=coverage.out ./...> ./test-report.json || true

- name: Analyze Test and/or Coverage Results
uses: runforesight/foresight-test-kit-action@v1
if: success() || failure()
with:
test_format: JSON
test_framework: GOLANG
test_path: "./test-report.json"
coverage_format: GOLANG
coverage_path: ./coverage.out
32 changes: 0 additions & 32 deletions .github/workflows/stale.yml

This file was deleted.

13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ PLUGINS=\
docs \
goakit \
i18n \
model \
otel \
types \
zaplogger \
zerologger

PROTOC_VERSION=22.2
UNZIP=unzip
PROTOC_VERSION=27.0
ifeq ($(GOOS),linux)
PROTOC=protoc-$(PROTOC_VERSION)-linux-x86_64
PROTOC_EXEC=$(PROTOC)/bin/protoc
endif
ifeq ($(GOOS),darwin)
PROTOC=protoc-$(PROTOC_VERSION)-osx-x86_64
PROTOC=protoc-$(PROTOC_VERSION)-osx-universal_binary
PROTOC_EXEC=$(PROTOC)/bin/protoc
endif
ifeq ($(GOOS),windows)
Expand All @@ -47,17 +47,18 @@ ci: depend all

depend:
@echo INSTALLING DEPENDENCIES...
@go mod download
@for package in $(DEPEND); do go install $$package; done
@go mod tidy -compat=1.19
@go mod tid
@if [[ `protoc --version` != "libprotoc ${PROTOC_VERSION}" ]]; then
@echo INSTALLING PROTOC...
@mkdir $(PROTOC)
@cd $(PROTOC); \
curl -O -L https://github.com/protocolbuffers/protobuf/releases/download/v$(PROTOC_VERSION)/$(PROTOC).zip; \
$(UNZIP) $(PROTOC).zip
unzip $(PROTOC).zip
@cp $(PROTOC_EXEC) $(GOPATH)/bin && \
rm -rf $(PROTOC) && \
echo "`protoc --version`"
@fi

check-goa:
ifdef GOA
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module goa.design/plugins/v3

go 1.21
go 1.22.0

toolchain go1.22.2

require (
github.com/go-kit/kit v0.13.0
Expand All @@ -9,18 +11,19 @@ require (
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
goa.design/goa/v3 v3.16.2
goa.design/model v1.9.8
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dimfeld/httppath v0.0.0-20170720192232-ee938bf73598 // indirect
github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/manveru/faker v0.0.0-20171103152722-9fbc68a78c4d // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q=
github.com/go-openapi/jsonpointer v0.20.2/go.mod h1:bHen+N0u1KEO3YlmqOjTT9Adn1RfD91Ar825/PuiRVs=
github.com/go-openapi/swag v0.22.8 h1:/9RjDSQ0vbFR+NyjGMkFTsA1IA0fmhKSThmfGZjicbw=
Expand Down Expand Up @@ -53,10 +55,14 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
goa.design/goa/v3 v3.16.2 h1:EwsYmtyhx4bmKjQ/+150nF26DIB5NjgYZyJq6Cg2jxA=
goa.design/goa/v3 v3.16.2/go.mod h1:YAY4TIUGlQH0Rj9AWtAyPktH3WLGWRaGxi3P19RvGXU=
goa.design/model v1.9.8 h1:SGf+q+hYO1rh/Jvq7T0ZbfBcANzi3Lc3RHKWBDZCWCE=
goa.design/model v1.9.8/go.mod h1:RqPSTbZV49gD3+IBsT9/zf+EPxt4zuDPuT/6r857H3w=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
Expand Down
8 changes: 8 additions & 0 deletions model/Makefile
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

78 changes: 78 additions & 0 deletions model/README.md
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()
// ...
})
```
125 changes: 125 additions & 0 deletions model/dsl/model.go
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] = ""
}
Loading

0 comments on commit 524e522

Please sign in to comment.