Skip to content

Commit

Permalink
allows alias in plantuml tuple data structure (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
nofun97 authored Jul 3, 2020
1 parent c787de6 commit eae73f0
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.13
replace github.com/spf13/afero => github.com/anz-bank/afero v1.2.3

require (
aqwari.net/xml v0.0.0-20200323224746-73105c9af915
aqwari.net/xml v0.0.0-20200619145941-6c62842e69c1
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38
github.com/alecthomas/chroma v0.7.3
github.com/alecthomas/colour v0.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum

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

24 changes: 22 additions & 2 deletions pkg/datamodeldiagram/datamodelview.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type EntityViewParam struct {
EntityColor string
EntityHeader string
EntityName string
EntityAlias string
IgnoredTypes map[string]struct{}
Types map[string]*sysl.Type
}
Expand Down Expand Up @@ -182,9 +183,28 @@ func (v *DataModelView) DrawTuple(
entity *sysl.Type_Tuple,
relationshipMap map[string]map[string]RelationshipParam,
) {
var alias, typeName string
if viewParam.EntityAlias == "" {
alias = viewParam.EntityName
} else {
alias = viewParam.EntityAlias
// add space for better formatting and allow empty space when alias == ""
typeName = " " + viewParam.EntityName
}
encEntity := v.UniqueVarForAppName(strings.Split(viewParam.EntityName, ".")...)
v.StringBuilder.WriteString(fmt.Sprintf("%s \"%s\" as %s %s(%s,%s)%s {\n", classString, viewParam.EntityName,
encEntity, entityLessThanArrow, viewParam.EntityHeader, viewParam.EntityColor, entityGreaterThanArrow))

// this will create a class header, with alias it will look like the following:
// class "AliasName" as _0 << (D,orchid) TypeName >> {
// Without the alias:
// class "TypeName" as _0 << (D,orchid) >> {
v.StringBuilder.WriteString(
fmt.Sprintf(
"%s \"%s\" as %s %s(%s,%s)%s%s {\n",
classString, alias,
encEntity, entityLessThanArrow, viewParam.EntityHeader,
viewParam.EntityColor, typeName, entityGreaterThanArrow,
),
)
var appName string
var relation string
var collectionString string
Expand Down
79 changes: 70 additions & 9 deletions pkg/datamodeldiagram/datamodelview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
"testing"

"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslwrapper"
"github.com/stretchr/testify/mock"

"github.com/stretchr/testify/assert"
Expand All @@ -18,22 +20,81 @@ func (mock *ClassLabelerMock) LabelClass(className string) string {
return args.String(0)
}

func TestDrawPrimitive(t *testing.T) {
func assertDraw(t *testing.T,
expected, className string,
drawFunc func(*DataModelView, map[string]map[string]RelationshipParam)) {
clMock := new(ClassLabelerMock)
clMock.On("LabelClass", "uuid").Return("test")
clMock.On("LabelClass", className).Return("test")

var stringBuilder strings.Builder
v := MakeDataModelView(clMock, nil, &stringBuilder, "title", "project")
viewParam := EntityViewParam{
EntityColor: "orchid",
EntityHeader: "D",
EntityName: "uuid",
}
relationshipMap := map[string]map[string]RelationshipParam{}
v.DrawPrimitive(viewParam, "INT", relationshipMap)
drawFunc(v, relationshipMap)
actual := v.StringBuilder.String()

expected := "class \"uuid\" as _0 << (D,orchid) int >> {\n" + "}\n"
assert.EqualValues(t, expected, actual, nil)
clMock.AssertExpectations(t)
}

func TestDrawPrimitive(t *testing.T) {
t.Parallel()
assertDraw(t,
"class \"uuid\" as _0 << (D,orchid) int >> {\n}\n",
"uuid",
func(v *DataModelView, relationshipMap map[string]map[string]RelationshipParam) {
v.DrawPrimitive(
EntityViewParam{
EntityColor: "orchid",
EntityHeader: "D",
EntityName: "uuid",
},
"INT",
relationshipMap,
)
},
)
}

func TestDrawTuple(t *testing.T) {
t.Parallel()
tuple := syslwrapper.MakeTuple(
map[string]*sysl.Type{"attr1": syslwrapper.MakePrimitive("string")},
).Type.(*sysl.Type_Tuple_).Tuple
assertDraw(t,
`class "aliasName" as _0 << (D,orchid) typeName >> {
+ attr1 : string
}
`,
"typeName",
func(v *DataModelView, relationshipMap map[string]map[string]RelationshipParam) {
v.DrawTuple(
EntityViewParam{
EntityColor: "orchid",
EntityHeader: "D",
EntityName: "typeName",
EntityAlias: "aliasName",
},
tuple, relationshipMap,
)
},
)

assertDraw(t,
`class "typeName" as _0 << (D,orchid) >> {
+ attr1 : string
}
`,
"typeName",
func(v *DataModelView, relationshipMap map[string]map[string]RelationshipParam) {
v.DrawTuple(
EntityViewParam{
EntityColor: "orchid",
EntityHeader: "D",
EntityName: "typeName",
EntityAlias: "",
},
tuple, relationshipMap,
)
},
)
}

0 comments on commit eae73f0

Please sign in to comment.