design

package module
v0.5.0 Latest Latest
Warning

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

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

README

Build Status codecov Maintainability

go-design - package for writing software design diagrams

  • Cross platform
  • No 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.

Class diagram

This diagram is rendered by example_test.go/ExampleClassDiagram

Sequence diagram

From example_test.go/ExampleSequenceDiagram

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

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 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")
}

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")
		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(0, 0)
	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(srv).Below(comp)
	d.VAlignCenter(comp, srv)
	d.Link(srv, comp)
	d.SaveAs("img/diagram_example.svg")
}

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)

Link places an arrow between the two shapes

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) 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 (
		cli = "Client"
		srv = "Server"
		db  = "Database"
		d   = design.NewSequenceDiagram()
	)
	d.AddColumns(cli, srv, db)
	d.Link(cli, srv, "connect()")
	d.Link(srv, db, "SELECT").Class = "highlight"
	d.Link(db, srv, "Rows")
	// Special link
	lnk := d.Link(srv, srv, "Transform to view model")
	lnk.Class = "highlight"
	d.Link(srv, cli, "Send HTML")
	d.SaveAs("img/sequence_example.svg")
}

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
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