add check for sequence diagram connection error

This commit is contained in:
Alexander Wang 2022-12-03 00:06:59 -08:00
parent c4dc93faee
commit 2b0f2d394f
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 38 additions and 0 deletions

View file

@ -1520,6 +1520,18 @@ dst.id <-> src.dst_id
assert.String(t, "sequence_diagram", g.Root.Attributes.Shape.Value)
},
},
{
name: "leaky_sequence",
text: `x: {
shape: sequence_diagram
a
}
b -> x.a
`,
expErr: `d2/testdata/d2compiler/TestCompile/leaky_sequence.d2:5:1: connections within sequence diagrams can connect only to other objects within the same sequence diagram
`,
},
{
name: "root_direction",

View file

@ -711,6 +711,16 @@ func (e *Edge) AbsID() string {
return fmt.Sprintf("%s(%s %s %s)[%d]", commonKey, strings.Join(srcIDA, "."), e.ArrowString(), strings.Join(dstIDA, "."), e.Index)
}
func (obj *Object) outerSequenceDiagram() *Object {
for obj != nil {
obj = obj.Parent
if obj.IsSequenceDiagram() {
return obj
}
}
return nil
}
func (obj *Object) Connect(srcID, dstID []string, srcArrow, dstArrow bool, label string) (*Edge, error) {
srcObj, srcID, err := ResolveUnderscoreKey(srcID, obj)
if err != nil {
@ -732,6 +742,10 @@ func (obj *Object) Connect(srcID, dstID []string, srcArrow, dstArrow bool, label
src := srcObj.EnsureChild(srcID)
dst := dstObj.EnsureChild(dstID)
if src.outerSequenceDiagram() != dst.outerSequenceDiagram() {
return nil, errors.New("connections within sequence diagrams can connect only to other objects within the same sequence diagram")
}
edge := &Edge{
Attributes: Attributes{
Label: Scalar{

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/leaky_sequence.d2,4:0:36-4:8:44",
"errmsg": "d2/testdata/d2compiler/TestCompile/leaky_sequence.d2:5:1: connections within sequence diagrams can only connect to other objects within the same sequence diagram"
}
]
}
}