Skip to content

Commit 7d84b7c

Browse files
authored
Merge pull request terrastruct#1874 from alixander/make-writeable-refs-public
d2oracle: make get writeable refs public
2 parents 3faa50c + 2187bf7 commit 7d84b7c

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

d2chaos/d2chaos.go

+4
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ func (gs *dslGenState) edge() error {
146146
}
147147
}
148148

149+
if src == dst && gs.nodeShapes[dst] == d2target.ShapeSequenceDiagram {
150+
return nil
151+
}
152+
149153
srcArrow := "-"
150154
if gs.randBool() {
151155
srcArrow = "<"

d2oracle/edit.go

+7-25
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func ReconnectEdge(g *d2graph.Graph, boardPath []string, edgeKey string, srcKey,
199199

200200
refs := edge.References
201201
if baseAST != g.AST {
202-
refs = getWriteableEdgeRefs(edge, baseAST)
202+
refs = GetWriteableEdgeRefs(edge, baseAST)
203203
if len(refs) == 0 || refs[0].ScopeAST != baseAST {
204204
// TODO null
205205
return nil, OutsideScopeError{}
@@ -387,7 +387,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
387387

388388
var maybeNewScope *d2ast.Map
389389
if baseAST != g.AST || imported {
390-
writeableRefs := getWriteableRefs(obj, baseAST)
390+
writeableRefs := GetWriteableRefs(obj, baseAST)
391391
for _, ref := range writeableRefs {
392392
if ref.MapKey != nil && ref.MapKey.Value.Map != nil {
393393
maybeNewScope = ref.MapKey.Value.Map
@@ -414,7 +414,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
414414
writeableLabelMK := true
415415
var objK *d2ast.Key
416416
if baseAST != g.AST || imported {
417-
writeableRefs := getWriteableRefs(obj, baseAST)
417+
writeableRefs := GetWriteableRefs(obj, baseAST)
418418
if len(writeableRefs) > 0 {
419419
objK = writeableRefs[0].MapKey
420420
}
@@ -497,7 +497,7 @@ func _set(g *d2graph.Graph, baseAST *d2ast.Map, key string, tag, value *string)
497497
imported = IsImportedEdge(baseAST, edge)
498498
refs := edge.References
499499
if baseAST != g.AST || imported {
500-
refs = getWriteableEdgeRefs(edge, baseAST)
500+
refs = GetWriteableEdgeRefs(edge, baseAST)
501501
}
502502
onlyInChain := true
503503
for _, ref := range refs {
@@ -920,7 +920,7 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
920920
} else {
921921
refs := e.References
922922
if len(boardPath) > 0 {
923-
refs := getWriteableEdgeRefs(e, baseAST)
923+
refs := GetWriteableEdgeRefs(e, baseAST)
924924
if len(refs) != len(e.References) {
925925
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
926926
}
@@ -991,7 +991,7 @@ func Delete(g *d2graph.Graph, boardPath []string, key string) (_ *d2graph.Graph,
991991
return g, nil
992992
}
993993
if len(boardPath) > 0 {
994-
writeableRefs := getWriteableRefs(obj, baseAST)
994+
writeableRefs := GetWriteableRefs(obj, baseAST)
995995
if len(writeableRefs) != len(obj.References) {
996996
mk.Value = d2ast.MakeValueBox(&d2ast.Null{})
997997
}
@@ -1759,7 +1759,7 @@ func move(g *d2graph.Graph, boardPath []string, key, newKey string, includeDesce
17591759
}
17601760

17611761
if len(boardPath) > 0 {
1762-
writeableRefs := getWriteableRefs(obj, baseAST)
1762+
writeableRefs := GetWriteableRefs(obj, baseAST)
17631763
if len(writeableRefs) != len(obj.References) {
17641764
return nil, OutsideScopeError{}
17651765
}
@@ -3226,21 +3226,3 @@ func filterReservedPath(path []*d2ast.StringBox) (filtered []*d2ast.StringBox) {
32263226
}
32273227
return
32283228
}
3229-
3230-
func getWriteableRefs(obj *d2graph.Object, writeableAST *d2ast.Map) (out []d2graph.Reference) {
3231-
for i, ref := range obj.References {
3232-
if ref.ScopeAST == writeableAST && ref.Key.Range.Path == writeableAST.Range.Path {
3233-
out = append(out, obj.References[i])
3234-
}
3235-
}
3236-
return
3237-
}
3238-
3239-
func getWriteableEdgeRefs(edge *d2graph.Edge, writeableAST *d2ast.Map) (out []d2graph.EdgeReference) {
3240-
for i, ref := range edge.References {
3241-
if ref.ScopeAST == writeableAST {
3242-
out = append(out, edge.References[i])
3243-
}
3244-
}
3245-
return
3246-
}

d2oracle/get.go

+18
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,21 @@ func GetID(key string) string {
245245

246246
return d2format.Format(d2ast.RawString(mk.Key.Path[len(mk.Key.Path)-1].Unbox().ScalarString(), true))
247247
}
248+
249+
func GetWriteableRefs(obj *d2graph.Object, writeableAST *d2ast.Map) (out []d2graph.Reference) {
250+
for i, ref := range obj.References {
251+
if ref.ScopeAST == writeableAST && ref.Key.Range.Path == writeableAST.Range.Path {
252+
out = append(out, obj.References[i])
253+
}
254+
}
255+
return
256+
}
257+
258+
func GetWriteableEdgeRefs(edge *d2graph.Edge, writeableAST *d2ast.Map) (out []d2graph.EdgeReference) {
259+
for i, ref := range edge.References {
260+
if ref.ScopeAST == writeableAST {
261+
out = append(out, edge.References[i])
262+
}
263+
}
264+
return
265+
}

0 commit comments

Comments
 (0)