Skip to content

Commit

Permalink
d2ir: Implement edge index globs
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Jul 27, 2023
1 parent a33cb20 commit 4a99d66
Show file tree
Hide file tree
Showing 5 changed files with 4,022 additions and 2 deletions.
2 changes: 1 addition & 1 deletion d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ func (c *compiler) _compileEdges(refctx *RefContext) {
refctx.Edge = refctx.Key.Edges[i]

var ea []*Edge
if eid.Index != nil {
if eid.Index != nil || eid.Glob {
ea = refctx.ScopeMap.GetEdges(eid)
if len(ea) == 0 {
c.errorf(refctx.Edge, "indexed edge does not exist")
Expand Down
2 changes: 2 additions & 0 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ type EdgeID struct {

// If nil, then any EdgeID with equal src/dst/arrows matches.
Index *int `json:"index"`
Glob bool `json:"glob"`
}

func NewEdgeIDs(k *d2ast.Key) (eida []*EdgeID) {
Expand All @@ -337,6 +338,7 @@ func NewEdgeIDs(k *d2ast.Key) (eida []*EdgeID) {
}
if k.EdgeIndex != nil {
eid.Index = k.EdgeIndex.Int
eid.Glob = k.EdgeIndex.Glob
}
eida = append(eida, eid)
}
Expand Down
15 changes: 15 additions & 0 deletions d2ir/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ sh*.an* -> sh*.an*`)
assertQuery(t, m, 0, 0, nil, "shared.(animal -> animal)[0]")
},
},
{
name: "edge-glob-index",
run: func(t testing.TB) {
m, err := compile(t, `a -> b
a -> b
a -> b
(a -> b)[*].style.fill: red
`)
assert.Success(t, err)
assertQuery(t, m, 8, 3, nil, "")
assertQuery(t, m, 0, 0, "red", "(a -> b)[0].style.fill")
assertQuery(t, m, 0, 0, "red", "(a -> b)[1].style.fill")
assertQuery(t, m, 0, 0, "red", "(a -> b)[2].style.fill")
},
},
{
name: "double-glob/1",
run: func(t testing.TB) {
Expand Down
2 changes: 1 addition & 1 deletion d2ir/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (m *Map) Query(idStr string) (Node, error) {
return nil, nil
}
if len(na) > 1 {
return nil, fmt.Errorf("expected only one query result but got: %#v", err)
return nil, fmt.Errorf("expected only one query result but got: %#v", na)
}
return na[0], nil
}
Loading

0 comments on commit 4a99d66

Please sign in to comment.