Skip to content

Commit

Permalink
d2ir: Make globs more ergonomic in two specific edge cases
Browse files Browse the repository at this point in the history
Were identified from @alixander writing documentation.
  • Loading branch information
nhooyr committed Jul 30, 2023
1 parent 73e4e68 commit 9c37d6d
Show file tree
Hide file tree
Showing 9 changed files with 2,009 additions and 1,271 deletions.
4 changes: 4 additions & 0 deletions d2ir/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func assertQuery(t testing.TB, n d2ir.Node, nfields, nedges int, primary interfa
}
}

if len(na) == 0 {
return nil
}

return na[0]
}

Expand Down
25 changes: 25 additions & 0 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ func (m *Map) FieldCountRecursive() int {
return acc
}

func (m *Map) IsContainer() bool {
if m == nil {
return false
}
for _, f := range m.Fields {
_, isReserved := d2graph.ReservedKeywords[f.Name]
if !isReserved {
return true
}
}
return false
}

func (m *Map) EdgeCountRecursive() int {
if m == nil {
return 0
Expand Down Expand Up @@ -1066,6 +1079,18 @@ func (m *Map) createEdge(eid *EdgeID, refctx *RefContext, ea *[]*Edge) error {

for _, src := range srcFA {
for _, dst := range dstFA {
if src == dst && (len(srcFA) > 1 || len(dstFA) > 1) {
// Globs do not make self edges.
continue
}

// If either has a double glob at the end we only select leafs, those without children.
if srcKP.Path[len(srcKP.Path)-1].ScalarString() == "**" || dstKP.Path[len(dstKP.Path)-1].ScalarString() == "**" {
if src.Map().IsContainer() || dst.Map().IsContainer() {
continue
}
}

eid2 := eid.Copy()
eid2.SrcPath = RelIDA(m, src)
eid2.DstPath = RelIDA(m, dst)
Expand Down
47 changes: 40 additions & 7 deletions d2ir/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ a*n*t*.constant.t*ink*r*t*inke*: globbed`)
animal
an* -> an*`)
assert.Success(t, err)
assertQuery(t, m, 2, 4, nil, "")
assertQuery(t, m, 2, 2, nil, "")
assertQuery(t, m, 0, 0, nil, "(animate -> animal)[0]")
assertQuery(t, m, 0, 0, nil, "(animal -> animal)[0]")
},
Expand All @@ -138,10 +138,10 @@ an* -> an*`)
shared.animal
sh*.(an* -> an*)`)
assert.Success(t, err)
assertQuery(t, m, 3, 4, nil, "")
assertQuery(t, m, 2, 4, nil, "shared")
assertQuery(t, m, 3, 2, nil, "")
assertQuery(t, m, 2, 2, nil, "shared")
assertQuery(t, m, 0, 0, nil, "shared.(animate -> animal)[0]")
assertQuery(t, m, 0, 0, nil, "shared.(animal -> animal)[0]")
assertQuery(t, m, 0, 0, nil, "shared.(animal -> animate)[0]")
},
},
{
Expand All @@ -151,8 +151,8 @@ sh*.(an* -> an*)`)
shared.animal
sh*.an* -> sh*.an*`)
assert.Success(t, err)
assertQuery(t, m, 3, 4, nil, "")
assertQuery(t, m, 2, 4, nil, "shared")
assertQuery(t, m, 3, 2, nil, "")
assertQuery(t, m, 2, 2, nil, "shared")
assertQuery(t, m, 0, 0, nil, "shared.(animate -> animal)[0]")
assertQuery(t, m, 0, 0, nil, "shared.(animal -> animal)[0]")
},
Expand Down Expand Up @@ -189,6 +189,23 @@ c -> b
assertQuery(t, m, 0, 0, "red", "(c -> b)[0].style.fill")
},
},
{
name: "edge-nexus",
run: func(t testing.TB) {
m, err := compile(t, `a
b
c
d
* -> nexus
`)
assert.Success(t, err)
assertQuery(t, m, 5, 4, nil, "")
assertQuery(t, m, 0, 0, nil, "(a -> nexus)[0]")
assertQuery(t, m, 0, 0, nil, "(b -> nexus)[0]")
assertQuery(t, m, 0, 0, nil, "(c -> nexus)[0]")
assertQuery(t, m, 0, 0, nil, "(d -> nexus)[0]")
},
},
{
name: "double-glob/1",
run: func(t testing.TB) {
Expand All @@ -205,6 +222,22 @@ shared.animal
assertQuery(t, m, 1, 0, nil, "shared.animal.style")
},
},
{
name: "double-glob/edge-no-container",
run: func(t testing.TB) {
m, err := compile(t, `zone A: {
machine A
machine B: {
submachine A
submachine B
}
}
zone A.** -> load balancer
`)
assert.Success(t, err)
assertQuery(t, m, 6, 3, nil, "")
},
},
{
name: "reserved",
run: func(t testing.TB) {
Expand All @@ -220,7 +253,7 @@ Spiderman 3
* -> *: arrow`)
assert.Success(t, err)
assertQuery(t, m, 6, 9, nil, "")
assertQuery(t, m, 6, 6, nil, "")
assertQuery(t, m, 0, 0, "arrow", "(* -> *)[*]")
},
},
Expand Down
Loading

0 comments on commit 9c37d6d

Please sign in to comment.