Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions plugin/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package plugin

import (
"encoding/json"
"os"
"path/filepath"
"text/template"

Expand All @@ -17,21 +16,12 @@ func init() {
codegen.RegisterPlugin("model", "gen", nil, Generate)
}

// Generate produces the design JSON representation inside the top level gen
// directory.
// Generate adds the design's Structurizr JSON representation, gen/structurizr/
// model.json, to files. Goa evaluates the design before it calls plugins, so
// Generate reads the evaluated model and neither runs the DSL again nor touches
// the file system: rendering the returned file replaces any previous one.
func Generate(_ string, _ []eval.Root, files []*codegen.File) ([]*codegen.File, error) {
err := eval.RunDSL()
if err != nil {
return nil, err
}

// Generate structurizr JSON
path := filepath.Join(codegen.Gendir, "structurizr", "model.json")
if _, err := os.Stat(path); !os.IsNotExist(err) {
if err := os.Remove(path); err != nil {
return nil, err
}
}
section := &codegen.SectionTemplate{
Name: "model",
FuncMap: template.FuncMap{"toJSON": toJSON},
Expand Down
88 changes: 88 additions & 0 deletions plugin/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package plugin_test

import (
"bytes"
"encoding/json"
"path/filepath"
"testing"

"goa.design/goa/v3/codegen"
"goa.design/goa/v3/eval"
_ "goa.design/model/examples/big_bank_plc/model"
"goa.design/model/plugin"
)

// TestGenerateDescribesTheEvaluatedDesignOnce runs the design once, as goa gen
// does before calling plugins, and checks that the generated Structurizr
// workspace lists every view and every identified element once.
func TestGenerateDescribesTheEvaluatedDesignOnce(t *testing.T) {
if err := eval.RunDSL(); err != nil {
t.Fatal(err)
}
files, err := plugin.Generate("", nil, nil)
if err != nil {
t.Fatal(err)
}
if len(files) != 1 {
t.Fatalf("got %d files, want 1", len(files))
}
if want := filepath.Join(codegen.Gendir, "structurizr", "model.json"); files[0].Path != want {
t.Errorf("got path %q, want %q", files[0].Path, want)
}
var buf bytes.Buffer
for _, s := range files[0].SectionTemplates {
if err := s.Write(&buf); err != nil {
t.Fatal(err)
}
}
var workspace struct {
Model json.RawMessage
Views map[string]json.RawMessage
}
if err := json.Unmarshal(buf.Bytes(), &workspace); err != nil {
t.Fatal(err)
}
for kind, raw := range workspace.Views {
var views []struct{ Key string }
if err := json.Unmarshal(raw, &views); err != nil {
continue // not a list of views, such as the view configuration
}
seen := map[string]bool{}
for _, v := range views {
if seen[v.Key] {
t.Errorf("%s lists view %q more than once", kind, v.Key)
}
seen[v.Key] = true
}
}
var model any
if err := json.Unmarshal(workspace.Model, &model); err != nil {
t.Fatal(err)
}
checkUniqueIDs(t, "model", model)
}

// checkUniqueIDs reports any list under v holding two objects with the same
// "id".
func checkUniqueIDs(t *testing.T, path string, v any) {
t.Helper()
switch x := v.(type) {
case map[string]any:
for k, child := range x {
checkUniqueIDs(t, path+"."+k, child)
}
case []any:
seen := map[any]bool{}
for _, e := range x {
if obj, ok := e.(map[string]any); ok {
if id, ok := obj["id"]; ok {
if seen[id] {
t.Errorf("%s lists id %v more than once", path, id)
}
seen[id] = true
}
}
checkUniqueIDs(t, path+"[]", e)
}
}
}
Loading