design

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2019 License: MIT Imports: 5 Imported by: 0

README

Build Status codecov Maintainability

go-design - package for writing software design diagrams

  • Cross platform
  • No external dependencies
  • SVG output

Program your diagrams and refactoring automatically updates them. Take a look at the below examples and then browse the showcase of golang standard packages.

Sequence diagram

var (
    d   = design.NewSequenceDiagram()
    cli = d.AddStruct(app.Client{})
    srv = d.AddStruct(app.Server{})
    db  = d.AddStruct(sql.DB{})
)
d.Link(cli, srv, "connect()")
d.Link(srv, db, "SELECT").Class = "highlight"
d.Link(db, srv, "Rows")
d.Link(srv, srv, "Transform to view model").Class = "highlight"
d.Link(srv, cli, "Send HTML")

Activity diagram

Rendered by example_test.go/ExampleActivityDiagram

Class diagram

Class diagrams show relations between structs and interfaces. Reflection includes fields and methods.

This diagram is rendered by example_test.go/ExampleClassDiagram

Generic diagram

It should be easy to just add any extra shapes to any diagram when explaining a design. This diagram is rendered by example_test.go/ExampleDiagram

Grid layout

Simplifying placing shapes in a grid layout aligning different sizes of shapes.

TODO

  • Labeled arrows
  • Link to optional godoc service
  • More shapes

WIP - major rewrites still going on

Documentation

Overview

Package go-design provides svg diagram creators

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActivityDiagram added in v0.6.0

type ActivityDiagram struct {
	Diagram
}
Example
package main

import (
	design "github.com/gregoryv/go-design"
	"github.com/gregoryv/go-design/shape"
)

func main() {
	var (
		d       = design.NewActivityDiagram()
		start   = shape.NewDot(10)
		push    = shape.NewState("Push commit")
		dec     = shape.NewDecision()
		run     = shape.NewState("Run git hook")
		deploy  = shape.NewState("Deploy")
		endOk   = shape.NewExitDot()
		endFail = shape.NewExitDot()
	)
	d.Place(start).At(80, 20)
	d.Place(push, run, dec, deploy, endOk).Below(start, 40)
	d.Place(endFail).RightOf(dec, 80)

	d.VAlignCenter(start, push, run, dec, deploy, endOk)
	d.HAlignCenter(dec, endFail)

	d.LinkAll(start, push, run, dec, deploy, endOk)
	d.Link(dec, endFail, "Tests failed")

	d.SaveAs("img/activity_diagram.svg")
}
Output:

func NewActivityDiagram added in v0.6.0

func NewActivityDiagram() *ActivityDiagram

type ClassDiagram

type ClassDiagram struct {
	Diagram
	// contains filtered or unexported fields
}
Example
package main

import (
	design "github.com/gregoryv/go-design"
	"github.com/gregoryv/go-design/shape"
)

func main() {
	var (
		d        = design.NewClassDiagram()
		record   = d.Struct(shape.Record{})
		arrow    = d.Struct(shape.Arrow{})
		line     = d.Struct(shape.Line{})
		circle   = d.Struct(shape.Circle{})
		diaarrow = d.Struct(shape.Diamond{})
		triangle = d.Struct(shape.Triangle{})
		shapE    = d.Interface((*shape.Shape)(nil))
	)
	d.HideRealizations()

	var (
		fnt      = d.Struct(shape.Font{})
		style    = d.Struct(shape.Style{})
		seqdia   = d.Struct(design.SequenceDiagram{})
		classdia = d.Struct(design.ClassDiagram{})
		dia      = d.Struct(design.Diagram{})
		aligner  = d.Struct(shape.Aligner{})
		adj      = d.Struct(shape.Adjuster{})
	)
	d.HideRealizations()

	d.Place(shapE).At(220, 20)
	d.Place(record).At(20, 120)
	d.Place(line).Below(shapE, 90)
	d.VAlignCenter(shapE, line)

	d.Place(arrow).RightOf(line, 90)
	d.Place(circle).RightOf(shapE, 280)
	d.Place(diaarrow).Below(circle)
	d.Place(triangle).Below(diaarrow)
	d.HAlignBottom(record, arrow, line)

	d.Place(fnt).Below(record, 120)
	d.Place(style).RightOf(fnt, 90)
	d.VAlignCenter(shapE, line, style)
	d.VAlignCenter(record, fnt)

	d.Place(dia).RightOf(style, 90)
	d.Place(aligner).RightOf(dia, 80)
	d.HAlignCenter(fnt, style, dia, aligner)

	d.Place(adj).Below(fnt, 70)
	d.Place(seqdia).Below(aligner, 90)
	d.Place(classdia).Below(dia, 90)
	d.VAlignCenter(dia, classdia)
	d.HAlignBottom(classdia, seqdia)

	d.SetCaption("Figure 1. Class diagram of design and design.shape packages")
	d.SaveAs("img/class_example.svg")
}
Output:

func NewClassDiagram

func NewClassDiagram() *ClassDiagram

NewClassDiagram returns a diagram representing structs and interfaces. Relations are reflected from the types and drawn as arrows.

func (*ClassDiagram) HideRealizations

func (d *ClassDiagram) HideRealizations()

HideRealizations hides all methods of structs that implement a visible interface.

func (*ClassDiagram) Interface

func (d *ClassDiagram) Interface(obj interface{}) VRecord

func (*ClassDiagram) SaveAs

func (d *ClassDiagram) SaveAs(filename string) error

SaveAs saves the diagram to filename as SVG

func (*ClassDiagram) Struct

func (d *ClassDiagram) Struct(obj interface{}) VRecord

func (*ClassDiagram) WriteSvg

func (d *ClassDiagram) WriteSvg(w io.Writer) error

WriteSvg renders the diagram as SVG to the given writer.

type Diagram

type Diagram struct {
	shape.Svg
	shape.Aligner
	shape.Style

	Caption *shape.Label
}

Diagram is a generic SVG image with box related styling

Example
package main

import (
	design "github.com/gregoryv/go-design"
	"github.com/gregoryv/go-design/shape"
)

func main() {
	var (
		record     = shape.NewRecord("Record")
		x, y       = 130, 80
		q1arrow    = shape.NewArrow(x, y, x+50, y-10)
		q2arrow    = shape.NewArrow(x, y, x-30, y-10)
		q3arrow    = shape.NewArrow(x, y, x-50, y+20)
		q4arrow    = shape.NewArrow(x, y, x+40, y+20)
		rightarrow = shape.NewArrow(x, y, x+90, y)
		leftarrow  = shape.NewArrow(x, y, x-50, y)
		uparrow    = shape.NewArrow(x, y, x, y-40)
		downarrow  = shape.NewArrow(x, y, x, y+40)
		label      = shape.NewLabel("Label")
		withtail   = shape.NewArrow(20, 100, 150, 100)
		diaarrow   = shape.NewArrow(20, 120, 150, 120)
		note       = shape.NewNote(`Notes support
multilines`)
		comp   = shape.NewComponent("database")
		srv    = shape.NewComponent("service")
		circle = shape.NewCircle(10)
		dot    = shape.NewDot(10)
		exit   = shape.NewExitDot()
		rect   = shape.NewRect("Rect")
		state  = shape.NewState("Waiting for go routine")
		d      = design.NewDiagram()
	)
	d.Place(record).At(10, 30)
	for _, arrow := range []*shape.Arrow{
		q1arrow, q2arrow, q3arrow, q4arrow,
		rightarrow, leftarrow,
		uparrow, downarrow,
	} {
		d.Place(arrow)
	}
	d.Place(label).RightOf(record, 150)
	withtail.Tail = shape.NewCircle(3)
	d.Place(withtail).At(20, 150)
	diaarrow.Tail = shape.NewDiamond()
	d.Place(diaarrow).Below(withtail)
	d.Place(note).Below(diaarrow)
	d.Place(circle).Below(note)
	d.Place(dot).RightOf(circle)
	d.Place(exit).RightOf(dot)
	d.Place(comp).RightOf(diaarrow)
	d.Place(rect).Below(circle)
	d.Place(state).RightOf(rect)
	d.Place(srv).Below(comp)
	d.VAlignCenter(comp, srv)
	d.LinkAll(srv, comp)
	d.SaveAs("img/diagram_example.svg")
}
Output:

func NewDiagram

func NewDiagram() Diagram

NewDiagram returns a diagram with present font and padding values.

TODO: size and padding affects eg. records, but is related to the styling

func (*Diagram) AdaptSize

func (diagram *Diagram) AdaptSize() (int, int)

AdaptSize adapts the diagram size to the shapes inside it so all are visible. Returns the new width and height

func (diagram *Diagram) Link(from, to shape.Shape, txt string)

func (*Diagram) LinkAll added in v0.6.0

func (diagram *Diagram) LinkAll(s ...shape.Shape)

LinkAll places an arrow between the shapes, s0->s1->...->sn

func (*Diagram) Place

func (diagram *Diagram) Place(s ...shape.Shape) *shape.Adjuster

Place adds the shape to the diagram returning an adjuster for positioning.

func (*Diagram) PlaceGrid added in v0.6.0

func (diagram *Diagram) PlaceGrid(cols, X, Y int, s ...shape.Shape)

PlaceGrid place all the shapes into a grid starting at X,Y position. Row height is adapted to heighest element.

func (*Diagram) SaveAs

func (d *Diagram) SaveAs(filename string) error

SaveAs saves the diagram to filename as SVG

func (*Diagram) SetCaption

func (d *Diagram) SetCaption(txt string)

func (*Diagram) SetHeight

func (d *Diagram) SetHeight(h int)

SetHeight sets a fixed height in pixels.

func (*Diagram) SetWidth

func (d *Diagram) SetWidth(w int)

SetWidth sets a fixe width in pixels.

func (*Diagram) WriteSvg

func (d *Diagram) WriteSvg(w io.Writer) error
type Link struct {
	Class     string
	TextClass string
	// contains filtered or unexported fields
}

Link represents an arrow in a sequence diagram

type Relation

type Relation struct {
	// contains filtered or unexported fields
}

Relation defines a relation between two records

type SequenceDiagram

type SequenceDiagram struct {
	Diagram
	ColWidth int
	VMargin  int // top margin for each horizontal lane
	// contains filtered or unexported fields
}

SequenceDiagram defines columns and links between columns.

Example
package main

import (
	design "github.com/gregoryv/go-design"
)

func main() {
	var (
		d   = design.NewSequenceDiagram()
		cli = "Client"
		srv = "Server"
		db  = "Database"
	)
	d.AddColumns(cli, srv, db)
	d.Link(cli, srv, "connect()")
	d.Link(srv, db, "SELECT").Class = "highlight"
	d.Link(db, srv, "Rows")
	d.Link(srv, srv, "Transform to view model").Class = "highlight"
	d.Link(srv, cli, "Send HTML")
	d.SaveAs("img/sequence_diagram.svg")
}
Output:

func NewSequenceDiagram

func NewSequenceDiagram() *SequenceDiagram

NewSequenceDiagram returns a sequence diagram with default column width.

func (*SequenceDiagram) AddColumns

func (d *SequenceDiagram) AddColumns(names ...string)

func (*SequenceDiagram) AddStruct added in v0.4.0

func (d *SequenceDiagram) AddStruct(obj interface{}) string
func (d *SequenceDiagram) ClearLinks()

func (*SequenceDiagram) Height

func (d *SequenceDiagram) Height() int

Height returns the total height of the diagram

func (d *SequenceDiagram) Link(from, to, text string) *Link

func (*SequenceDiagram) SaveAs

func (d *SequenceDiagram) SaveAs(filename string) error

func (*SequenceDiagram) Width

func (d *SequenceDiagram) Width() int

Width returns the total width of the diagram

func (*SequenceDiagram) WriteSvg

func (d *SequenceDiagram) WriteSvg(w io.Writer) error

WriteSvg renders the diagram as SVG to the given writer.

type SvgWriter

type SvgWriter interface {
	WriteSvg(io.Writer) error
}

type VRecord

type VRecord struct {
	*shape.Record
	// contains filtered or unexported fields
}

VRecord represents a type struct or interface as a record shape.

func NewInterface

func NewInterface(obj interface{}) VRecord

NewInterface returns a VRecord of the given object, panics if not interface.

func NewStruct

func NewStruct(obj interface{}) VRecord

NewStruct returns a VRecord of the given object, panics if not struct.

func (*VRecord) TitleOnly

func (vr *VRecord) TitleOnly()

Directories

Path Synopsis
internal
app
Package shape provides various SVG shapes
Package shape provides various SVG shapes
Package xy provides xy Position
Package xy provides xy Position

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL