Skip to content

Commit

Permalink
Leverage "Component" to model service endpoints (#538)
Browse files Browse the repository at this point in the history
* Remove Endpoint/Calls in favor of Component

Components can be used to represent endpoints:

```go
		Container("Web Application", "Delivers content to users.", func() {
			Component("Dashboard Endpoint", "Serve dashboard content.", func() {
				Tag("endpoint")
			})
			Uses("Application Database", "Reads from and writes to", "MySQL", Synchronous)
		})
```
`Uses` can target the endpoint alleviating the need for `Calls`:
```go
		Container("Load Balancer", "Distributes requests across the Web Application instances.", func() {
			Uses("Web Application/Dashboard Endpoint", "Routes requests to", "HTTPS", Synchronous)
		})
```

* Cleanup

* Remove unused code
  • Loading branch information
raphael committed Oct 14, 2023
1 parent a71f459 commit e9f3cc2
Show file tree
Hide file tree
Showing 35 changed files with 327 additions and 168 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ GO_FILES=$(shell find . -type f -name '*.go')
# Standard dependencies are installed via go get
DEPEND=\
golang.org/x/tools/cmd/goimports@latest \
honnef.co/go/tools/cmd/staticcheck@latest \
github.com/golangci/golangci-lint/cmd/golangci-lint@latest \
github.com/mjibson/esc@latest

all: lint check-generated test
Expand All @@ -41,8 +41,8 @@ ifneq ($(GOOS),windows)
@if [ "`goimports -l $(GO_FILES) | tee /dev/stderr`" ]; then \
echo "^ - Repo contains improperly formatted go files" && echo && exit 1; \
fi
@if [ "`staticcheck ./... | tee /dev/stderr`" ]; then \
echo "^ - staticcheck errors!" && echo && exit 1; \
@if [ "`golangci-lint run ./... | tee /dev/stderr`" ]; then \
echo "^ - golangci-lint errors!" && echo && exit 1; \
fi
endif

Expand All @@ -52,12 +52,12 @@ check-generated: generate
fi

test:
env GO111MODULE=on go test ./...
go test ./...

release:
# First make sure all is clean
@git diff-index --quiet HEAD
@go mod tidy --compat=1.17
@go mod tidy

# Bump version number
@sed 's/Major = .*/Major = $(MAJOR)/' pkg/version.go > _tmp && mv _tmp pkg/version.go
Expand Down
14 changes: 10 additions & 4 deletions cmd/mdl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,19 @@ func main() {
switch cmd {
case "gen":
addGlobals(genset)
genset.Parse(os.Args[idx:])
if err := genset.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}
case "serve":
addGlobals(svrset)
svrset.Parse(os.Args[idx:])
if err := svrset.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}
default:
addGlobals(gset)
gset.Parse(os.Args[idx:])
if err := gset.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}
}

if *h || *help {
Expand Down Expand Up @@ -154,7 +160,7 @@ func printUsage(fss ...*flag.FlagSet) {
}
}

func fail(format string, args ...interface{}) {
func fail(format string, args ...any) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}
16 changes: 10 additions & 6 deletions cmd/mdl/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type (
}

// Layout is position info saved for one view (diagram)
Layout = map[string]interface{}
Layout = map[string]any

// Layouts is a map from view key to the view Layout
Layouts = map[string]Layout
Expand Down Expand Up @@ -81,7 +81,7 @@ func (s *Server) Serve(outDir string, devmode bool, port int) error {
http.HandleFunc("/data/save", func(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {
http.Error(w, "Param id is missing", http.StatusBadRequest)
handleError(w, fmt.Errorf("missing id"))
return
}

Expand All @@ -91,9 +91,7 @@ func (s *Server) Serve(outDir string, devmode bool, port int) error {
svgFile := path.Join(outDir, id+".svg")
f, err := os.Create(svgFile)
if err != nil {
msg := fmt.Sprintf("Saving failed, can't write to %s: %s!\n", svgFile, err)
fmt.Println(msg)
http.Error(w, msg, http.StatusInternalServerError)
handleError(w, err)
return
}
defer func() { _ = f.Close() }()
Expand Down Expand Up @@ -123,6 +121,12 @@ func (s *Server) SetDesign(d *mdl.Design) {
s.design = b
}

// handleError writes the given error to stderr and http.Error.
func handleError(w http.ResponseWriter, err error) {
fmt.Fprintln(os.Stderr, err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
}

// loadLayouts lists out directory and reads layout info from SVG files
// for backwards compatibility, fallback to layout.json
func loadLayouts(dir string) ([]byte, error) {
Expand Down Expand Up @@ -164,7 +168,7 @@ func loadLayouts(dir string) ([]byte, error) {
end := bytes.Index(b, endMark)
b = b[begin:end]

var l Layout = make(map[string]interface{})
var l Layout = make(map[string]any)
err = json.Unmarshal(b, &l)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdl/webapp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cmd/stz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func main() {
}
}
done:
fs.Parse(os.Args[idx:])
if err := fs.Parse(os.Args[idx:]); err != nil {
fail(err.Error())
}

pathOrDefault := func(p string) string {
if p == "" {
Expand Down Expand Up @@ -208,7 +210,7 @@ func put(path, wid, key, secret string, debug bool) error {
return c.Put(wid, local)
}

func fail(format string, args ...interface{}) {
func fail(format string, args ...any) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}
Expand Down
6 changes: 3 additions & 3 deletions dsl/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func DeploymentEnvironment(name string, dsl func()) {
// })
// })
// })
func DeploymentNode(name string, args ...interface{}) *expr.DeploymentNode {
func DeploymentNode(name string, args ...any) *expr.DeploymentNode {
if strings.Contains(name, "/") {
eval.ReportError("DeploymentNode: name cannot include slashes")
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func DeploymentNode(name string, args ...interface{}) *expr.DeploymentNode {
// })
// })
// })
func InfrastructureNode(name string, args ...interface{}) *expr.InfrastructureNode {
func InfrastructureNode(name string, args ...any) *expr.InfrastructureNode {
d, ok := eval.Current().(*expr.DeploymentNode)
if !ok {
eval.IncompatibleDSL()
Expand Down Expand Up @@ -241,7 +241,7 @@ func InfrastructureNode(name string, args ...interface{}) *expr.InfrastructureNo
// })
// })
// })
func ContainerInstance(container interface{}, dsl ...func()) *expr.ContainerInstance {
func ContainerInstance(container any, dsl ...func()) *expr.ContainerInstance {
d, ok := eval.Current().(*expr.DeploymentNode)
if !ok {
eval.IncompatibleDSL()
Expand Down
2 changes: 1 addition & 1 deletion dsl/design.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
// var _ = Design("My Design", "A great architecture.", func() {
// SoftwareSystem("My Software System")
// })
func Design(args ...interface{}) *expr.Design {
func Design(args ...any) *expr.Design {
_, ok := eval.Current().(eval.TopExpr)
if !ok {
eval.IncompatibleDSL()
Expand Down
2 changes: 1 addition & 1 deletion dsl/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ general shape of the DSL is:
│ ├── Prop │ ├── AutoLayout
│ ├── Uses │ ├── AnimationStep
│ ├── Delivers │ ├── PaperSize
│ └── Container │ └── EnterpriseBoundaryVisible
│ └── Container │ └── EnterpriseBoundaryVisible
│ ├── Tag ├── SystemContextView
│ ├── URL │ └── ... (same as SystemLandsapeView)
│ ├── Prop ├── ContainerView
Expand Down
8 changes: 4 additions & 4 deletions dsl/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
// Delivers("Customer", "Delivers emails to", "SMTP", Synchronous)
// })
// })
func SoftwareSystem(name string, args ...interface{}) *expr.SoftwareSystem {
func SoftwareSystem(name string, args ...any) *expr.SoftwareSystem {
w, ok := eval.Current().(*expr.Design)
if !ok {
eval.IncompatibleDSL()
Expand Down Expand Up @@ -112,7 +112,7 @@ func SoftwareSystem(name string, args ...interface{}) *expr.SoftwareSystem {
// })
// })
// })
func Container(args ...interface{}) *expr.Container {
func Container(args ...any) *expr.Container {
system, ok := eval.Current().(*expr.SoftwareSystem)
if !ok {
eval.IncompatibleDSL()
Expand Down Expand Up @@ -209,7 +209,7 @@ func Container(args ...interface{}) *expr.Container {
// })
// })
// })
func Component(name string, args ...interface{}) *expr.Component {
func Component(name string, args ...any) *expr.Component {
container, ok := eval.Current().(*expr.Container)
if !ok {
eval.IncompatibleDSL()
Expand Down Expand Up @@ -243,7 +243,7 @@ func Component(name string, args ...interface{}) *expr.Component {
// func()
// "[description]", func()
// "[description]", "[technology]", func()
func parseElementArgs(args ...interface{}) (description, technology string, dsl func(), err error) {
func parseElementArgs(args ...any) (description, technology string, dsl func(), err error) {
if len(args) == 0 {
return
}
Expand Down
2 changes: 1 addition & 1 deletion dsl/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
// InteractsWith(Employee)
// })
// })
func Person(name string, args ...interface{}) *expr.Person {
func Person(name string, args ...any) *expr.Person {
w, ok := eval.Current().(*expr.Design)
if !ok {
eval.IncompatibleDSL()
Expand Down
8 changes: 4 additions & 4 deletions dsl/relationship.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const (
// InteractsWith("Customer", "Sends invoices to", Synchronous)
// })
// })
func Uses(element interface{}, description string, args ...interface{}) {
func Uses(element any, description string, args ...any) {
var src *expr.Element
switch e := eval.Current().(type) {
case *expr.Person:
Expand Down Expand Up @@ -142,7 +142,7 @@ func Uses(element interface{}, description string, args ...interface{}) {
// InteractsWith(Employee, "Sends requests to", "email")
// })
// })
func InteractsWith(person interface{}, description string, args ...interface{}) {
func InteractsWith(person any, description string, args ...any) {
src, ok := eval.Current().(*expr.Person)
if !ok {
eval.IncompatibleDSL()
Expand Down Expand Up @@ -205,7 +205,7 @@ func InteractsWith(person interface{}, description string, args ...interface{})
// Delivers(Customer, "Sends requests to", "email")
// })
// })
func Delivers(person interface{}, description string, args ...interface{}) {
func Delivers(person any, description string, args ...any) {
var src *expr.Element
switch e := eval.Current().(type) {
case *expr.SoftwareSystem:
Expand Down Expand Up @@ -258,7 +258,7 @@ func Description(desc string) {

// uses adds a relationship between the given source and destination. The caller
// must make sure that the relationship is valid.
func uses(src *expr.Element, dest interface{}, desc string, args ...interface{}) error {
func uses(src *expr.Element, dest any, desc string, args ...any) error {
var (
technology string
style InteractionStyleKind
Expand Down
Loading

0 comments on commit e9f3cc2

Please sign in to comment.