Skip to content

Commit

Permalink
d2ir: Fix globs to not match reserved
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Jul 29, 2023
1 parent 82663f0 commit 63efa12
Show file tree
Hide file tree
Showing 5 changed files with 1,729 additions and 11 deletions.
22 changes: 13 additions & 9 deletions d2ir/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,27 @@ func assertQuery(t testing.TB, n d2ir.Node, nfields, nedges int, primary interfa
m := n.Map()
p := n.Primary()

var na []d2ir.Node
if idStr != "" {
var err error
n, err = m.Query(idStr)
na, err = m.QueryAll(idStr)
assert.Success(t, err)
assert.NotEqual(t, n, nil)

p = n.Primary()
m = n.Map()
} else {
na = append(na, n)
}

assert.Equal(t, nfields, m.FieldCountRecursive())
assert.Equal(t, nedges, m.EdgeCountRecursive())
if !makeScalar(p).Equal(makeScalar(primary)) {
t.Fatalf("expected primary %#v but got %s", primary, p)
for _, n := range na {
m = n.Map()
p = n.Primary()
assert.Equal(t, nfields, m.FieldCountRecursive())
assert.Equal(t, nedges, m.EdgeCountRecursive())
if !makeScalar(p).Equal(makeScalar(primary)) {
t.Fatalf("expected primary %#v but got %s", primary, p)
}
}

return n
return na[0]
}

func makeScalar(v interface{}) *d2ir.Scalar {
Expand Down
3 changes: 3 additions & 0 deletions d2ir/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func matchPattern(s string, pattern []string) bool {
if len(pattern) == 0 {
return true
}
if _, ok := d2graph.ReservedKeywords[s]; ok {
return false
}

for i := 0; i < len(pattern); i++ {
if pattern[i] == "*" {
Expand Down
19 changes: 19 additions & 0 deletions d2ir/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,25 @@ shared.animal
assertQuery(t, m, 1, 0, nil, "shared.animal.style")
},
},
{
name: "reserved",
run: func(t testing.TB) {
m, err := compile(t, `vars: {
d2-config: {
layout-engine: elk
}
}
Spiderman 1
Spiderman 2
Spiderman 3
* -> *: arrow`)
assert.Success(t, err)
assertQuery(t, m, 6, 9, nil, "")
assertQuery(t, m, 0, 0, "arrow", "(* -> *)[*]")
},
},
}

runa(t, tca)
Expand Down
10 changes: 8 additions & 2 deletions d2ir/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ func (m *Map) QueryAll(idStr string) (na []Node, _ error) {
}

eida := NewEdgeIDs(k)
for _, eid := range eida {
ea := m.GetEdges(eid, nil)

for i, eid := range eida {
refctx := &RefContext{
Key: k,
ScopeMap: m,
Edge: k.Edges[i],
}
ea := m.GetEdges(eid, refctx)
for _, e := range ea {
if k.EdgeKey == nil {
na = append(na, e)
Expand Down
Loading

0 comments on commit 63efa12

Please sign in to comment.