d2compiler: fix underscore links
This commit is contained in:
parent
7ee1715168
commit
26ded98f2e
4 changed files with 1461 additions and 1 deletions
|
|
@ -14,3 +14,4 @@
|
||||||
- Edge globs setting styles inherit correctly in child boards [#1967](https://github.com/terrastruct/d2/pull/1967)
|
- 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)
|
- 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 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)
|
||||||
|
|
|
||||||
|
|
@ -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",
|
name: "import-nested-layers",
|
||||||
|
|
|
||||||
|
|
@ -863,8 +863,17 @@ func (c *compiler) updateLinks(m *Map) {
|
||||||
}
|
}
|
||||||
bida := BoardIDA(f)
|
bida := BoardIDA(f)
|
||||||
aida := IDA(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) {
|
if len(bida) != len(aida) {
|
||||||
prependIDA := aida[:len(aida)-len(bida)]
|
prependIDA := aida[:len(aida)-len(bida)]
|
||||||
|
if uplevels > 0 {
|
||||||
|
prependIDA = prependIDA[:len(prependIDA)-uplevels]
|
||||||
|
}
|
||||||
fullIDA := []string{"root"}
|
fullIDA := []string{"root"}
|
||||||
// With nested imports, a value may already have been updated with part of the absolute path
|
// With nested imports, a value may already have been updated with part of the absolute path
|
||||||
// E.g.,
|
// E.g.,
|
||||||
|
|
@ -876,9 +885,11 @@ func (c *compiler) updateLinks(m *Map) {
|
||||||
// -------
|
// -------
|
||||||
// a b c d
|
// a b c d
|
||||||
OUTER:
|
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 i := 1; i < len(prependIDA); i += 2 {
|
||||||
for j := 0; i+j < len(prependIDA); j++ {
|
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
|
break
|
||||||
}
|
}
|
||||||
// Reached the end and all common
|
// Reached the end and all common
|
||||||
|
|
|
||||||
1415
testdata/d2compiler/TestCompile/import-link-underscore.exp.json
generated
vendored
Normal file
1415
testdata/d2compiler/TestCompile/import-link-underscore.exp.json
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue