diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 7bed57db9..01fbde588 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -63,6 +63,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph { ir = ir.Copy(nil).(*d2ir.Map) // c.preprocessSeqDiagrams(ir) c.compileMap(g.Root, ir) + // c.resolveUnderscoreRefs(g.Root, ir) if len(c.err.Errors) == 0 { c.validateKeys(g.Root, ir) } @@ -111,6 +112,18 @@ func (c *compiler) errorf(n d2ast.Node, f string, v ...interface{}) { c.err.Errors = append(c.err.Errors, d2parser.Errorf(n, f, v...).(d2ast.Error)) } +// func (c *compiler) resolveUnderscoreRefs(obj *d2graph.Object, m *d2ir.Map) { +// for _, f := range m.Fields { +// } +// +// for _, obj := range g.Objects { +// for _, ref := range obj.References { +// if ref.ScopeObj == nil { +// } +// } +// } +// } + func (c *compiler) compileMap(obj *d2graph.Object, m *d2ir.Map) { shape := m.GetField("shape") if shape != nil { @@ -189,7 +202,13 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) { } } scopeObjIDA := d2ir.IDA(fr.Context.ScopeMap) - scopeObj, _ := obj.Graph.Root.HasChildIDVal(scopeObjIDA) + // spew.Dump("==========") + // println("\033[1;31m--- DEBUG:", "=======================", "\033[m") + // for _, o := range obj.Graph.Root.ChildrenArray { + // println("\033[1;31m--- DEBUG:", o.AbsID(), "\033[m") + // } + scopeObj := obj.Graph.Root.EnsureChildIDVal(scopeObjIDA) + // spew.Dump(obj.AbsID(), scopeObjIDA, scopeObj == nil) obj.References = append(obj.References, d2graph.Reference{ Key: fr.KeyPath, KeyPathIndex: fr.KeyPathIndex(), @@ -427,7 +446,7 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) { edge.Attributes.Label.MapKey = e.LastPrimaryKey() for _, er := range e.References { scopeObjIDA := d2ir.IDA(er.Context.ScopeMap) - scopeObj, _ := edge.Src.Graph.Root.HasChildIDVal(scopeObjIDA) + scopeObj := edge.Src.Graph.Root.EnsureChildIDVal(scopeObjIDA) edge.References = append(edge.References, d2graph.EdgeReference{ Edge: er.Context.Edge, MapKey: er.Context.Key, diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index ae3bfc74d..a19487ee2 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -560,15 +560,15 @@ func (obj *Object) HasChild(ids []string) (*Object, bool) { return child, true } -// Keep in sync with HasChild. -func (obj *Object) HasChildIDVal(ids []string) (*Object, bool) { +// Keep in sync with EnsureChild. +func (obj *Object) EnsureChildIDVal(ids []string) *Object { if len(ids) == 0 { - return obj, true + return obj } if len(ids) == 1 && ids[0] != "style" { _, ok := ReservedKeywords[ids[0]] if ok { - return obj, true + return obj } } @@ -583,13 +583,13 @@ func (obj *Object) HasChildIDVal(ids []string) (*Object, bool) { } } if child == nil { - return nil, false + child = obj.newObject(id) } if len(ids) >= 1 { - return child.HasChildIDVal(ids) + return child.EnsureChildIDVal(ids) } - return child, true + return child } func (obj *Object) HasEdge(mk *d2ast.Key) (*Edge, bool) { diff --git a/d2oracle/edit_test.go b/d2oracle/edit_test.go index 9b84e15ed..0050b7f91 100644 --- a/d2oracle/edit_test.go +++ b/d2oracle/edit_test.go @@ -3322,8 +3322,7 @@ b: { key: `a`, newKey: `b.a`, - exp: `b.a -b: { + exp: `b: { a } `, diff --git a/testdata/d2compiler/TestCompile2/boards/recursive.exp.json b/testdata/d2compiler/TestCompile2/boards/recursive.exp.json index 4496b0752..805ca96fe 100644 --- a/testdata/d2compiler/TestCompile2/boards/recursive.exp.json +++ b/testdata/d2compiler/TestCompile2/boards/recursive.exp.json @@ -740,6 +740,56 @@ } }, "zIndex": 0 + }, + { + "id": "layers", + "id_val": "layers", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "layers" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "one", + "id_val": "one", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "one" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 } ] }, @@ -1112,6 +1162,56 @@ } }, "zIndex": 0 + }, + { + "id": "layers", + "id_val": "layers", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "layers" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "two", + "id_val": "two", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "two" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 } ], "steps": [ @@ -1484,6 +1584,106 @@ } }, "zIndex": 0 + }, + { + "id": "layers", + "id_val": "layers", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "layers" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "two", + "id_val": "two", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "two" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "steps", + "id_val": "steps", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "steps" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "seinfeld", + "id_val": "seinfeld", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "seinfeld" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 } ] }, @@ -1856,6 +2056,106 @@ } }, "zIndex": 0 + }, + { + "id": "layers", + "id_val": "layers", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "layers" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "two", + "id_val": "two", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "two" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "steps", + "id_val": "steps", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "steps" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "missoula", + "id_val": "missoula", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "missoula" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 } ] } diff --git a/testdata/d2compiler/TestCompile2/boards/root.exp.json b/testdata/d2compiler/TestCompile2/boards/root.exp.json index 7efd58eac..7d49aad12 100644 --- a/testdata/d2compiler/TestCompile2/boards/root.exp.json +++ b/testdata/d2compiler/TestCompile2/boards/root.exp.json @@ -474,6 +474,56 @@ } }, "zIndex": 0 + }, + { + "id": "layers", + "id_val": "layers", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "layers" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "one", + "id_val": "one", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "one" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 } ] }, @@ -713,6 +763,56 @@ } }, "zIndex": 0 + }, + { + "id": "layers", + "id_val": "layers", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "layers" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "two", + "id_val": "two", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "two" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 } ] } diff --git a/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.exp.json b/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.exp.json new file mode 100644 index 000000000..ef29483a3 --- /dev/null +++ b/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.exp.json @@ -0,0 +1,185 @@ +{ + "graph": { + "name": "", + "ast": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-3:0:11", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-2:1:10", + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "primary": {}, + "value": { + "map": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:3:3-2:0:9", + "nodes": [ + { + "map_key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "primary": {}, + "value": {} + } + } + ] + } + } + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "attributes": { + "label": { + "value": "" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + "edges": null, + "objects": [ + { + "id": "b", + "id_val": "b", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,0:0:0-0:1:1", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "b" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + }, + { + "id": "a", + "id_val": "a", + "label_dimensions": { + "width": 0, + "height": 0 + }, + "references": [ + { + "key": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2oracle/TestMove/container_multiple_refs_with_underscore.d2,1:2:7-1:3:8", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "key_path_index": 0, + "map_key_edge_index": -1 + } + ], + "attributes": { + "label": { + "value": "a" + }, + "style": {}, + "near_key": null, + "shape": { + "value": "rectangle" + }, + "direction": { + "value": "" + }, + "constraint": { + "value": "" + } + }, + "zIndex": 0 + } + ] + }, + "err": "" +}