Skip to content

Commit

Permalink
d2ir: Make globs work through imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Sep 4, 2023
1 parent d03082d commit 303058d
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 2 deletions.
31 changes: 29 additions & 2 deletions d2ir/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,14 @@ func (c *compiler) overlay(base *Map, f *Field) {
f.Composite = base
}

func (g *globContext) prefixed(dst *Map) *globContext {
func (g *globContext) copy() *globContext {
g2 := *g
g2.refctx = g.root.refctx.Copy()
return &g2
}

func (g *globContext) prefixed(dst *Map) *globContext {
g2 := g.copy()
prefix := d2ast.MakeKeyPath(RelIDA(g2.refctx.ScopeMap, dst))
g2.refctx.Key = g2.refctx.Key.Copy()
if g2.refctx.Key.Key != nil {
Expand All @@ -373,7 +378,7 @@ func (g *globContext) prefixed(dst *Map) *globContext {
if len(prefix.Path) > 0 {
g2.refctx.Key.Key = prefix
}
return &g2
return g2
}

func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool {
Expand Down Expand Up @@ -427,6 +432,7 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
c.globContextStack = append(c.globContextStack, globs)
defer func() {
dst.globs = c.globContexts()
c.globContextStack = c.globContextStack[:len(c.globContextStack)-1]
}()

Expand Down Expand Up @@ -464,6 +470,20 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
c.errorf(n.Import, "cannot spread import non map into map")
continue
}

for _, gctx := range impn.Map().globs {
if !gctx.refctx.Key.HasTripleGlob() {
continue
}
if gctx.refctx.ScopeMap != impn.Map() {
continue
}
gctx2 := gctx.copy()
gctx2.refctx.ScopeMap = dst
c.compileKey(gctx2.refctx)
c.ensureGlobContext(gctx2.refctx)
}

OverlayMap(dst, impn.Map())

if impnf, ok := impn.(*Field); ok {
Expand All @@ -474,6 +494,13 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
}
}
}

for _, gctx2 := range c.globContexts() {
old := c.lazyGlobBeingApplied
c.lazyGlobBeingApplied = true
c.compileKey(gctx2.refctx)
c.lazyGlobBeingApplied = old
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ type Map struct {
parent Node
Fields []*Field `json:"fields"`
Edges []*Edge `json:"edges"`

globs []*globContext
}

func (m *Map) initRoot() {
Expand Down
16 changes: 16 additions & 0 deletions d2ir/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,22 @@ two
assertQuery(t, m, 12, 0, nil, "")
},
},
{
name: "import-glob",
run: func(t testing.TB) {
m, err := compileFS(t, "index.d2", map[string]string{
"index.d2": "before; ...@globs.d2; after",
"globs.d2": `*: jingle
**: true
***: meow`,
})
assert.Success(t, err)

assertQuery(t, m, 2, 0, nil, "")
assertQuery(t, m, 0, 0, "meow", "before")
assertQuery(t, m, 0, 0, "meow", "after")
},
},
}

runa(t, tca)
Expand Down
143 changes: 143 additions & 0 deletions testdata/d2ir/TestCompile/patterns/import-glob.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 303058d

Please sign in to comment.