Skip to content

Commit

Permalink
Merge pull request #2203 from alixander/transition-links
Browse files Browse the repository at this point in the history
d2compile: invalidate self-links
  • Loading branch information
alixander authored Nov 8, 2024
2 parents 2f3721c + 1545ddf commit 95a0b94
Show file tree
Hide file tree
Showing 5 changed files with 784 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

#### Improvements 🧹

- Composition: links pointing to own board are purged [#2203](https://github.com/terrastruct/d2/pull/2203)

#### Bugfixes ⛑️
6 changes: 6 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/fs"
"net/url"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -1224,6 +1225,11 @@ func (c *compiler) validateBoardLinks(g *d2graph.Graph) {
obj.Link = nil
continue
}

if slices.Equal(linkKey.IDA(), obj.Graph.IDA()) {
obj.Link = nil
continue
}
}
for _, b := range g.Layers {
c.validateBoardLinks(b)
Expand Down
21 changes: 21 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,27 @@ scenarios: {
tassert.Equal(t, "root.layers.cat", g.Scenarios[0].Objects[0].Link.Value)
},
},
{
name: "no-self-link",
text: `
x.link: scenarios.a
layers: {
g: {
s.link: _.layers.g
}
}
scenarios: {
a: {
b
}
}`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, (*d2graph.Scalar)(nil), g.Scenarios[0].Objects[0].Link)
tassert.Equal(t, (*d2graph.Scalar)(nil), g.Layers[0].Objects[0].Link)
},
},
{
name: "link-board-not-found-1",
text: `x.link: layers.x
Expand Down
60 changes: 60 additions & 0 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,66 @@ func (g *Graph) RootBoard() *Graph {
return g
}

func (g *Graph) IDA() []string {
if g == nil {
return nil
}

var parts []string

current := g
for current != nil {
if current.Name != "" {
parts = append(parts, current.Name)
}
current = current.Parent
}

for i := 0; i < len(parts)/2; i++ {
j := len(parts) - 1 - i
parts[i], parts[j] = parts[j], parts[i]
}

if len(parts) == 0 {
return []string{"root"}
}

parts = append([]string{"root"}, parts...)

if g.Parent != nil {
var containerName string
if len(g.Parent.Layers) > 0 {
for _, l := range g.Parent.Layers {
if l == g {
containerName = "layers"
break
}
}
}
if len(g.Parent.Scenarios) > 0 {
for _, s := range g.Parent.Scenarios {
if s == g {
containerName = "scenarios"
break
}
}
}
if len(g.Parent.Steps) > 0 {
for _, s := range g.Parent.Steps {
if s == g {
containerName = "steps"
break
}
}
}
if containerName != "" {
parts = append(parts[:1], append([]string{containerName}, parts[1:]...)...)
}
}

return parts
}

type LayoutGraph func(context.Context, *Graph) error
type RouteEdges func(context.Context, *Graph, []*Edge) error

Expand Down
Loading

0 comments on commit 95a0b94

Please sign in to comment.