d2ir: Make double globs work sanely across boards

See test.
This commit is contained in:
Anmol Sethi 2023-07-30 03:15:33 -07:00
parent 9c37d6dcfb
commit 6fdf4b07a5
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
6 changed files with 5284 additions and 3 deletions

View file

@ -747,6 +747,15 @@ func (kp *KeyPath) Copy() *KeyPath {
return &kp2
}
func (kp *KeyPath) HasDoubleGlob() bool {
for _, el := range kp.Path {
if el.ScalarString() == "**" {
return true
}
}
return false
}
type Edge struct {
Range Range `json:"range"`

View file

@ -1084,11 +1084,15 @@ func (m *Map) createEdge(eid *EdgeID, refctx *RefContext, ea *[]*Edge) error {
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 srcKP.HasDoubleGlob() || dstKP.HasDoubleGlob() {
// If either has a double glob we only select leafs, those without children.
if src.Map().IsContainer() || dst.Map().IsContainer() {
continue
}
// If either has a double glob we ignore connections across boards
if ParentBoard(src) != ParentBoard(dst) {
continue
}
}
eid2 := eid.Copy()

View file

@ -18,7 +18,9 @@ func (m *Map) doubleGlob(pattern []string) ([]*Field, bool) {
func (m *Map) _doubleGlob(fa *[]*Field) {
for _, f := range m.Fields {
if _, ok := d2graph.ReservedKeywords[f.Name]; ok {
continue
if _, ok := d2graph.BoardKeywords[f.Name]; !ok {
continue
}
}
*fa = append(*fa, f)
if f.Map() != nil {

View file

@ -257,6 +257,34 @@ Spiderman 3
assertQuery(t, m, 0, 0, "arrow", "(* -> *)[*]")
},
},
{
name: "scenarios",
run: func(t testing.TB) {
m, err := compile(t, `
scenarios: {
meow: {
e
f
g
h
}
}
a
b
c
d
**: something
** -> **
`)
assert.Success(t, err)
assertQuery(t, m, 10, 24, nil, "")
assertQuery(t, m, 0, 0, "something", "**")
assertQuery(t, m, 0, 0, nil, "(* -> *)[*]")
},
},
}
runa(t, tca)

1375
testdata/d2ir/TestCompile/layers/errs/3/bad_edge.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff

3863
testdata/d2ir/TestCompile/patterns/scenarios.exp.json generated vendored Normal file

File diff suppressed because it is too large Load diff