d2ir: Implement edge index globs
This commit is contained in:
parent
d9b4b952ee
commit
0d2b0aa99c
5 changed files with 4022 additions and 2 deletions
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
4003
testdata/d2ir/TestCompile/patterns/edge-glob-index.exp.json
generated
vendored
Normal file
4003
testdata/d2ir/TestCompile/patterns/edge-glob-index.exp.json
generated
vendored
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue