From f215870a3dd06d867b5dc23305e1685cebf748a9 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 26 Feb 2025 15:06:48 -0700 Subject: [PATCH] ignore nulls when lazy globbing --- d2ir/compile.go | 20 +- .../txtar/model-nulling/dagre/board.exp.json | 316 ++++++++++++++++++ .../txtar/model-nulling/dagre/sketch.exp.svg | 106 ++++++ .../txtar/model-nulling/elk/board.exp.json | 307 +++++++++++++++++ .../txtar/model-nulling/elk/sketch.exp.svg | 106 ++++++ e2etests/txtar.txt | 27 ++ 6 files changed, 875 insertions(+), 7 deletions(-) create mode 100644 e2etests/testdata/txtar/model-nulling/dagre/board.exp.json create mode 100644 e2etests/testdata/txtar/model-nulling/dagre/sketch.exp.svg create mode 100644 e2etests/testdata/txtar/model-nulling/elk/board.exp.json create mode 100644 e2etests/testdata/txtar/model-nulling/elk/sketch.exp.svg diff --git a/d2ir/compile.go b/d2ir/compile.go index 668bc9190..f7436dcb1 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -858,12 +858,14 @@ func (c *compiler) _compileField(f *Field, refctx *RefContext) { } if len(refctx.Key.Edges) == 0 && (refctx.Key.Primary.Null != nil || refctx.Key.Value.Null != nil) { - // For vars, if we delete the field, it may just resolve to an outer scope var of the same name - // Instead we keep it around, so that resolveSubstitutions can find it - if !IsVar(ParentMap(f)) { - ParentMap(f).DeleteField(f.Name.ScalarString()) - return + if !c.lazyGlobBeingApplied { + // For vars, if we delete the field, it may just resolve to an outer scope var of the same name + // Instead we keep it around, so that resolveSubstitutions can find it + if !IsVar(ParentMap(f)) { + ParentMap(f).DeleteField(f.Name.ScalarString()) + } } + return } if refctx.Key.Primary.Unbox() != nil { @@ -1143,7 +1145,9 @@ func (c *compiler) _compileEdges(refctx *RefContext) { eida := NewEdgeIDs(refctx.Key) for i, eid := range eida { if !eid.Glob && (refctx.Key.Primary.Null != nil || refctx.Key.Value.Null != nil) { - refctx.ScopeMap.DeleteEdge(eid) + if !c.lazyGlobBeingApplied { + refctx.ScopeMap.DeleteEdge(eid) + } continue } @@ -1161,7 +1165,9 @@ func (c *compiler) _compileEdges(refctx *RefContext) { } for _, e := range ea { if refctx.Key.Primary.Null != nil || refctx.Key.Value.Null != nil { - refctx.ScopeMap.DeleteEdge(e.ID) + if !c.lazyGlobBeingApplied { + refctx.ScopeMap.DeleteEdge(e.ID) + } continue } e.References = append(e.References, &EdgeReference{ diff --git a/e2etests/testdata/txtar/model-nulling/dagre/board.exp.json b/e2etests/testdata/txtar/model-nulling/dagre/board.exp.json new file mode 100644 index 000000000..92915fc79 --- /dev/null +++ b/e2etests/testdata/txtar/model-nulling/dagre/board.exp.json @@ -0,0 +1,316 @@ +{ + "name": "", + "config": { + "sketch": false, + "themeID": 0, + "darkThemeID": null, + "pad": null, + "center": null, + "layoutEngine": null + }, + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "hello", + "type": "rectangle", + "pos": { + "x": 0, + "y": 50 + }, + "width": 80, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "hello", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 35, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "yes", + "type": "rectangle", + "pos": { + "x": 130, + "y": 20 + }, + "width": 123, + "height": 126, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B4", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "yes", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 38, + "labelHeight": 36, + "labelPosition": "OUTSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "yes.ok", + "type": "rectangle", + "pos": { + "x": 160, + "y": 50 + }, + "width": 63, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "ok", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 18, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "a", + "type": "rectangle", + "pos": { + "x": 303, + "y": 50 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "a", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "b", + "type": "rectangle", + "pos": { + "x": 303, + "y": 266 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "b", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + } + ], + "connections": [ + { + "id": "(a -> b)[0]", + "src": "a", + "srcArrow": "none", + "dst": "b", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "link": "", + "route": [ + { + "x": 329.5, + "y": 116 + }, + { + "x": 329.5, + "y": 156 + }, + { + "x": 329.5, + "y": 226 + }, + { + "x": 329.5, + "y": 266 + } + ], + "isCurve": true, + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + } + ], + "root": { + "id": "", + "type": "", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 0, + "strokeDash": 0, + "strokeWidth": 0, + "borderRadius": 0, + "fill": "N7", + "stroke": "", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 0 + } +} diff --git a/e2etests/testdata/txtar/model-nulling/dagre/sketch.exp.svg b/e2etests/testdata/txtar/model-nulling/dagre/sketch.exp.svg new file mode 100644 index 000000000..833c578d5 --- /dev/null +++ b/e2etests/testdata/txtar/model-nulling/dagre/sketch.exp.svg @@ -0,0 +1,106 @@ +helloyesabok + + + + + + + \ No newline at end of file diff --git a/e2etests/testdata/txtar/model-nulling/elk/board.exp.json b/e2etests/testdata/txtar/model-nulling/elk/board.exp.json new file mode 100644 index 000000000..15d161ccf --- /dev/null +++ b/e2etests/testdata/txtar/model-nulling/elk/board.exp.json @@ -0,0 +1,307 @@ +{ + "name": "", + "config": { + "sketch": false, + "themeID": 0, + "darkThemeID": null, + "pad": null, + "center": null, + "layoutEngine": null + }, + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "hello", + "type": "rectangle", + "pos": { + "x": 12, + "y": 62 + }, + "width": 80, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "hello", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 35, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "yes", + "type": "rectangle", + "pos": { + "x": 112, + "y": 12 + }, + "width": 163, + "height": 166, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B4", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "yes", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 38, + "labelHeight": 36, + "labelPosition": "INSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "yes.ok", + "type": "rectangle", + "pos": { + "x": 162, + "y": 62 + }, + "width": 63, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "ok", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 18, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "a", + "type": "rectangle", + "pos": { + "x": 295, + "y": 112 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "a", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "b", + "type": "rectangle", + "pos": { + "x": 295, + "y": 248 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "b", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + } + ], + "connections": [ + { + "id": "(a -> b)[0]", + "src": "a", + "srcArrow": "none", + "dst": "b", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "link": "", + "route": [ + { + "x": 321.5, + "y": 178 + }, + { + "x": 321.5, + "y": 248 + } + ], + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + } + ], + "root": { + "id": "", + "type": "", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 0, + "strokeDash": 0, + "strokeWidth": 0, + "borderRadius": 0, + "fill": "N7", + "stroke": "", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 0 + } +} diff --git a/e2etests/testdata/txtar/model-nulling/elk/sketch.exp.svg b/e2etests/testdata/txtar/model-nulling/elk/sketch.exp.svg new file mode 100644 index 000000000..1bad0a97a --- /dev/null +++ b/e2etests/testdata/txtar/model-nulling/elk/sketch.exp.svg @@ -0,0 +1,106 @@ +helloyesabok + + + + + + + \ No newline at end of file diff --git a/e2etests/txtar.txt b/e2etests/txtar.txt index fcc6a7619..63ca40aec 100644 --- a/e2etests/txtar.txt +++ b/e2etests/txtar.txt @@ -775,3 +775,30 @@ a -> b: hello { b -> c: { icon: https://icons.terrastruct.com/essentials%2F213-alarm.svg } + +-- model-nulling -- +# Models +user +softwareSystem: { + serviceA + serviceB + serviceC + serviceD + + serviceA -> serviceB + serviceA -> serviceD + serviceC -> serviceB +} +externalSystem +user -> softwareSystem +softwareSystem -> externalSystem + +# Clear models +**: null +(** -> **)[*]: null + +hello +yes: { + ok +} +a -> b