Skip to content

Commit 3bf7b49

Browse files
authored
Merge pull request #2418 from alixander/unsuspend-label
`suspend` fixes
2 parents 0135cd1 + cbb54f0 commit 3bf7b49

File tree

7 files changed

+1182
-5
lines changed

7 files changed

+1182
-5
lines changed

d2compiler/compile_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -5617,6 +5617,44 @@ d -> d: "suspend"
56175617
assert.Equal(t, 1, len(g.Edges))
56185618
},
56195619
},
5620+
{
5621+
name: "unsuspend-edge-label",
5622+
run: func(t *testing.T) {
5623+
g, _ := assertCompile(t, `
5624+
a -> b: hello
5625+
c
5626+
**: suspend
5627+
(** -> **)[*]: suspend
5628+
5629+
(* -> *)[*]: unsuspend
5630+
`, ``)
5631+
assert.Equal(t, 2, len(g.Objects))
5632+
assert.Equal(t, 1, len(g.Edges))
5633+
assert.Equal(t, "hello", g.Edges[0].Label.Value)
5634+
},
5635+
},
5636+
{
5637+
name: "unsuspend-shape-label",
5638+
run: func(t *testing.T) {
5639+
g, _ := assertCompile(t, `
5640+
a: hello
5641+
*: suspend
5642+
*: unsuspend
5643+
`, ``)
5644+
assert.Equal(t, 1, len(g.Objects))
5645+
assert.Equal(t, "hello", g.Objects[0].Label.Value)
5646+
},
5647+
},
5648+
{
5649+
name: "suspend-shape",
5650+
run: func(t *testing.T) {
5651+
g, _ := assertCompile(t, `
5652+
a: hello
5653+
*: suspend
5654+
`, ``)
5655+
assert.Equal(t, 0, len(g.Objects))
5656+
},
5657+
},
56205658
{
56215659
name: "edge-glob-ampersand-filter/1",
56225660
run: func(t *testing.T) {

d2ir/compile.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -1275,10 +1275,26 @@ func (c *compiler) _compileEdges(refctx *RefContext) {
12751275

12761276
if refctx.Key.Primary.Suspension != nil || refctx.Key.Value.Suspension != nil {
12771277
if !c.lazyGlobBeingApplied {
1278+
var suspensionValue bool
12781279
if refctx.Key.Primary.Suspension != nil {
1279-
e.suspended = refctx.Key.Primary.Suspension.Value
1280+
suspensionValue = refctx.Key.Primary.Suspension.Value
12801281
} else {
1281-
e.suspended = refctx.Key.Value.Suspension.Value
1282+
suspensionValue = refctx.Key.Value.Suspension.Value
1283+
}
1284+
e.suspended = suspensionValue
1285+
1286+
// If we're unsuspending an edge, we should also unsuspend its src and dst objects
1287+
if !suspensionValue {
1288+
srcPath, dstPath := e.ID.SrcPath, e.ID.DstPath
1289+
srcObj := refctx.ScopeMap.GetField(srcPath...)
1290+
dstObj := refctx.ScopeMap.GetField(dstPath...)
1291+
1292+
if srcObj != nil {
1293+
srcObj.suspended = false
1294+
}
1295+
if dstObj != nil {
1296+
dstObj.suspended = false
1297+
}
12821298
}
12831299
}
12841300
}
@@ -1309,7 +1325,7 @@ func (c *compiler) _compileEdges(refctx *RefContext) {
13091325
}
13101326
c.compileField(e.Map_, refctx.Key.EdgeKey, refctx)
13111327
} else {
1312-
if refctx.Key.Primary.Unbox() != nil {
1328+
if refctx.Key.Primary.Unbox() != nil && refctx.Key.Primary.Suspension == nil {
13131329
if c.ignoreLazyGlob(e) {
13141330
return
13151331
}
@@ -1330,7 +1346,7 @@ func (c *compiler) _compileEdges(refctx *RefContext) {
13301346
c.mapRefContextStack = append(c.mapRefContextStack, refctx)
13311347
c.compileMap(e.Map_, refctx.Key.Value.Map, refctx.ScopeAST)
13321348
c.mapRefContextStack = c.mapRefContextStack[:len(c.mapRefContextStack)-1]
1333-
} else if refctx.Key.Value.ScalarBox().Unbox() != nil {
1349+
} else if refctx.Key.Value.ScalarBox().Unbox() != nil && refctx.Key.Value.Suspension == nil {
13341350
if c.ignoreLazyGlob(e) {
13351351
return
13361352
}

d2ir/d2ir.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,41 @@ func (rc *RefContext) EdgeIndex() int {
650650
func (rc *RefContext) Equal(rc2 *RefContext) bool {
651651
// We intentionally ignore edges here because the same glob can produce multiple RefContexts that should be treated the same with only the edge as the difference.
652652
// Same with ScopeMap.
653-
return rc.Key.Equals(rc2.Key) && rc.Scope == rc2.Scope && rc.ScopeAST == rc2.ScopeAST
653+
if !(rc.Key.Equals(rc2.Key) && rc.Scope == rc2.Scope && rc.ScopeAST == rc2.ScopeAST) {
654+
return false
655+
}
656+
657+
// Check if suspension values match for suspension operations
658+
// We don't want these two to equal
659+
// 1. *: suspend
660+
// 2. *: unsuspend
661+
hasSuspension1 := (rc.Key.Primary.Suspension != nil || rc.Key.Value.Suspension != nil)
662+
hasSuspension2 := (rc2.Key.Primary.Suspension != nil || rc2.Key.Value.Suspension != nil)
663+
664+
if hasSuspension1 || hasSuspension2 {
665+
var val1, val2 bool
666+
if rc.Key.Primary.Suspension != nil {
667+
val1 = rc.Key.Primary.Suspension.Value
668+
} else if rc.Key.Value.Suspension != nil {
669+
val1 = rc.Key.Value.Suspension.Value
670+
}
671+
672+
if rc2.Key.Primary.Suspension != nil {
673+
val2 = rc2.Key.Primary.Suspension.Value
674+
} else if rc2.Key.Value.Suspension != nil {
675+
val2 = rc2.Key.Value.Suspension.Value
676+
}
677+
678+
if hasSuspension1 && hasSuspension2 && val1 != val2 {
679+
return false
680+
}
681+
682+
if hasSuspension1 != hasSuspension2 {
683+
return false
684+
}
685+
}
686+
687+
return true
654688
}
655689

656690
func (m *Map) FieldCountRecursive() int {

testdata/d2compiler/TestCompile2/globs/suspend-shape.exp.json

+101
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)