Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2compiler: fix import underscore links #1999

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading