Skip to content

Commit

Permalink
d2compiler: fix underscore links
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Jul 13, 2024
1 parent 7ee1715 commit 26ded98
Show file tree
Hide file tree
Showing 4 changed files with 1,461 additions and 1 deletion.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
- Edge globs setting styles inherit correctly in child boards [#1967](https://github.com/terrastruct/d2/pull/1967)
- Board links imported with spread imports work [#1972](https://github.com/terrastruct/d2/pull/1972)
- Fix importing a file with nested boards [#1998](https://github.com/terrastruct/d2/pull/1998)
- Fix importing a file with underscores in links [#1999](https://github.com/terrastruct/d2/pull/1999)
33 changes: 33 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2917,6 +2917,39 @@ layers: {
}
}`,
},
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "root.layers.x.layers.b", g.Layers[0].Objects[0].Link.Value)
},
},
{
name: "import-link-underscore",
text: `k
layers: {
x: {...@x}
}`,
files: map[string]string{
"x.d2": `a
layers: {
b: {
d.link: _
layers: {
c: {
c.link: _
z.link: _._
f.link: _._.layers.b
}
}
}
}`,
},
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, "root.layers.x", g.Layers[0].Layers[0].Objects[0].Link.Value)
tassert.Equal(t, "root.layers.x.layers.b", g.Layers[0].Layers[0].Layers[0].Objects[0].Link.Value)
tassert.Equal(t, "root.layers.x", g.Layers[0].Layers[0].Layers[0].Objects[1].Link.Value)
tassert.Equal(t, "root.layers.x.layers.b", g.Layers[0].Layers[0].Layers[0].Objects[2].Link.Value)
},
},
{
name: "import-nested-layers",
Expand Down
13 changes: 12 additions & 1 deletion d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,17 @@ func (c *compiler) updateLinks(m *Map) {
}
bida := BoardIDA(f)
aida := IDA(f)

// The id path from that board to field
relaida := aida[len(bida)+1 : len(aida)-len(bida)]
// If the link value has underscores, the path length can be less than the path length of the field
uplevels := len(relaida) - len(linkIDA) + 1

if len(bida) != len(aida) {
prependIDA := aida[:len(aida)-len(bida)]
if uplevels > 0 {
prependIDA = prependIDA[:len(prependIDA)-uplevels]
}
fullIDA := []string{"root"}
// With nested imports, a value may already have been updated with part of the absolute path
// E.g.,
Expand All @@ -876,9 +885,11 @@ func (c *compiler) updateLinks(m *Map) {
// -------
// a b c d
OUTER:
// Starts at 1 assuming 0 is "root" for both
// +2 assuming layers/scenarios/steps is in between both
for i := 1; i < len(prependIDA); i += 2 {
for j := 0; i+j < len(prependIDA); j++ {
if prependIDA[i+j] != linkIDA[1+j] {
if 1+j >= len(linkIDA) || prependIDA[i+j] != linkIDA[1+j] {
break
}
// Reached the end and all common
Expand Down
Loading

0 comments on commit 26ded98

Please sign in to comment.