d2ir: Filter glob creation of edges with edge index where appropriate

On query globs, filtering without the edge index overfilters as we only match
one of the edge instead all of them.
This commit is contained in:
Anmol Sethi 2023-08-18 09:35:55 -07:00
parent 69df741a6b
commit 57ffc8dbbd
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
9 changed files with 19695 additions and 3 deletions

View file

@ -1237,10 +1237,14 @@ func (m *Map) createEdge2(eid *EdgeID, refctx *RefContext, gctx *globContext, sr
if gctx != nil { if gctx != nil {
var ks string var ks string
// We only ever want to create one of the edge per glob so we filter without the edge index.
e2 := e.Copy(e.Parent()).(*Edge)
e2.ID = e2.ID.Copy()
e2.ID.Index = nil
if refctx.Key.HasTripleGlob() { if refctx.Key.HasTripleGlob() {
ks = d2format.Format(d2ast.MakeKeyPath(IDA(e))) ks = d2format.Format(d2ast.MakeKeyPath(IDA(e2)))
} else { } else {
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(e))) ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(e2)))
} }
if _, ok := gctx.appliedEdges[ks]; ok { if _, ok := gctx.appliedEdges[ks]; ok {
return nil, nil return nil, nil
@ -1304,6 +1308,11 @@ func (e *Edge) AST() d2ast.Node {
func (e *Edge) IDString() string { func (e *Edge) IDString() string {
ast := e.AST().(*d2ast.Key) ast := e.AST().(*d2ast.Key)
if e.ID.Index != nil {
ast.EdgeIndex = &d2ast.EdgeIndex{
Int: e.ID.Index,
}
}
ast.Primary = d2ast.ScalarBox{} ast.Primary = d2ast.ScalarBox{}
ast.Value = d2ast.ValueBox{} ast.Value = d2ast.ValueBox{}
return d2format.Format(ast) return d2format.Format(ast)

View file

@ -97,7 +97,7 @@ x -> y
}, },
}, },
{ {
name: "label-filter", name: "label-filter/1",
run: func(t testing.TB) { run: func(t testing.TB) {
m, err := compile(t, ` m, err := compile(t, `
x x
@ -129,6 +129,27 @@ a -> z: delta
assertQuery(t, m, 0, 0, "diamond", "(a -> z).target-arrowhead.shape") assertQuery(t, m, 0, 0, "diamond", "(a -> z).target-arrowhead.shape")
}, },
}, },
{
name: "label-filter/2",
run: func(t testing.TB) {
m, err := compile(t, `
(* -> *)[*].style.opacity: 0.1
(* -> *)[*]: {
&label: hi
style.opacity: 1
}
x -> y: hi
x -> y
`)
assert.Success(t, err)
assertQuery(t, m, 6, 2, nil, "")
assertQuery(t, m, 2, 0, "hi", "(x -> y)[0]")
assertQuery(t, m, 0, 0, 1, "(x -> y)[0].style.opacity")
assertQuery(t, m, 0, 0, 0.1, "(x -> y)[1].style.opacity")
},
},
} }
runa(t, tca) runa(t, tca)

View file

@ -500,6 +500,21 @@ scenarios: {
assertQuery(t, m, 0, 0, "red", "scenarios.b.b.style.fill") assertQuery(t, m, 0, 0, "red", "scenarios.b.b.style.fill")
}, },
}, },
{
name: "alixander-review/6",
run: func(t testing.TB) {
m, err := compile(t, `
(* -> *)[*].style.opacity: 0.1
x -> y: hi
x -> y
`)
assert.Success(t, err)
assertQuery(t, m, 6, 2, nil, "")
assertQuery(t, m, 0, 0, 0.1, "(x -> y)[0].style.opacity")
assertQuery(t, m, 0, 0, 0.1, "(x -> y)[1].style.opacity")
},
},
{ {
name: "override/1", name: "override/1",
run: func(t testing.TB) { run: func(t testing.TB) {

File diff suppressed because it is too large Load diff

4654
testdata/d2ir/TestCompile/filters/label-filter/1.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff

3068
testdata/d2ir/TestCompile/filters/label-filter/2.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff