d2compiler: fix underscore links

This commit is contained in:
Alexander Wang 2024-07-13 08:38:57 -06:00
parent 7ee1715168
commit 26ded98f2e
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
4 changed files with 1461 additions and 1 deletions

View file

@ -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)

View file

@ -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",

View file

@ -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.,
@ -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

File diff suppressed because it is too large Load diff