Skip to content

Commit

Permalink
Add custom style shapes to view (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofreczek authored Dec 3, 2020
1 parent 1e292a7 commit 6b56dc1
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ v := view.NewView().
view.NewComponentStyle("TAG").
WithBackgroundColor(color.White).
WithFontColor(color.Black).
WithBorderColor(color.Black).
WithShape("database").
Build(),
).
WithTag("TAG").
Expand All @@ -158,6 +160,7 @@ view:
background_color: ffffffff
font_color: 000000ff
border_color: 000000ff
shape: database
tags:
- TAG
```
Expand All @@ -183,4 +186,3 @@ The best results and experience in using the library will be ensured by enforcin

## Full code documentation
https://pkg.go.dev/github.com/krzysztofreczek/go-structurizr

13 changes: 11 additions & 2 deletions pkg/view/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (v view) render(s model.Structure) string {
sb.WriteString(buildSkinParamDefault())

for _, s := range v.componentStyles {
sb.WriteString(buildSkinParamRectangle(s.id, s.backgroundColor, s.fontColor, s.borderColor))
sb.WriteString(buildSkinParamShape(s.id, s.backgroundColor, s.fontColor, s.borderColor, s.shape))
}

excludedComponentIds := map[string]struct{}{}
Expand All @@ -39,7 +39,16 @@ func (v view) render(s model.Structure) string {
if excluded {
continue
}
sb.WriteString(buildComponent(c))

shape := defaultShape
if len(c.Tags) > 0 {
s, exists := v.componentStyles[c.Tags[0]]
if exists {
shape = s.shape
}
}

sb.WriteString(buildComponent(c, shape))
}

for src, to := range s.Relations {
Expand Down
15 changes: 10 additions & 5 deletions pkg/view/snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ skinparam {
hide stereotype
top to bottom direction
`
snippetSkinParamRectangle = `
skinparam rectangle<<{{rec_name}}>> {
snippetSkinParamShape = `
skinparam {{shape}}<<{{rec_name}}>> {
BackgroundColor {{background_color_hash}}
FontColor {{font_color_hash}}
BorderColor {{border_color_hash}}
}
`
snippetComponent = `
rectangle "=={{component_name}}\n<size:10>[{{component_kind}}{{component_technology}}]</size>\n\n{{component_desc}}" <<{{rec_name}}>> as {{component_id}}`
{{shape}} "=={{component_name}}\n<size:10>[{{component_kind}}{{component_technology}}]</size>\n\n{{component_desc}}" <<{{rec_name}}>> as {{component_id}}`
snippetComponentConnection = `
{{component_id_from}} .[{{line_color_hash}}].> {{component_id_to}} : ""`

Expand All @@ -52,6 +52,7 @@ rectangle "=={{component_name}}\n<size:10>[{{component_kind}}{{component_technol
paramFontColor = "{{font_color_hash}}"
paramBorderColor = "{{border_color_hash}}"
paramLineColor = "{{line_color_hash}}"
paramShape = "{{shape}}"
)

func buildUMLHead() string {
Expand All @@ -74,24 +75,28 @@ func buildSkinParamDefault() string {
return snippetSkinParamDefault
}

func buildSkinParamRectangle(
func buildSkinParamShape(
name string,
backgroundColor color.Color,
fontColor color.Color,
borderColor color.Color,
shape string,
) string {
s := snippetSkinParamRectangle
s := snippetSkinParamShape
s = strings.Replace(s, paramRectangleName, name, -1)
s = strings.Replace(s, paramBackgroundColor, toHex(backgroundColor), -1)
s = strings.Replace(s, paramFontColor, toHex(fontColor), -1)
s = strings.Replace(s, paramBorderColor, toHex(borderColor), -1)
s = strings.Replace(s, paramShape, shape, -1)
return s
}

func buildComponent(
c model.Component,
shape string,
) string {
s := snippetComponent
s = strings.Replace(s, paramShape, shape, -1)
s = strings.Replace(s, paramComponentID, c.ID, -1)
s = strings.Replace(s, paramComponentName, c.Name, -1)
s = strings.Replace(s, paramComponentKind, c.Kind, -1)
Expand Down
39 changes: 33 additions & 6 deletions pkg/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,36 @@ type ComponentStyle struct {
backgroundColor color.Color
fontColor color.Color
borderColor color.Color
shape string
}

func newComponentStyle(
id string,
backgroundColor color.Color,
fontColor color.Color,
borderColor color.Color,
shape string,
) ComponentStyle {
return ComponentStyle{
id: id,
backgroundColor: backgroundColor,
fontColor: fontColor,
borderColor: borderColor,
shape: shape,
}
}

const defaultShape = "rectangle"

func newDefaultComponentStyle(
id string,
) ComponentStyle {
return ComponentStyle{
id: id,
backgroundColor: color.White,
fontColor: color.Black,
borderColor: color.Black,
shape: defaultShape,
}
}

Expand All @@ -170,13 +187,17 @@ func newComponentStyle(
// WithBackgroundColor sets background color.
// WithFontColor sets font color.
// WithBorderColor sets border color
// WithShape sets component shape that corresponds to plantUML
// shapes (rectangle, component, database, etc.).
// If shape is not provided, it is defaulted to rectangle.
//
// Build returns default ComponentStyle implementation constructed from
// the provided configuration.
type ComponentStyleBuilder interface {
WithBackgroundColor(c color.Color) ComponentStyleBuilder
WithFontColor(c color.Color) ComponentStyleBuilder
WithBorderColor(c color.Color) ComponentStyleBuilder
WithShape(s string) ComponentStyleBuilder

Build() ComponentStyle
}
Expand All @@ -188,12 +209,7 @@ type componentStyleBuilder struct {
// NewView returns ComponentStyleBuilder with provided id.
func NewComponentStyle(id string) ComponentStyleBuilder {
return &componentStyleBuilder{
ComponentStyle: ComponentStyle{
id: id,
backgroundColor: color.White,
fontColor: color.Black,
borderColor: color.Black,
},
ComponentStyle: newDefaultComponentStyle(id),
}
}

Expand Down Expand Up @@ -221,6 +237,16 @@ func (b *componentStyleBuilder) WithBorderColor(c color.Color) ComponentStyleBui
return b
}

// WithShape sets component shape that corresponds to plantUML
// shapes (rectangle, component, database, etc.).
// If shape is not provided, it is defaulted to rectangle.
func (b *componentStyleBuilder) WithShape(s string) ComponentStyleBuilder {
if s != "" {
b.shape = s
}
return b
}

// Build returns default ComponentStyle implementation constructed from
// the provided configuration.
func (b componentStyleBuilder) Build() ComponentStyle {
Expand All @@ -229,5 +255,6 @@ func (b componentStyleBuilder) Build() ComponentStyle {
b.backgroundColor,
b.fontColor,
b.borderColor,
b.shape,
)
}
43 changes: 43 additions & 0 deletions pkg/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,46 @@ ID_1 .[#000000].> ID_2 : ""
`
require.NotContains(t, outString, expectedContent)
}

func TestNewView_with_component_of_custom_style_shape(t *testing.T) {
s := model.NewStructure()
s.Components = map[string]model.Component{
"ID_1": {
ID: "ID_1",
Kind: "component",
Name: "test.Component",
Description: "description",
Technology: "technology",
Tags: []string{"DB"},
},
}

out := bytes.Buffer{}

style := view.NewComponentStyle("DB").
WithBackgroundColor(color.White).
WithFontColor(color.Black).
WithBorderColor(color.White).
WithShape("database").
Build()
v := view.NewView().
WithComponentStyle(style).
Build()
err := v.RenderStructureTo(s, &out)
require.NoError(t, err)

outString := string(out.Bytes())

expectedContent := `
skinparam database<<DB>> {
BackgroundColor #ffffff
FontColor #000000
BorderColor #ffffff
}`
require.Contains(t, outString, expectedContent)

expectedContent = `
database "==test.Component\n<size:10>[component:technology]</size>\n\ndescription" <<DB>> as ID_1
`
require.Contains(t, outString, expectedContent)
}
4 changes: 4 additions & 0 deletions pkg/view/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func toView(c yaml.Config) (View, error) {
style.WithBorderColor(col)
}

if s.Shape != "" {
style.WithShape(s.Shape)
}

v.WithComponentStyle(style.Build())
}

Expand Down
1 change: 1 addition & 0 deletions pkg/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ConfigViewStyle struct {
BackgroundColor string `yaml:"background_color"`
FontColor string `yaml:"font_color"`
BorderColor string `yaml:"border_color"`
Shape string `yaml:"shape"`
}

// LoadFromFile loads Config from YAML file.
Expand Down

0 comments on commit 6b56dc1

Please sign in to comment.