d2ir: Implement edge index globs

This commit is contained in:
Anmol Sethi 2023-07-27 02:51:43 -07:00
parent d9b4b952ee
commit 0d2b0aa99c
No known key found for this signature in database
GPG key ID: 8CEF1878FF10ADEB
5 changed files with 4022 additions and 2 deletions

View file

@ -647,7 +647,7 @@ func (c *compiler) _compileEdges(refctx *RefContext) {
refctx.Edge = refctx.Key.Edges[i] refctx.Edge = refctx.Key.Edges[i]
var ea []*Edge var ea []*Edge
if eid.Index != nil { if eid.Index != nil || eid.Glob {
ea = refctx.ScopeMap.GetEdges(eid) ea = refctx.ScopeMap.GetEdges(eid)
if len(ea) == 0 { if len(ea) == 0 {
c.errorf(refctx.Edge, "indexed edge does not exist") c.errorf(refctx.Edge, "indexed edge does not exist")

View file

@ -325,6 +325,7 @@ type EdgeID struct {
// If nil, then any EdgeID with equal src/dst/arrows matches. // If nil, then any EdgeID with equal src/dst/arrows matches.
Index *int `json:"index"` Index *int `json:"index"`
Glob bool `json:"glob"`
} }
func NewEdgeIDs(k *d2ast.Key) (eida []*EdgeID) { func NewEdgeIDs(k *d2ast.Key) (eida []*EdgeID) {
@ -337,6 +338,7 @@ func NewEdgeIDs(k *d2ast.Key) (eida []*EdgeID) {
} }
if k.EdgeIndex != nil { if k.EdgeIndex != nil {
eid.Index = k.EdgeIndex.Int eid.Index = k.EdgeIndex.Int
eid.Glob = k.EdgeIndex.Glob
} }
eida = append(eida, eid) eida = append(eida, eid)
} }

View file

@ -133,6 +133,21 @@ sh*.an* -> sh*.an*`)
assertQuery(t, m, 0, 0, nil, "shared.(animal -> animal)[0]") 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", name: "double-glob/1",
run: func(t testing.TB) { run: func(t testing.TB) {

View file

@ -56,7 +56,7 @@ func (m *Map) Query(idStr string) (Node, error) {
return nil, nil return nil, nil
} }
if len(na) > 1 { 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 return na[0], nil
} }

File diff suppressed because it is too large Load diff