Skip to content

Commit 808f5c3

Browse files
authoredMar 7, 2024
Merge pull request #1857 from alixander/delete-connection-glob
fix edge glob bug
2 parents ff9150a + 993e369 commit 808f5c3

File tree

27 files changed

+7127
-13122
lines changed

27 files changed

+7127
-13122
lines changed
 

‎ci/release/changelogs/next.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99

1010
#### Bugfixes ⛑️
1111

12+
- Fixes styles in connections not overriding styles set by globs [#1857](https://github.com/terrastruct/d2/pull/1857)
1213
- Fixes `null` being set on a nested shape not working in certain cases when connections also pointed to that shape [#1830](https://github.com/terrastruct/d2/pull/1830)
1314
- Fixes edge case of bad import syntax crashing using d2 as a library [#1829](https://github.com/terrastruct/d2/pull/1829)

‎d2ast/d2ast.go

+1
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ type EdgeIndex struct {
10251025
}
10261026

10271027
func (ei1 *EdgeIndex) Equals(ei2 *EdgeIndex) bool {
1028+
// TODO probably should be checking the values, but will wait until something breaks to change
10281029
if ei1.Int != ei2.Int {
10291030
return false
10301031
}

‎d2compiler/compile_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -4367,6 +4367,29 @@ container_2: {
43674367
assert.Equal(t, 4, len(g.Objects))
43684368
},
43694369
},
4370+
{
4371+
name: "override-edge/1",
4372+
run: func(t *testing.T) {
4373+
g, _ := assertCompile(t, `
4374+
(* -> *)[*].style.stroke: red
4375+
(* -> *)[*].style.stroke: green
4376+
a -> b
4377+
`, ``)
4378+
assert.Equal(t, "green", g.Edges[0].Attributes.Style.Stroke.Value)
4379+
},
4380+
},
4381+
{
4382+
name: "override-edge/2",
4383+
run: func(t *testing.T) {
4384+
g, _ := assertCompile(t, `
4385+
(* -> *)[*].style.stroke: red
4386+
a -> b: {style.stroke: green}
4387+
a -> b
4388+
`, ``)
4389+
assert.Equal(t, "green", g.Edges[0].Attributes.Style.Stroke.Value)
4390+
assert.Equal(t, "red", g.Edges[1].Attributes.Style.Stroke.Value)
4391+
},
4392+
},
43704393
}
43714394

43724395
for _, tc := range tca {

‎d2ir/compile.go

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ func (c *compiler) ampersandFilterMap(dst *Map, ast, scopeAST *d2ast.Map) bool {
413413
ks = d2format.Format(d2ast.MakeKeyPath(BoardIDA(dst)))
414414
}
415415
delete(gctx.appliedFields, ks)
416+
delete(gctx.appliedEdges, ks)
416417
return false
417418
}
418419
}

‎d2ir/d2ir.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ func (m *Map) getEdges(eid *EdgeID, refctx *RefContext, gctx *globContext, ea *[
10941094
}
10951095
gctx.appliedEdges[ks] = struct{}{}
10961096
}
1097-
*ea = append(*ea, ea2...)
1097+
*ea = append(*ea, e)
10981098
}
10991099
}
11001100
}

‎d2ir/filter_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ x -> y
150150
assertQuery(t, m, 0, 0, 0.1, "(x -> y)[1].style.opacity")
151151
},
152152
},
153+
{
154+
name: "label-filter/3",
155+
run: func(t testing.TB) {
156+
m, err := compile(t, `
157+
(* -> *)[*]: {
158+
&label: hi
159+
style.opacity: 0.1
160+
}
161+
162+
x -> y: hi
163+
`)
164+
assert.Success(t, err)
165+
assertQuery(t, m, 0, 0, 0.1, "(x -> y)[0].style.opacity")
166+
},
167+
},
153168
{
154169
name: "lazy-filter",
155170
run: func(t testing.TB) {

‎d2ir/import_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ label: meow`,
232232
_, err := compileFS(t, "index.d2", map[string]string{
233233
"index.d2": "...@'./../x.d2'",
234234
})
235-
assert.ErrorString(t, err, `index.d2:1:1: failed to import "../x.d2": stat ../x.d2: invalid argument`)
235+
assert.ErrorString(t, err, `index.d2:1:1: failed to import "../x.d2": open ../x.d2: invalid argument`)
236236
},
237237
},
238238
{

‎d2oracle/edit.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -497,16 +497,19 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
497497
onlyInChain = false
498498
}
499499
}
500-
// If a ref has an exact match on this key, just change the value
501-
tmp1 := *ref.MapKey
502-
tmp2 := *mk
503-
noVal1 := &tmp1
504-
noVal2 := &tmp2
505-
noVal1.Value = d2ast.ValueBox{}
506-
noVal2.Value = d2ast.ValueBox{}
507-
if noVal1.D2OracleEquals(noVal2) {
508-
ref.MapKey.Value = mk.Value
509-
return nil
500+
501+
if ref.MapKey.EdgeIndex == nil || !ref.MapKey.EdgeIndex.Glob {
502+
// If a ref has an exact match on this key, just change the value
503+
tmp1 := *ref.MapKey
504+
tmp2 := *mk
505+
noVal1 := &tmp1
506+
noVal2 := &tmp2
507+
noVal1.Value = d2ast.ValueBox{}
508+
noVal2.Value = d2ast.ValueBox{}
509+
if noVal1.D2OracleEquals(noVal2) {
510+
ref.MapKey.Value = mk.Value
511+
return nil
512+
}
510513
}
511514
}
512515
if onlyInChain {
@@ -575,6 +578,10 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
575578
if s.MapKey.Range.Path != baseAST.Range.Path {
576579
return false
577580
}
581+
// Globs are also not writeable
582+
if s.MapKey.HasGlob() {
583+
return false
584+
}
578585
}
579586
return s != nil && s.MapKey != nil && !ir.InClass(s.MapKey)
580587
}

‎d2oracle/edit_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,48 @@ layers: {
23352335
near: bottom-right
23362336
}
23372337
}
2338+
`,
2339+
},
2340+
{
2341+
name: "glob-field/1",
2342+
2343+
text: `*.style.fill: red
2344+
a
2345+
b
2346+
`,
2347+
key: `a.style.fill`,
2348+
value: go2.Pointer(`blue`),
2349+
exp: `*.style.fill: red
2350+
a: {style.fill: blue}
2351+
b
2352+
`,
2353+
},
2354+
{
2355+
name: "glob-field/2",
2356+
2357+
text: `(* -> *)[*].style.stroke: red
2358+
a -> b
2359+
a -> b
2360+
`,
2361+
key: `(a -> b)[0].style.stroke`,
2362+
value: go2.Pointer(`blue`),
2363+
exp: `(* -> *)[*].style.stroke: red
2364+
a -> b: {style.stroke: blue}
2365+
a -> b
2366+
`,
2367+
},
2368+
{
2369+
name: "glob-field/3",
2370+
2371+
text: `(* -> *)[*].style.stroke: red
2372+
a -> b: {style.stroke: blue}
2373+
a -> b
2374+
`,
2375+
key: `(a -> b)[0].style.stroke`,
2376+
value: go2.Pointer(`green`),
2377+
exp: `(* -> *)[*].style.stroke: red
2378+
a -> b: {style.stroke: green}
2379+
a -> b
23382380
`,
23392381
},
23402382
}
@@ -7445,6 +7487,32 @@ a.style.fill: null
74457487
`,
74467488
key: `yes.label.near`,
74477489
exp: `yes
7490+
`,
7491+
},
7492+
{
7493+
name: "connection-glob",
7494+
7495+
text: `* -> *
7496+
a
7497+
b
7498+
`,
7499+
key: `(a -> b)[0]`,
7500+
exp: `* -> *
7501+
a
7502+
b
7503+
(a -> b)[0]: null
7504+
`,
7505+
},
7506+
{
7507+
name: "glob-child/1",
7508+
7509+
text: `*.b
7510+
a
7511+
`,
7512+
key: `a.b`,
7513+
exp: `*.b
7514+
a
7515+
a.b: null
74487516
`,
74497517
},
74507518
}

‎d2oracle/get.go

+8
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, er
142142

143143
func IsImportedObj(ast *d2ast.Map, obj *d2graph.Object) bool {
144144
for _, ref := range obj.References {
145+
if ref.Key.HasGlob() {
146+
return true
147+
}
145148
if ref.Key.Range.Path != ast.Range.Path {
146149
return true
147150
}
@@ -150,8 +153,13 @@ func IsImportedObj(ast *d2ast.Map, obj *d2graph.Object) bool {
150153
return false
151154
}
152155

156+
// Globs count as imported for now
157+
// TODO Probably rename later
153158
func IsImportedEdge(ast *d2ast.Map, edge *d2graph.Edge) bool {
154159
for _, ref := range edge.References {
160+
if ref.Edge.Src.HasGlob() || ref.Edge.Dst.HasGlob() {
161+
return true
162+
}
155163
if ref.Edge.Range.Path != ast.Range.Path {
156164
return true
157165
}

‎docs/CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ git submodule update --recursive
6868

6969
## Logistics
7070

71-
- Use Go 1.20.
71+
- Use Go 1.22.
7272
- Please sign your commits
7373
([https://github.com/terrastruct/d2/pull/557#issuecomment-1367468730](https://github.com/terrastruct/d2/pull/557#issuecomment-1367468730)).
7474
- D2 uses Issues as TODOs. No auto-closing on staleness.

‎go.mod

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go.sum

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Failed to load comments.