From 15ba701576b3c6b2bec03dc26b74e7284e68441f Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 3 Jun 2024 13:11:45 -0600 Subject: [PATCH 1/3] d2oracle: glob null edges --- d2ir/compile.go | 7 +- d2ir/pattern_test.go | 12 + .../patterns/edge-glob-null.exp.json | 473 ++++++++++++++++++ 3 files changed, 491 insertions(+), 1 deletion(-) create mode 100644 testdata/d2ir/TestCompile/patterns/edge-glob-null.exp.json diff --git a/d2ir/compile.go b/d2ir/compile.go index 273637a9d..6d5145b53 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -991,7 +991,7 @@ func (c *compiler) compileEdges(refctx *RefContext) { func (c *compiler) _compileEdges(refctx *RefContext) { eida := NewEdgeIDs(refctx.Key) for i, eid := range eida { - if refctx.Key != nil && refctx.Key.Value.Null != nil { + if !eid.Glob && (refctx.Key.Primary.Null != nil || refctx.Key.Value.Null != nil) { refctx.ScopeMap.DeleteEdge(eid) continue } @@ -1016,6 +1016,11 @@ func (c *compiler) _compileEdges(refctx *RefContext) { }) refctx.ScopeMap.appendFieldReferences(0, refctx.Edge.Src, refctx, c) refctx.ScopeMap.appendFieldReferences(0, refctx.Edge.Dst, refctx, c) + + if refctx.Key.Primary.Null != nil || refctx.Key.Value.Null != nil { + refctx.ScopeMap.DeleteEdge(e.ID) + continue + } } } else { var err error diff --git a/d2ir/pattern_test.go b/d2ir/pattern_test.go index 941db0785..a8a835f2c 100644 --- a/d2ir/pattern_test.go +++ b/d2ir/pattern_test.go @@ -310,6 +310,18 @@ layers.x: { wrapper.p } assertQuery(t, m, 0, 0, nil, "layers.x.wrapper.p") }, }, + { + name: "edge-glob-null", + run: func(t testing.TB) { + m, err := compile(t, `a -> b +(* -> *)[*]: null +x -> y +`) + assert.Success(t, err) + // 4 fields and 0 edges + assertQuery(t, m, 4, 0, nil, "") + }, + }, { name: "double-glob/edge/1", run: func(t testing.TB) { diff --git a/testdata/d2ir/TestCompile/patterns/edge-glob-null.exp.json b/testdata/d2ir/TestCompile/patterns/edge-glob-null.exp.json new file mode 100644 index 000000000..a61325deb --- /dev/null +++ b/testdata/d2ir/TestCompile/patterns/edge-glob-null.exp.json @@ -0,0 +1,473 @@ +{ + "fields": [ + { + "name": "a", + "references": [ + { + "string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "context": { + "edge": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:6:6", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "dst_arrow": ">" + }, + "key": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:6:6", + "edges": [ + { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:6:6", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + }, + { + "name": "b", + "references": [ + { + "string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "context": { + "edge": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:6:6", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "dst_arrow": ">" + }, + "key": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:6:6", + "edges": [ + { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:6:6", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:0:0-0:1:1", + "value": [ + { + "string": "a", + "raw_string": "a" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,0:5:5-0:6:6", + "value": [ + { + "string": "b", + "raw_string": "b" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + }, + { + "name": "x", + "references": [ + { + "string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "context": { + "edge": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:6:31", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "dst_arrow": ">" + }, + "key": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:6:31", + "edges": [ + { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:6:31", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + }, + { + "name": "y", + "references": [ + { + "string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + }, + "key_path": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "context": { + "edge": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:6:31", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "dst_arrow": ">" + }, + "key": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:6:31", + "edges": [ + { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:6:31", + "src": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:0:25-2:1:26", + "value": [ + { + "string": "x", + "raw_string": "x" + } + ] + } + } + ] + }, + "src_arrow": "", + "dst": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "path": [ + { + "unquoted_string": { + "range": "TestCompile/patterns/edge-glob-null.d2,2:5:30-2:6:31", + "value": [ + { + "string": "y", + "raw_string": "y" + } + ] + } + } + ] + }, + "dst_arrow": ">" + } + ], + "primary": {}, + "value": {} + } + }, + "due_to_glob": false, + "due_to_lazy_glob": false + } + ] + } + ], + "edges": [] +} From b2451eb30c7a2d2d9ca135b629bce49a055d718e Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 3 Jun 2024 13:18:20 -0600 Subject: [PATCH 2/3] fix d2chaos --- d2chaos/d2chaos.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2chaos/d2chaos.go b/d2chaos/d2chaos.go index 42445a974..337ac24ae 100644 --- a/d2chaos/d2chaos.go +++ b/d2chaos/d2chaos.go @@ -269,7 +269,7 @@ func (gs *dslGenState) randStr(n int, inKey bool) string { func (gs *dslGenState) randShape() string { for { s := shapes[gs.rand.Intn(len(shapes))] - if s != d2target.ShapeImage { + if s != d2target.ShapeImage && s != d2target.ShapeText { return s } } From 65a3dadfbaf7fcf6dfa69049444a15f9b9e99bf4 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 3 Jun 2024 13:27:33 -0600 Subject: [PATCH 3/3] move up --- d2ir/compile.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/d2ir/compile.go b/d2ir/compile.go index 6d5145b53..c147daf36 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -1009,6 +1009,10 @@ func (c *compiler) _compileEdges(refctx *RefContext) { continue } for _, e := range ea { + if refctx.Key.Primary.Null != nil || refctx.Key.Value.Null != nil { + refctx.ScopeMap.DeleteEdge(e.ID) + continue + } e.References = append(e.References, &EdgeReference{ Context_: refctx, DueToGlob_: len(c.globRefContextStack) > 0, @@ -1016,11 +1020,6 @@ func (c *compiler) _compileEdges(refctx *RefContext) { }) refctx.ScopeMap.appendFieldReferences(0, refctx.Edge.Src, refctx, c) refctx.ScopeMap.appendFieldReferences(0, refctx.Edge.Dst, refctx, c) - - if refctx.Key.Primary.Null != nil || refctx.Key.Value.Null != nil { - refctx.ScopeMap.DeleteEdge(e.ID) - continue - } } } else { var err error