d2ir: Make globs work through imports
This commit is contained in:
parent
d03082d84d
commit
b1f9083ba1
4 changed files with 183 additions and 2 deletions
|
|
@ -362,9 +362,14 @@ func (c *compiler) overlay(base *Map, f *Field) {
|
||||||
f.Composite = base
|
f.Composite = base
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *globContext) prefixed(dst *Map) *globContext {
|
func (g *globContext) copy() *globContext {
|
||||||
g2 := *g
|
g2 := *g
|
||||||
g2.refctx = g.root.refctx.Copy()
|
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))
|
prefix := d2ast.MakeKeyPath(RelIDA(g2.refctx.ScopeMap, dst))
|
||||||
g2.refctx.Key = g2.refctx.Key.Copy()
|
g2.refctx.Key = g2.refctx.Key.Copy()
|
||||||
if g2.refctx.Key.Key != nil {
|
if g2.refctx.Key.Key != nil {
|
||||||
|
|
@ -373,7 +378,7 @@ func (g *globContext) prefixed(dst *Map) *globContext {
|
||||||
if len(prefix.Path) > 0 {
|
if len(prefix.Path) > 0 {
|
||||||
g2.refctx.Key.Key = prefix
|
g2.refctx.Key.Key = prefix
|
||||||
}
|
}
|
||||||
return &g2
|
return g2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool {
|
func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool {
|
||||||
|
|
@ -427,6 +432,7 @@ func (c *compiler) compileMap(dst *Map, ast, scopeAST *d2ast.Map) {
|
||||||
}
|
}
|
||||||
c.globContextStack = append(c.globContextStack, globs)
|
c.globContextStack = append(c.globContextStack, globs)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
dst.globs = c.globContexts()
|
||||||
c.globContextStack = c.globContextStack[:len(c.globContextStack)-1]
|
c.globContextStack = c.globContextStack[:len(c.globContextStack)-1]
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
@ -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")
|
c.errorf(n.Import, "cannot spread import non map into map")
|
||||||
continue
|
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())
|
OverlayMap(dst, impn.Map())
|
||||||
|
|
||||||
if impnf, ok := impn.(*Field); ok {
|
if impnf, ok := impn.(*Field); ok {
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,8 @@ type Map struct {
|
||||||
parent Node
|
parent Node
|
||||||
Fields []*Field `json:"fields"`
|
Fields []*Field `json:"fields"`
|
||||||
Edges []*Edge `json:"edges"`
|
Edges []*Edge `json:"edges"`
|
||||||
|
|
||||||
|
globs []*globContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Map) initRoot() {
|
func (m *Map) initRoot() {
|
||||||
|
|
|
||||||
|
|
@ -734,6 +734,22 @@ two
|
||||||
assertQuery(t, m, 12, 0, nil, "")
|
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)
|
runa(t, tca)
|
||||||
|
|
|
||||||
143
testdata/d2ir/TestCompile/patterns/import-glob.exp.json
generated
vendored
Normal file
143
testdata/d2ir/TestCompile/patterns/import-glob.exp.json
generated
vendored
Normal file
|
|
@ -0,0 +1,143 @@
|
||||||
|
{
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"name": "before",
|
||||||
|
"primary": {
|
||||||
|
"value": {
|
||||||
|
"range": "globs.d2,2:5:24-2:9:28",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "meow",
|
||||||
|
"raw_string": "meow"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"string": {
|
||||||
|
"range": "index.d2,0:0:0-0:6:6",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "before",
|
||||||
|
"raw_string": "before"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"key_path": {
|
||||||
|
"range": "index.d2,0:0:0-0:6:6",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "index.d2,0:0:0-0:6:6",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "before",
|
||||||
|
"raw_string": "before"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"context": {
|
||||||
|
"edge": null,
|
||||||
|
"key": {
|
||||||
|
"range": "index.d2,0:0:0-0:6:6",
|
||||||
|
"key": {
|
||||||
|
"range": "index.d2,0:0:0-0:6:6",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "index.d2,0:0:0-0:6:6",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "before",
|
||||||
|
"raw_string": "before"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"primary": {},
|
||||||
|
"value": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"due_to_glob": false,
|
||||||
|
"due_to_lazy_glob": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "after",
|
||||||
|
"primary": {
|
||||||
|
"value": {
|
||||||
|
"range": "globs.d2,2:5:24-2:9:28",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "meow",
|
||||||
|
"raw_string": "meow"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"string": {
|
||||||
|
"range": "index.d2,0:22:22-0:27:27",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "after",
|
||||||
|
"raw_string": "after"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"key_path": {
|
||||||
|
"range": "index.d2,0:22:22-0:27:27",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "index.d2,0:22:22-0:27:27",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "after",
|
||||||
|
"raw_string": "after"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"context": {
|
||||||
|
"edge": null,
|
||||||
|
"key": {
|
||||||
|
"range": "index.d2,0:22:22-0:27:27",
|
||||||
|
"key": {
|
||||||
|
"range": "index.d2,0:22:22-0:27:27",
|
||||||
|
"path": [
|
||||||
|
{
|
||||||
|
"unquoted_string": {
|
||||||
|
"range": "index.d2,0:22:22-0:27:27",
|
||||||
|
"value": [
|
||||||
|
{
|
||||||
|
"string": "after",
|
||||||
|
"raw_string": "after"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"primary": {},
|
||||||
|
"value": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"due_to_glob": false,
|
||||||
|
"due_to_lazy_glob": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"edges": null
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue