Skip to content

Commit 20c3598

Browse files
authored
Merge pull request #1832 from alixander/fix-edge-delete
Fix d2oracle.set and d2oracle.delete on imported edges
2 parents 86f96fc + 706951b commit 20c3598

File tree

5 files changed

+598
-36
lines changed

5 files changed

+598
-36
lines changed

d2oracle/edit.go

+42-35
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
383383
break
384384
}
385385
obj = o
386-
imported = IsImported(baseAST, obj)
386+
imported = IsImportedObj(baseAST, obj)
387387

388388
var maybeNewScope *d2ast.Map
389389
if baseAST != g.AST || imported {
@@ -481,8 +481,9 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
481481
if !ok {
482482
return errors.New("edge not found")
483483
}
484+
imported = IsImportedEdge(baseAST, edge)
484485
refs := edge.References
485-
if baseAST != g.AST {
486+
if baseAST != g.AST || imported {
486487
refs = getWriteableEdgeRefs(edge, baseAST)
487488
}
488489
onlyInChain := true
@@ -868,45 +869,52 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
868869
return g, nil
869870
}
870871

871-
refs := e.References
872-
if len(boardPath) > 0 {
873-
refs := getWriteableEdgeRefs(e, baseAST)
874-
if len(refs) != len(e.References) {
875-
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
876-
}
877-
}
872+
imported := IsImportedEdge(baseAST, e)
878873

879-
if _, ok := mk.Value.Unbox().(*d2ast.Null); !ok {
880-
ref := refs[0]
881-
var refEdges []*d2ast.Edge
882-
for _, ref := range refs {
883-
refEdges = append(refEdges, ref.Edge)
874+
if imported {
875+
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
876+
appendMapKey(baseAST, mk)
877+
} else {
878+
refs := e.References
879+
if len(boardPath) > 0 {
880+
refs := getWriteableEdgeRefs(e, baseAST)
881+
if len(refs) != len(e.References) {
882+
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
883+
}
884884
}
885-
ensureNode(g, refEdges, ref.ScopeObj, ref.Scope, ref.MapKey, ref.MapKey.Edges[ref.MapKeyEdgeIndex].Src, true)
886-
ensureNode(g, refEdges, ref.ScopeObj, ref.Scope, ref.MapKey, ref.MapKey.Edges[ref.MapKeyEdgeIndex].Dst, false)
887885

888-
for i := len(e.References) - 1; i >= 0; i-- {
889-
ref := e.References[i]
890-
deleteEdge(g, ref.Scope, ref.MapKey, ref.MapKeyEdgeIndex)
891-
}
886+
if _, ok := mk.Value.Unbox().(*d2ast.Null); !ok {
887+
ref := refs[0]
888+
var refEdges []*d2ast.Edge
889+
for _, ref := range refs {
890+
refEdges = append(refEdges, ref.Edge)
891+
}
892+
ensureNode(g, refEdges, ref.ScopeObj, ref.Scope, ref.MapKey, ref.MapKey.Edges[ref.MapKeyEdgeIndex].Src, true)
893+
ensureNode(g, refEdges, ref.ScopeObj, ref.Scope, ref.MapKey, ref.MapKey.Edges[ref.MapKeyEdgeIndex].Dst, false)
892894

893-
edges, ok := obj.FindEdges(mk)
894-
if ok {
895-
for _, e2 := range edges {
896-
if e2.Index <= e.Index {
897-
continue
898-
}
899-
for i := len(e2.References) - 1; i >= 0; i-- {
900-
ref := e2.References[i]
901-
if ref.MapKey.EdgeIndex != nil {
902-
*ref.MapKey.EdgeIndex.Int--
895+
for i := len(e.References) - 1; i >= 0; i-- {
896+
ref := e.References[i]
897+
deleteEdge(g, ref.Scope, ref.MapKey, ref.MapKeyEdgeIndex)
898+
}
899+
900+
edges, ok := obj.FindEdges(mk)
901+
if ok {
902+
for _, e2 := range edges {
903+
if e2.Index <= e.Index {
904+
continue
905+
}
906+
for i := len(e2.References) - 1; i >= 0; i-- {
907+
ref := e2.References[i]
908+
if ref.MapKey.EdgeIndex != nil {
909+
*ref.MapKey.EdgeIndex.Int--
910+
}
903911
}
904912
}
905913
}
914+
} else {
915+
// NOTE: it only needs to be after the last ref, but perhaps simplest and cleanest to append all nulls at the end
916+
appendMapKey(baseAST, mk)
906917
}
907-
} else {
908-
// NOTE: it only needs to be after the last ref, but perhaps simplest and cleanest to append all nulls at the end
909-
appendMapKey(baseAST, mk)
910918
}
911919
if len(boardPath) > 0 {
912920
replaced := ReplaceBoardNode(g.AST, baseAST, boardPath)
@@ -925,10 +933,9 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
925933
return g, nil
926934
}
927935

928-
imported := IsImported(baseAST, obj)
936+
imported := IsImportedObj(baseAST, obj)
929937

930938
if imported {
931-
println(d2format.Format(boardG.AST))
932939
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
933940
appendMapKey(baseAST, mk)
934941
} else {

d2oracle/edit_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,20 @@ layers: {
21712171
b.style.fill: red
21722172
}
21732173
}
2174+
`,
2175+
},
2176+
{
2177+
name: "import/9",
2178+
2179+
text: `...@yo
2180+
`,
2181+
fsTexts: map[string]string{
2182+
"yo.d2": `a -> b`,
2183+
},
2184+
key: `(a -> b)[0].style.stroke`,
2185+
value: go2.Pointer(`red`),
2186+
exp: `...@yo
2187+
(a -> b)[0].style.stroke: red
21742188
`,
21752189
},
21762190
}
@@ -7205,6 +7219,20 @@ scenarios: {
72057219
x: null
72067220
}
72077221
}
7222+
`,
7223+
},
7224+
{
7225+
name: "import/3",
7226+
7227+
text: `...@meow
7228+
`,
7229+
fsTexts: map[string]string{
7230+
"meow.d2": `a -> b
7231+
`,
7232+
},
7233+
key: `(a -> b)[0]`,
7234+
exp: `...@meow
7235+
(a -> b)[0]: null
72087236
`,
72097237
},
72107238
}

d2oracle/get.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func GetParentID(g *d2graph.Graph, boardPath []string, absID string) (string, er
140140
return obj.Parent.AbsID(), nil
141141
}
142142

143-
func IsImported(ast *d2ast.Map, obj *d2graph.Object) bool {
143+
func IsImportedObj(ast *d2ast.Map, obj *d2graph.Object) bool {
144144
for _, ref := range obj.References {
145145
if ref.Key.Range.Path != ast.Range.Path {
146146
return true
@@ -150,6 +150,16 @@ func IsImported(ast *d2ast.Map, obj *d2graph.Object) bool {
150150
return false
151151
}
152152

153+
func IsImportedEdge(ast *d2ast.Map, edge *d2graph.Edge) bool {
154+
for _, ref := range edge.References {
155+
if ref.Edge.Range.Path != ast.Range.Path {
156+
return true
157+
}
158+
}
159+
160+
return false
161+
}
162+
153163
func GetObj(g *d2graph.Graph, boardPath []string, absID string) *d2graph.Object {
154164
g = GetBoardGraph(g, boardPath)
155165
if g == nil {

testdata/d2oracle/TestDelete/import/3.exp.json

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

0 commit comments

Comments
 (0)