d2: Fix nested import spread ghost container bug

This commit is contained in:
Anmol Sethi 2023-06-06 15:09:27 -07:00
parent 0300b81c31
commit de0a863e9f
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
6 changed files with 277 additions and 10 deletions

View file

@ -262,17 +262,19 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
obj.Map = fr.Context.Key.Value.Map
}
}
scopeObjIDA := d2ir.BoardIDA(fr.Context.ScopeMap)
scopeObj := obj.Graph.Root.EnsureChildIDVal(scopeObjIDA)
obj.References = append(obj.References, d2graph.Reference{
r := d2graph.Reference{
Key: fr.KeyPath,
KeyPathIndex: fr.KeyPathIndex(),
MapKey: fr.Context.Key,
MapKeyEdgeIndex: fr.Context.EdgeIndex(),
Scope: fr.Context.Scope,
ScopeObj: scopeObj,
})
}
if fr.Context.ScopeMap != nil {
scopeObjIDA := d2ir.BoardIDA(fr.Context.ScopeMap)
r.ScopeObj = obj.Graph.Root.EnsureChildIDVal(scopeObjIDA)
}
obj.References = append(obj.References, r)
}
}
@ -604,15 +606,17 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) {
edge.Label.MapKey = e.LastPrimaryKey()
for _, er := range e.References {
scopeObjIDA := d2ir.BoardIDA(er.Context.ScopeMap)
scopeObj := edge.Src.Graph.Root.EnsureChildIDVal(scopeObjIDA)
edge.References = append(edge.References, d2graph.EdgeReference{
r := d2graph.EdgeReference{
Edge: er.Context.Edge,
MapKey: er.Context.Key,
MapKeyEdgeIndex: er.Context.EdgeIndex(),
Scope: er.Context.Scope,
ScopeObj: scopeObj,
})
}
if er.Context.ScopeMap != nil {
scopeObjIDA := d2ir.BoardIDA(er.Context.ScopeMap)
r.ScopeObj = edge.Src.Graph.Root.EnsureChildIDVal(scopeObjIDA)
}
edge.References = append(edge.References, r)
}
}

View file

@ -48,6 +48,7 @@ func (c *compiler) _import(imp *d2ast.Import) (Node, bool) {
if !ok {
return nil, false
}
nilScopeMap(ir)
if len(imp.IDA()) > 0 {
f := ir.GetField(imp.IDA()...)
if f == nil {
@ -126,3 +127,29 @@ func (c *compiler) __import(imp *d2ast.Import) (*Map, bool) {
return ir, true
}
func nilScopeMap(n Node) {
switch n := n.(type) {
case *Map:
for _, f := range n.Fields {
nilScopeMap(f)
}
for _, e := range n.Edges {
nilScopeMap(e)
}
case *Edge:
for _, r := range n.References {
r.Context.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
case *Field:
for _, r := range n.References {
r.Context.ScopeMap = nil
}
if n.Map() != nil {
nilScopeMap(n.Map())
}
}
}

View file

@ -83,6 +83,19 @@ label: meow`,
assertQuery(t, m, 0, 0, "wowa", "x")
},
},
{
name: "nested/spread",
run: func(t testing.TB) {
m, err := compileFS(t, "index.d2", map[string]string{
"index.d2": "...@x.y",
"x.d2": "y: { jon; jan }",
})
assert.Success(t, err)
assertQuery(t, m, 2, 0, nil, "")
assertQuery(t, m, 0, 0, nil, "jan")
assertQuery(t, m, 0, 0, nil, "jon")
},
},
}
runa(t, tca)

View file

@ -378,6 +378,17 @@ steps: {
assert.Testdata(t, ".svg", svg)
},
},
{
name: "import_spread_nested",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "hello-world.d2", `...@x.y`)
writeFile(t, dir, "x.d2", `y: { jon; jan }`)
err := runTestMain(t, ctx, dir, env, filepath.Join(dir, "hello-world.d2"))
assert.Success(t, err)
svg := readFile(t, dir, "hello-world.svg")
assert.Testdata(t, ".svg", svg)
},
},
}
ctx := context.Background()

View file

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" d2Version="v0.4.2-HEAD" preserveAspectRatio="xMinYMin meet" viewBox="0 0 399 268"><svg id="d2-svg" class="d2-3967102011" width="399" height="268" viewBox="-101 -101 399 268"><rect x="-101.000000" y="-101.000000" width="399.000000" height="268.000000" rx="0.000000" class=" fill-N7" stroke-width="0" /><style type="text/css"><![CDATA[
.d2-3967102011 .text-bold {
font-family: "d2-3967102011-font-bold";
}
@font-face {
font-family: d2-3967102011-font-bold;
src: url("data:application/font-woff;base64,d09GRgABAAAAAAdYAAoAAAAADCwAAguFAAAAAAAAAAAAAAAAAAAAAAAAAABPUy8yAAAA9AAAAGAAAABgXxHXrmNtYXAAAAFUAAAARQAAAEYAggEoZ2x5ZgAAAZwAAAHJAAAB5EfbS0ZoZWFkAAADaAAAADYAAAA2G38e1GhoZWEAAAOgAAAAJAAAACQKfwXGaG10eAAAA8QAAAAcAAAAHAtXACZsb2NhAAAD4AAAABAAAAAQAdICTG1heHAAAAPwAAAAIAAAACAAHwD3bmFtZQAABBAAAAMoAAAIKgjwVkFwb3N0AAAHOAAAAB0AAAAg/9EAMgADAioCvAAFAAACigJYAAAASwKKAlgAAAFeADIBKQAAAgsHAwMEAwICBGAAAvcAAAADAAAAAAAAAABBREJPACAAIP//Au7/BgAAA9gBESAAAZ8AAAAAAfAClAAAACAAA3icLMqxDYQwFAXB+bYDB1fQtUNKANUgEdLpQwI22mBQusLP8Mc0NSxWe/LdluTKmeOxb6XpBjcAAAD//wEAAP//X78LpAAAAHicNJA9b9NQGIXPvUluSqgU3PgjgYZ83CS3DrKR7NpWaSMnkiWEVFehGYpAEJGBpVUqlVZUzIwwpQMTE4wMjHRgYoSpMLPwA4pUMQUb2VKHdzznfZ6DHIYAndATZHAFRSxBAWypIbVtIXjesz2PaxlPECk/pEvRh/dCz+p6tlt/W3s5HpPwCT35t/conEz+jtfXo3efT6M35PkpQNGNL8gPMkcFHNCaHWfV9Tod3mR54bq2pSoSF5wxz3I9hzFFVr8Ew1czyvVav+Xc3r0zfnZcyNbuLlTapa2N2uKOv/Wg2BBl5Wm1NT2IftvL/EAr7RRuVcsaEMfx93gDv+gZ7SAHIA+G1wAIBgkMmUNOvGzNvnwuraYw0uC4kK2H1v17s2p9eaVMzv2bxu7j6BtpuCsVLfqUxFvxBc2TOYq4AeSanU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lM9AelZZO80u0ZwEjU0UmhlP1cqmN3PoXmiKmqqWc7e19uQ1z273lFt+QaodLtS44lZNbMHrfVL13NHOtH4+AkJQLWQxImdKg4Ea8zwm4IsZxrO6daEsKWiufMs+NVBIxFYMOieLMyPQ3MN34xn2woXtnb0ko/5Lp5aqq+2Rx6tXtjN6oe8s737ocrU2gYVNN19Q0ENfEtB9pp9b5+/LN9bqlPOWIlJjwXy/AMzya7HPAIWNlGOhmbq9DUy9Ek5ccqvpLIlkNpefIIhzg8ZwDDnjJ83f6uGTijItbcVnP3eKYI7ocflAVC/suR7xeffv/rL+LaVO1OJ6uTi/uPcUnd1DrF9qz2/eyp4mVk5hbtNutOCNgWnJxu+s1ucd4/wAAAP//AQAA///0t09ReJxiYGYAg//nGIwYsAAAAAAA//8BAAD//y8BAgMAAAA=");
}]]></style><style type="text/css"><![CDATA[.shape {
shape-rendering: geometricPrecision;
stroke-linejoin: round;
}
.connection {
stroke-linecap: round;
stroke-linejoin: round;
}
.blend {
mix-blend-mode: multiply;
opacity: 0.5;
}
.d2-3967102011 .fill-N1{fill:#0A0F25;}
.d2-3967102011 .fill-N2{fill:#676C7E;}
.d2-3967102011 .fill-N3{fill:#9499AB;}
.d2-3967102011 .fill-N4{fill:#CFD2DD;}
.d2-3967102011 .fill-N5{fill:#DEE1EB;}
.d2-3967102011 .fill-N6{fill:#EEF1F8;}
.d2-3967102011 .fill-N7{fill:#FFFFFF;}
.d2-3967102011 .fill-B1{fill:#0D32B2;}
.d2-3967102011 .fill-B2{fill:#0D32B2;}
.d2-3967102011 .fill-B3{fill:#E3E9FD;}
.d2-3967102011 .fill-B4{fill:#E3E9FD;}
.d2-3967102011 .fill-B5{fill:#EDF0FD;}
.d2-3967102011 .fill-B6{fill:#F7F8FE;}
.d2-3967102011 .fill-AA2{fill:#4A6FF3;}
.d2-3967102011 .fill-AA4{fill:#EDF0FD;}
.d2-3967102011 .fill-AA5{fill:#F7F8FE;}
.d2-3967102011 .fill-AB4{fill:#EDF0FD;}
.d2-3967102011 .fill-AB5{fill:#F7F8FE;}
.d2-3967102011 .stroke-N1{stroke:#0A0F25;}
.d2-3967102011 .stroke-N2{stroke:#676C7E;}
.d2-3967102011 .stroke-N3{stroke:#9499AB;}
.d2-3967102011 .stroke-N4{stroke:#CFD2DD;}
.d2-3967102011 .stroke-N5{stroke:#DEE1EB;}
.d2-3967102011 .stroke-N6{stroke:#EEF1F8;}
.d2-3967102011 .stroke-N7{stroke:#FFFFFF;}
.d2-3967102011 .stroke-B1{stroke:#0D32B2;}
.d2-3967102011 .stroke-B2{stroke:#0D32B2;}
.d2-3967102011 .stroke-B3{stroke:#E3E9FD;}
.d2-3967102011 .stroke-B4{stroke:#E3E9FD;}
.d2-3967102011 .stroke-B5{stroke:#EDF0FD;}
.d2-3967102011 .stroke-B6{stroke:#F7F8FE;}
.d2-3967102011 .stroke-AA2{stroke:#4A6FF3;}
.d2-3967102011 .stroke-AA4{stroke:#EDF0FD;}
.d2-3967102011 .stroke-AA5{stroke:#F7F8FE;}
.d2-3967102011 .stroke-AB4{stroke:#EDF0FD;}
.d2-3967102011 .stroke-AB5{stroke:#F7F8FE;}
.d2-3967102011 .background-color-N1{background-color:#0A0F25;}
.d2-3967102011 .background-color-N2{background-color:#676C7E;}
.d2-3967102011 .background-color-N3{background-color:#9499AB;}
.d2-3967102011 .background-color-N4{background-color:#CFD2DD;}
.d2-3967102011 .background-color-N5{background-color:#DEE1EB;}
.d2-3967102011 .background-color-N6{background-color:#EEF1F8;}
.d2-3967102011 .background-color-N7{background-color:#FFFFFF;}
.d2-3967102011 .background-color-B1{background-color:#0D32B2;}
.d2-3967102011 .background-color-B2{background-color:#0D32B2;}
.d2-3967102011 .background-color-B3{background-color:#E3E9FD;}
.d2-3967102011 .background-color-B4{background-color:#E3E9FD;}
.d2-3967102011 .background-color-B5{background-color:#EDF0FD;}
.d2-3967102011 .background-color-B6{background-color:#F7F8FE;}
.d2-3967102011 .background-color-AA2{background-color:#4A6FF3;}
.d2-3967102011 .background-color-AA4{background-color:#EDF0FD;}
.d2-3967102011 .background-color-AA5{background-color:#F7F8FE;}
.d2-3967102011 .background-color-AB4{background-color:#EDF0FD;}
.d2-3967102011 .background-color-AB5{background-color:#F7F8FE;}
.d2-3967102011 .color-N1{color:#0A0F25;}
.d2-3967102011 .color-N2{color:#676C7E;}
.d2-3967102011 .color-N3{color:#9499AB;}
.d2-3967102011 .color-N4{color:#CFD2DD;}
.d2-3967102011 .color-N5{color:#DEE1EB;}
.d2-3967102011 .color-N6{color:#EEF1F8;}
.d2-3967102011 .color-N7{color:#FFFFFF;}
.d2-3967102011 .color-B1{color:#0D32B2;}
.d2-3967102011 .color-B2{color:#0D32B2;}
.d2-3967102011 .color-B3{color:#E3E9FD;}
.d2-3967102011 .color-B4{color:#E3E9FD;}
.d2-3967102011 .color-B5{color:#EDF0FD;}
.d2-3967102011 .color-B6{color:#F7F8FE;}
.d2-3967102011 .color-AA2{color:#4A6FF3;}
.d2-3967102011 .color-AA4{color:#EDF0FD;}
.d2-3967102011 .color-AA5{color:#F7F8FE;}
.d2-3967102011 .color-AB4{color:#EDF0FD;}
.d2-3967102011 .color-AB5{color:#F7F8FE;}.appendix text.text{fill:#0A0F25}.md{--color-fg-default:#0A0F25;--color-fg-muted:#676C7E;--color-fg-subtle:#9499AB;--color-canvas-default:#FFFFFF;--color-canvas-subtle:#EEF1F8;--color-border-default:#0D32B2;--color-border-muted:#0D32B2;--color-neutral-muted:#EEF1F8;--color-accent-fg:#0D32B2;--color-accent-emphasis:#0D32B2;--color-attention-subtle:#676C7E;--color-danger-fg:red;}.sketch-overlay-B1{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-B2{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-B3{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B6{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AA2{fill:url(#streaks-dark);mix-blend-mode:overlay}.sketch-overlay-AA4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AA5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AB4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AB5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N1{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-N2{fill:url(#streaks-dark);mix-blend-mode:overlay}.sketch-overlay-N3{fill:url(#streaks-normal);mix-blend-mode:color-burn}.sketch-overlay-N4{fill:url(#streaks-normal);mix-blend-mode:color-burn}.sketch-overlay-N5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N6{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N7{fill:url(#streaks-bright);mix-blend-mode:darken}.light-code{display: block}.dark-code{display: none}]]></style><g id="jon"><g class="shape" ><rect x="0.000000" y="0.000000" width="69.000000" height="66.000000" class=" stroke-B1 fill-B6" style="stroke-width:2;" /></g><text x="34.500000" y="38.500000" class="text-bold fill-N1" style="text-anchor:middle;font-size:16px">jon</text></g><g id="jan"><g class="shape" ><rect x="129.000000" y="0.000000" width="68.000000" height="66.000000" class=" stroke-B1 fill-B6" style="stroke-width:2;" /></g><text x="163.000000" y="38.500000" class="text-bold fill-N1" style="text-anchor:middle;font-size:16px">jan</text></g><mask id="d2-3967102011" maskUnits="userSpaceOnUse" x="-101" y="-101" width="399" height="268">
<rect x="-101" y="-101" width="399" height="268" fill="white"></rect>
</mask></svg></svg>

After

Width:  |  Height:  |  Size: 9.1 KiB

View file

@ -0,0 +1,117 @@
{
"fields": [
{
"name": "jon",
"references": [
{
"string": {
"range": "x.d2,0:5:5-0:8:8",
"value": [
{
"string": "jon",
"raw_string": "jon"
}
]
},
"key_path": {
"range": "x.d2,0:5:5-0:8:8",
"path": [
{
"unquoted_string": {
"range": "x.d2,0:5:5-0:8:8",
"value": [
{
"string": "jon",
"raw_string": "jon"
}
]
}
}
]
},
"context": {
"edge": null,
"key": {
"range": "x.d2,0:5:5-0:8:8",
"key": {
"range": "x.d2,0:5:5-0:8:8",
"path": [
{
"unquoted_string": {
"range": "x.d2,0:5:5-0:8:8",
"value": [
{
"string": "jon",
"raw_string": "jon"
}
]
}
}
]
},
"primary": {},
"value": {}
}
}
}
]
},
{
"name": "jan",
"references": [
{
"string": {
"range": "x.d2,0:10:10-0:13:13",
"value": [
{
"string": "jan",
"raw_string": "jan"
}
]
},
"key_path": {
"range": "x.d2,0:10:10-0:13:13",
"path": [
{
"unquoted_string": {
"range": "x.d2,0:10:10-0:13:13",
"value": [
{
"string": "jan",
"raw_string": "jan"
}
]
}
}
]
},
"context": {
"edge": null,
"key": {
"range": "x.d2,0:10:10-0:14:14",
"key": {
"range": "x.d2,0:10:10-0:13:13",
"path": [
{
"unquoted_string": {
"range": "x.d2,0:10:10-0:13:13",
"value": [
{
"string": "jan",
"raw_string": "jan"
}
]
}
}
]
},
"primary": {},
"value": {}
}
}
}
]
}
],
"edges": null
}